NTP解读-Centos实操记录

检查是否安装ntp服务:rpm -qa | grep ntp
安装yum install ntp ntpdate -y
firewall-cmd –permanent –add-port=123/udp
firewall-cmd –reload
启动ntp服务systemctl start ntpd
设置自启动:systemctl enable ntpd
重新启动ntp服务:systemctl restart ntpd
检查ntp服务启动状态:systemctl status ntpd
服务端和客户端都需要开启ntp服务

查看chrony是否被设置为开机自启动。通过指令 systemctl is-enabled chronyd 查看,发现这个服务已经被设置为开机自启动所以导致NTP服务的自启动失败。
1.需要禁掉chronyd.service:
systemctl disable chronyd
2.手动启动ntpd:
systemctl start ntpd
3.再次设置开机自启动:
systemctl enable ntpd
重启服务器测试ntpd即可正常自启动了

服务端vi /etc/ntp.conf
#允许该IP段该掩码下IP可以同步本服务器,后面啥也不加,表示开放所有权限,默认加了的可以不改
restrict 192.168.2.0 mask 255.255.255.0
#注释掉原有外网的server
#pool 0.ubuntu.pool.ntp.org iburst
#pool 1.ubuntu.pool.ntp.org iburst
#pool 2.ubuntu.pool.ntp.org iburst
#pool 3.ubuntu.pool.ntp.org iburst
#pool ntp.ubuntu.com
#新增修改本机为服务器,127.127.1.0代表本机,stratum代表层级,设置服务器的初始层级为5
#这里本机ip用127.0.0.1似乎效果不好,可以都试试
#此处IP为优先对时对方服务器的IP,如使用了外网域名可不设该条信息
server 192.168.4.109 prefer
server 127.127.1.0
fudge 127.127.1.0 stratum 5
#禁用认证功能,不知道有用没有,放不放都行,可以不改
disable auth

配置好之后,重启ntp服务

客户端vi /etc/ntp.conf

#指定服务器的固定ip,并设为优先级prefer;如果找不到服务器,再用本地时间,本地时间的层级是8
server 192.168.2.100 prefer
server 127.127.1.0
fudge 127.127.1.0 stratum 8

配置好之后,重启ntp服务

测试
ntpdate -u 192.168.111.130
返回19 May 18:38:02 ntpdate[1525]: adjust time server 192.168.111.130 offset 0.004885 sec

crontab -e 每晚凌晨1点执行一次,hwclock代表硬件时钟可加可不加
00 01 * * * root /usr/sbin/ntpdate -u 192.168.137.3; /sbin/hwclock -w
0 */1 * * *为每隔一小时执行一次

service crond restart //重启服务

查看日志
cat /var/spool/mail/root 看对时是否成功
cat /var/log/cron 看定时任务是否执行

如果重启cron出错:Redirecting to /bin/systemctl start crond.service

使用systemctl执行即可

systemctl start crond.service //启动服务
systemctl stop crond.service //关闭服务
systemctl restart crond.service //重启服务
systemctl reload crond.service //重新载入配置
systemctl status crond.service //查看状态

//或者
crond start
crond stop
crond restart
crond reload
crond status
————————————————
如果出现crond: can’t lock /var/run/crond.pid

直接把lock删掉就行了

rm -rf /var/run/crond.pid