一、简介
NTP:网络校时(Network Time Protocol),多台客户端主机可以同步服务端的时间,从而与服务端的时间保持一致。
二、原理
1. 主机当然需要启动 daemon ;
2. Client 会向 NTP Server 发送出调校时间的 message ;
3. NTP Server 会送出目前的标准时间给 Client ;
4. Client 接收了来自 Server 的时间后,会据以调整自己的时间。
三、安装
1.安装ntp服务
RedHat系列:Redhat、Centos、Fedora 等;中标麒麟也是这个系列的;
Debian系列:Debian、Ubuntu 等;uos,银河麒麟等则是依附这个系列的。
RedHat系列安装:
#yum install ntp
#yum install ntpdate
Debian系列安装:
#sudo atp-get install ntp
#sudo atp-get install ntpdate
2.查看安装信息:
rpm -qa | grep ntp
四、配置ntp
1.服务端配置
sudo vim /etc/ntp.conf
添加如下内容:
restrict 192.168.6.3 nomodify notrap nopeer noquery //IP地址为上层IP
restrict 192.168.6.0 mask 255.255.255.0 nomodify notrap //集群所在网段的网关(允许192.168.6段机器同步时间),子网掩码(Genmask)
注释掉server 0 ~ n
在server部分添加
restrict 207.46.232.182 mask 255.255.255.255 nomodify notrap noquery
server 207.46.232.182 //网络时间
server 127.127.1.0 prefer //本地时钟 以这部主机为最优先的server
Fudge 127.127.1.0 stratum 10
2.客户端配置
注释掉server 0 ~ n
server 192.168.6.3 //IP地址为服务端IP
3.服务端设置开机启动
chkconfig ntpd on(旧指令)
systemctl enable ntpd(新指令)
systemctl is-enable ntpd enabled(查看是否开机自启动)
4.关闭服务端开机启动
chkconfig ntpd off(旧指令)
systemctl disable ntpd(新指令)
五、启动ntp服务、查看状态
1.修改客户端配置
sudo vim /etc/ntp.conf
server 192.168.12.13 //IP地址为服务端IP
2.启动ntp服务
service ntpd start
systemctl start ntpd
3.关闭ntp服务
service ntpd stop
systemctl stop ntpd
4.重启ntp服务
service ntpd restart
systemctl restart ntpd
5.查看ntp服务器有无和上层ntp连通
ntpstat
6.查看ntp服务器与上层ntp的状态
ntpq -p
7.查看当前启动状态
service ntpd
status
8.客户端主动同步服务端时间
ntpdate 172.168.12.13 //IP地址为服务端IP
9.客户端主动同步服务端时间
sudo vim /etc/crontab
*/5 * * * * root ntpdate 192.168.12.13 //每隔5分钟执行任务
00 23 * * * root ntpdate 192.168.12.13 //每天晚上23点
00 17-19 * * * root ntpdate 192.168.12.13 //每天晚上17 18 19执行任务
30 17,18,19 * * * root ntpdate 192.168.12.13 //每天晚上17:30,18:30,19:30执行任务
service crond restart
六、重启ntp服务(客户端和服务端)
systemctl stop firewalld
setenforce 0
sudo vim /etc/selinux/config
SELINUX=disabled
systemctl restart ntpd
希望可以帮到你~
参考博客: