Linux 时区、NTP设置

醉酒当歌 提交于 2020-01-06 23:25:32

CentOS 时区及NTP配置

时区 配置

1.查看当前时区

[root@localhost ~]# ll /etc/localtime 
lrwxrwxrwx. 1 root root 35 Dec 23 22:58 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai

2.修改时区

[root@localhost ~]# timedatectl list-timezones |grep Shanghai
Asia/Shanghai

[root@localhost ~]# timedatectl set-timezone Asia/Shanghai


软链接方式修改
ln -sf  /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

NTP 配置

同步时间,可以使用ntpdate命令,也可以使用ntpd服务。
ntpdate命令需要配合计划任务来执行,定期强制同步时间;如果使用ntpd是创建了ntp的服务进程 ,该进程既可以作为ntp的客户端,也可以作为ntp的服务端,因此建议使用ntpd服务。

1.安装ntpd服务

[root@localhost ~]# yum -y install ntp
Loaded plugins: fastestmirror
base                                                                                                                                                                         | 3.6 kB  00:00:00     
epel/x86_64/metalink                                                                                                                                                         | 9.4 kB  00:00:00     
epel                                                                                                                                                                         | 5.3 kB  00:00:00     
extras                                                                                                                                                                       | 2.9 kB  00:00:00     
updates                                                                                                                                                                      | 2.9 kB  00:00:00     
(1/2): epel/x86_64/updateinfo                  

2.开启ntpd服务,设置开机自动运行

[root@localhost ~]# systemctl start ntpd

[root@localhost ~]# systemctl enable ntpd

[root@localhost ~]# netstat -an | grep 123
udp        0      0 10.23.0.114:123         0.0.0.0:*                          
udp        0      0 127.0.0.1:123           0.0.0.0:*                          
udp        0      0 0.0.0.0:123             0.0.0.0:*                          
udp6       0      0 fe80::250:56ff:fe88:123 :::*                               
udp6       0      0 ::1:123                 :::*                               
udp6       0      0 :::123                  :::*                      

3.配置ntp时间同步源,使用阿里的时间服务区 ntp1.aliyun.com 作为时间同步源。

ntpd的配置文件为/etc/ntp.conf

[root@localhost ~]# vi /etc/ntp.conf 
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

driftfile /var/lib/ntp/drift

# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default nomodify notrap nopeer noquery

# Permit all access over the loopback interface.  This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict ::1

# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 10.23.255.250 prefer

4.配置/etc/sysconfig/ntpd文件

ntp服务默认只会同步系统时间,如果想同步服务器硬件时间可以配置
SYNC_HWCLOCK=yes
或者命令hwclock -w

[root@localhost sysconfig]# vi /etc/sysconfig/ntpd
# Command line options for ntpd
OPTIONS="-g"
SYNC_HWCLOCK=yes
~
~

[root@localhost sysconfig]# hwclock -w

5.配置/etc/ntp/stpe-tickers文件

step-tickers使用ntpdate,其中ntp.conf是ntpd守护程序的配置文件。 最初运行ntpdate来设置ntpd之前的时钟,以确保时间在1000秒以内。 如果服务器与客户端之间的时间差超过1000秒(或大约1000秒),则ntp将不会运行。

6.查看NTP状态

ntpstat
ntpq -p


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!