Linux系统如何更改时间时区

Linux系统如何更改时间时区,第1张

对于许多与系统相关的任务和进程,使用正确的时区是必不可少的。例如,cron守护进程使用系统的时区执行cron作业,日志文件中的时间戳基于同一系统的时区。 环境 Centos 8检查现在的时区timedatectl是一个 命令 行实用程序,允许您查看和更改系统的时间和日期。 [root@localhost ~]# timedatectl               Local time: Tue 2020-03-31 16:35:23 CST           Universal time: Tue 2020-03-31 08:35:23 UTC                 RTC time: Tue 2020-03-31 08:35:23                 Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: yes               NTP service: active           RTC in local TZ: no更改时区在更改时区之前,您需要找出要使用的时区名称。要列出所有可用时区,请使用下面的 命令 : [root@localhost ~]# timedatectl list-timezones … Asia/Samarkand Asia/Seoul Asia/Shanghai Asia/Singapore Asia/Srednekolymsk … 然后使用set-timezone选项更改时区,比如更改为Europe/Berlin: [root@localhost ~]# timedatectl set-timezone Europe/Berlin使用创建链接的方式更改时区如果运行的是旧版本的Centos,比如Centos6之前的版本,并且系统上不存在timedatectl命令,则可以通过将/etc/localtime符号链接到/usr/share/zoneinfo目录中的时区文件来更改时区。 查看一下/usr/share/zoninfo下面的时区文件, [root@localhost ~]# ls /usr/share/zoneinfo/ [root@localhost ~]# ls /usr/share/zoneinfo/Asia/ 现在替换时区文件。 [root@localhost ~]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime [root@localhost ~]# date Tue Mar 31 16:53:59 CST 2020 可以看到已经切换到了CST中国标准时间总结在本文中,使用了两种方式修改时区。一种是使用timedatectl,另一种方式使用创建链接的方式。

服务器date时间不准:

jingke@ubuntu:~# date

Tue, 05 Nov 2019 21:08:31 -0500

实际是下午9点

  第一步:执行tzselect

修改/etc/localtime文件

控制系统时区的文件是/etc/localtime。只要把这个文件复制成相应时区的文件,系统时区就改成相应的时间了。

时区信息在:/usr/share/zoneinfo里。比如需要把本机 时区改成Shanghai的时间:

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

这两条指令都可以更改。

使用date查看时间

                                                 第二种修改方式 timedatectl

1.查看当前时间状态

查看当前时间状态 timedatectl status :

wxs@ubuntu:~/6-1/task4$ timedatectl status

      Local time: Tue 2017-11-07 18:14:30 PST

  Universal time: Wed 2017-11-08 02:14:30 UTC

        RTC time: Wed 2017-11-08 02:14:29

      Time zone: America/Los_Angeles (PST, -0800)

Network time on: yes

NTP synchronized: yes

RTC in local TZ: no

系统显示的时间是错误的,错误原因也是显而易见的,使用的是America/Los_Angeles的时区。

因此只要修改时区就能保证时间的正确。

2.修改时区

所有的时区名称存储在/usr/share/zoneinfo文件中。

执行命令timedatectl set-timezone "Asia/Shanghai" 就可以将时区设为上海时区。

==== AUTHENTICATING FOR org.freedesktop.timedate1.set-timezone ===

Authentication is required to set the system timezone.

Authenticating as: wxs,,, (wxs)

Password:

==== AUTHENTICATION COMPLETE ===12345

重新查看当前时间状态 timedatectl status :

wxs@ubuntu:~/6-1/task4$ timedatectl status

      Local time: Wed 2017-11-08 10:15:45 CST

  Universal time: Wed 2017-11-08 02:15:45 UTC

        RTC time: Wed 2017-11-08 02:15:45

      Time zone: Asia/Shanghai (CST, +0800)

Network time on: yes

NTP synchronized: yes

RTC in local TZ: no12345678

此时时间已经正常了。

inux修改时间命令是date-s+时分秒。

修改linux系统时间。root使用date指令:date -s

1、只修改日期,不修改时间,输入:date -s 2021-04-02。

2、只修改时间,输入:date -s 12:00:00。

3、同时修改日期时间,注意要加双引号,日期与时间之间有一空格,输入:date -s “2021-04-02 10:25:00”

4、修改完后,记得输入:clock -w。把系统时间写入CMOS修改时区不重启。在root下。

复制相应的时区文件,替换系统时区文件:

cp /usr/share/zoneinfo/主时区/次时区 /etc/localtime

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

5、文件复制命令—cp cp [参数] [源地址] [目的地址]主要参数描述如下:-a :此参数和同时指定“-dpR”参数相同 -d :在复制有符号连接的文件时,保留原始的连接。 -f :强行复制文件,不管要复制的文件是否已经存在于目标目录。 -I :覆盖现有文件之前询问用户。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/tougao/6053135.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-03-13
下一篇2023-03-13

发表评论

登录后才能评论

评论列表(0条)

    保存