说明:
NTP ( Network Time Protocol ) 服务的作用是在计算机网络中同步各个设备的时间,确保网络中所有设备的时间保持一致和准确。通过与参考时间源 ( NTP 服务器 ) 同步,可以避免不同设备之间出现时间差,确保网络中的事件顺序正确,同时避免一些服务因为时间不同步导致报错。
查看系统是否安装了ntp服务,一般默认安装ntpdate;
[root@CS01 ~]# rpm -qa | grep ntp
ntpdate-4.2.6p5-29.el7.centos.2.x86_64
ntp-4.2.6p5-29.el7.centos.2.x86_64
安装ntp服务
yum install ntp ntpdate -y
修改配置文件,同时过滤掉一些无用的信息
vim /etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery ## restrict限制操作,默认放开客户端所有操作
restrict 127.0.0.1 ## 允许本机所有操作
restrict ::1
restrict 172.22.19.0 mask 255.255.255.0 nomodify notrap ## 限制172.22.19.0/24网段内的服务器不能修改
restrict 172.22.22.180 mask 255.255.255.0 nomodify notrap ## 限制172.22.22.180这台服务器不能修改
logfile /var/log/ntpd.log ## 默认日志路径,可根据需求修改
# server 0.centos.pool.ntp.org iburst #####################################
# server 1.centos.pool.ntp.org iburst ## 这些系统自带的ntp服务器地址 ##
# server 2.centos.pool.ntp.org iburst ## 可以注销掉 ##
# server 3.centos.pool.ntp.org iburst #####################################
server 127.127.1.1 ## 配置本机为ntp服务
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
启动ntp服务并加入开机启动;
systemctl start ntpd
systemctl enable ntpd
systemctl list-unit-files --type=service | grep ntpd
ss -nulp | grep ntpd ## 因为是UDP协议所以这里需要-u参数
[root@CS01 ~]# ntpq -p ## 查看ntp状态
remote refid st t when poll reach delay offset jitter
==============================================================================
*LOCAL(1) .LOCL. 5 l 29 64 377 0.000 0.000 0.000
ntp服务端如何同步外网的时间
/usr/sbin/ntpdate -u ntp1.aliyun.com 9000 ## 因为ntpd将123端口占用,所以我们则可以只从端口去同步外网ntp服务器
客户端同步时间
ntpdate CS01
或者crontab -e加入
0 */2 * * * /usr/sbin/ntpdate CS01;/sbin/hwclock -w >/dev/null 2>&1