1)服务端部署
安装所需软件包
[root@test ~]# yum -y install ntp ntpdate
服务端自己先手工同步一次时间。
[root@test ~]# ntpdate ntp.sjtu.edu.cn
21 Jun 16:48:54 ntpdate[10781]: the NTP socket is in use, exiting
或者使用阿里云的时间服务器进行在线同步
[root@test ~]# ntpdate ntp2.aliyun.com
[root@test ~]# ntpdate ntp1.aliyun.com
编辑NTP主配置文件,添加NTP服务器(对于大陆地区的服务器)并同步BIOS时间。
对于位于中国大陆地区IDC机房的服务器,由于CentOS/RHEL默认的ntp服务器无法访问,因此要设置中国大陆地区自己的ntp服务器;
对于位于中国大陆地区以外的IDC机房的服务器,可以使用CentOS/RHEL默认的ntp服务器(但一定要检查确保可以访问);
修改如下:
[root@test ~]# cp /etc/ntp.conf /etc/ntp.conf.bak
[root@test ~]# vim /etc/ntp.conf
restrict default nomodify notrap noquery
restrict 127.0.0.1
restrict 192.168.0.0 mask 255.255.0.0 nomodify #只允许192.168.0.0网段的客户机进行时间同步。如果允许任何IP的客户机都可以进行时间同步,就修改为"restrict default nomodify"
server ntp1.aliyun.com
server ntp2.aliyun.com
server time1.aliyun.com
server time2.aliyun.com
server time-a.nist.gov
server time-b.nist.gov
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
driftfile /var/lib/ntp/drift
broadcastdelay 0.008
keys /etc/ntp/keys
[root@test ~]# service ntpd start
Starting ntpd: [ OK ]
[root@test ~]# netstat -tulnp | grep ntp
udp 0 0 182.48.115.233:123 0.0.0.0:* 10023/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 10023/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 10023/ntpd
udp 0 0 fe80::5054:ff:feab:db19:123 :::* 10023/ntpd
udp 0 0 ::1:123 :::* 10023/ntpd
udp 0 0 :::123 :::* 10023/ntpd
设置开机自启动
[root@test ~]# /sbin/chkconfig --level=2345 ntpd on
查看现有连接客户端
[root@test ~]# watch ntpq -p
服务端可以设置定期在线同步一次时间
[root@test ~]# crontab -l
#服务器时间同步
0 * * * * /usr/sbin/ntpdate ntp1.aliyun.com; /sbin/hwclock -w
2)客户端配置
命令行手动执行一次验证是否可用:
[root@test2 ~]# ntpdate 182.148.15.33 #182.148.15.33是上面ntp服务器的ip地址
21 Jun 16:45:35 ntpdate[2323]: adjust time server 182.148.15.33 offset 0.319548 sec
配置自动同步
[root@test2 ~]# crontab -l
#服务器时间同步
*/5 * * * * /usr/sbin/ntpdate 182.148.15.33; /sbin/hwclock -w