Linux服务器校对时间

发布时间 2023-07-13 10:37:07作者: 06

一、查看时间命令

date

二、使用ntpdate命令校对时间
1.安装NTP校时包
Debian系统安装NTP校时包:
apt-get install ntpdate
1
CentOS系统安装NTP校时包:
yum install ntp
1
1.2 校时命令:
ntpdate cn.pool.ntp.org

三、设置NTP开机自启
设置服务开机自启
# systemctl enable ntpd
查看是否开机自启
# systemctl is-enabled ntpd enabled

 

四、ntp配置文件设置

6、服务端配置文件设置(/etc/ntp.conf)

#ipv4 ipv6
restrict -4 default kod notrap nomodify
restrict -6 default kod notrap nomodify

#允许192.168.110段设备同步时间
restrict 192.168.110.0 mask 255.255.255.0 nomodify #notrust 

//添加修改上层时间服务器,如果能联通外网,则不需要修改,
server 210.72.145.44 #这是中国国家授时中心的IP
server 0.rhel.pool.ntp.org iburst
server 1.rhel.pool.ntp.org iburst
server 2.rhel.pool.ntp.org iburst
server 3.rhel.pool.ntp.org iburst

//如果联不通外网,则把上面5行注释,然后添加下面两行,意思是设置成时间服务器是自己
server 127.127.1.0 fudge
127.127.1.0 stratum 8

 


7、客户端配置文件设置(/etc/ntp.conf)

#如果本地时间和目标服务器差别太大,ntpd不会进行时间同步,并会退出ntpd程序,以下表示无论本机时间与服务端时间源时间偏移多大都进行时间同步
tinker panic 0

#目标服务器ip
server 192.168.110.159

 

8、重启服务端和客户端ntpd
systemctl stop ntpd

systemctl start ntpd

9、查看ntp服务同步状况
重启后需要等5~10分钟才会时间同步

ntpq -p

10.使用ntpdate当作客户端
NOTE:ntpdate不可与ntpd一起使用

ntpdate 192.168.110.159

 

 

五、时区

3、修改时区

时区是具有相同标准时间的地理区域。在操作系统安装过程中一般会让选择设置时区,但是后续也能对时区进行修改。下面介绍一下查看、修改时区的方法。

3.1 检查当前时区

检查当前时区用到的命令其实在上文中已经提到过,为timedatectl,当该命令不带任何参数时可以查看时区和时间,如下显示的时区为Asia/Shanghai

[root@hadoopmaster ~]# timedatectl       
Local time: Sun 2022-04-17 16:06:55 CST   
Universal time: Sun 2022-04-17 08:06:55 UTC         
RTC time: Sun 2022-04-17 08:06:55        
Time zone: Asia/Shanghai (CST, +0800)      
NTP enabled: yes 
NTP synchronized: yes  
RTC in local TZ: no       
DST active: n/a

其实还可以通过查看/etc/localtime被链接到哪个文件进行确定当前时区属于哪个时区,如下时区信息为Asia/Shanghai

[root@hadoopmaster ~]# ll /etc/localtime 
lrwxrwxrwx 1 root root 33 Apr 16 17:12 /etc/localtime -> /usr/share/zoneinfo/Asia/Shanghai

3.2 更改时区

时区的命名约定格式为“地区/城市名”方式,可以通过timedatectl list-timezone命令查询系统中所有可用的时区,下面展示的只是一部分

[root@bogon ~]# timedatectl list-timezones 
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Bamako
Africa/Bangui
Africa/Banjul

或者通过查看/usr/share/zoneinfo/下的文件来确认有哪些可用的时区

[root@bogon ~]# ll /usr/share/zoneinfo/
total 392
drwxr-xr-x.  2 root root   4096 Mar  9  2020 Africa
drwxr-xr-x.  6 root root   8192 Mar  9  2020 America
drwxr-xr-x.  2 root root    187 Mar  9  2020 Antarctica
drwxr-xr-x.  2 root root     26 Mar  9  2020 Arctic
drwxr-xr-x.  2 root root   4096 Mar  9  2020 Asia
drwxr-xr-x.  2 root root    196 Mar  9  2020 Atlantic
drwxr-xr-x.  2 root root   4096 Mar  9  2020 Australia
drwxr-xr-x.  2 root root     59 Mar  9  2020 Brazil
drwxr-xr-x.  2 root root    136 Mar  9  2020 Canada
-rw-r--r--.  1 root root   2102 Jan 30  2018 CET
drwxr-xr-x.  2 root root     45 Mar  9  2020 Chile

更改时区有两种方式,一种是使用timedatectl命令(如果系统中有该命令的话),另一种是为时区文件创建符合链接。
方式一:使用timedatactl命令
确定完要修改的时区后,可以使用如下命令进行修改,下面的命令将时区改为America/New_York时区

[root@hadoopmaster ~]# timedatectl set-timezone America/New_York
 You have mail in /var/spool/mail/root

改完后可以查看验证一下是否更改成功,

[root@hadoopmaster ~]# timedatectl
      Local time: Sun 2022-04-17 05:10:49 EDT
  Universal time: Sun 2022-04-17 09:10:49 UTC
        RTC time: Sun 2022-04-17 09:10:49
       Time zone: America/New_York (EDT, -0400)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: yes
 Last DST change: DST began at
                  Sun 2022-03-13 01:59:59 EST
                  Sun 2022-03-13 03:00:00 EDT
 Next DST change: DST ends (the clock jumps one hour backwards) at
                  Sun 2022-11-06 01:59:59 EDT
                  Sun 2022-11-06 01:00:00 EST

显然已经更改成功。
方式二:通过创建符号链接来更改时区
在没有timedatectl命令的系统中,可以通过将/usr/share/zoneinfo下的不同时区文件链接到/etc/localtime的方式来更改时区,一般而言系统中已经有时区文件链接到/etc/localtime,我们可以将该文件先备份:cp /etc/localtime /etc/localtime.20220417bak01,然后为新的时区文件创建符号链接,注意如果不加-f可能会报“failed to create symbolic link ‘xxx’: File exists”的错误,

ln -s /usr/share/zoneinfo/America/New_York /etc/localtime

 

参考文档:

https://blog.csdn.net/weixin_56175092/article/details/129388065

https://blog.csdn.net/thunderLZM/article/details/125996390

https://zhuanlan.zhihu.com/p/500102049