目录
1.安装ntpd服务
[root@giantfox ~]# yum install -y ntp ntpdate
Centos7版本可能自带ntp 如果没有就手动下载一下
2.修改 /etc/ntp.conf 的配置
[root@giantfox ~]# vim /etc/ntp.conf
[root@giantfox ~]# cat /etc/ntp.conf
driftfile /var/lib/ntp/drift #会把时间误差记录下来
logfile /var/log/ntpd.log #日志记录
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap #控制允许那些网络查询和同步时间
#server 0.centos.pool.ntp.org iburst //这四行server注释掉 增加本机的server
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server 192.168.1.6 iburst
fudge 127.0.0.1 stratum 10
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
3.启动并开机自启
[root@giantfox ~]# systemctl restart ntpd ; systemctl enable ntpd
4.检查ntpd是否开启成功
[root@giantfox ~]# systemctl status ntpd
● ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2022-11-05 02:46:32 UTC; 10min ago
Process: 9401 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 9402 (ntpd)
CGroup: /system.slice/ntpd.service
└─9402 /usr/sbin/ntpd -u ntp:ntp -g
Nov 05 02:46:32 giantfox.novalocal systemd[1]: Starting Network Time Service...
Nov 05 02:46:32 giantfox.novalocal ntpd[9402]: proto: precision = 0.085 usec
Nov 05 02:46:32 giantfox.novalocal ntpd[9402]: 0.0.0.0 c01d 0d kern kernel time sync enabled
Nov 05 02:46:32 giantfox.novalocal systemd[1]: Started Network Time Service.
状态正常
查看UDP123端口是否启动成功
[root@giantfox ~]# netstat -ntplu |grep ntp
udp 0 0 192.168.1.6:123 0.0.0.0:* 9402/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 9402/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 9402/ntpd
udp6 0 0 fe80::f816:3eff:fe7:123 :::* 9402/ntpd
udp6 0 0 ::1:123 :::* 9402/ntpd
udp6 0 0 :::123 :::* 9402/ntpd
5.将当前时间和日期写入BIOS
[root@giantfox ~]# echo "SYNC_HWCLOCK=yes" >> /etc/sysconfig/ntpd
6.检查同步信息
[root@giantfox ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
ns01.linux.com .INIT. 16 u - 512 0 0.000 0.000 0.000
7.设置定时任务进行时间校对
需安装ntpdate,每天24点更新同步时间
[root@giantfox ~]# crontab -e
0 0 * * * /usr/sbin/sntp -P no -r 192.168.1.6; hwclock -w