Centos7服务端搭建ntp
- 使用原因
使用NTP的目的是对网络内所有具有时钟的设备进行时钟同步,使网络内所有设备的时钟保持一致,从而使设备能够提供基于统一时间的多种应用。对于运行NTP的本地系统,既可以接收来自其他时钟源的同步,又可以作为时钟源同步其他的时钟,并且可以和其他设备互相同步。
- 下载ntp rpm依赖包
http://www.rpmfind.net/linux/rpm2html/search.php?query=fontpackages&submit=Search+...&system=&arch=
- 安装ntp服务
rpm -ivh autogen-libopts-5.18-5.el7.x86_64.rpm
rpm -ivh glibc-2.17-317.el7.x86_64.rpm glibc-common-2.17-317.el7.x86_64.rpm
rpm -ivh openssl-libs-1.0.2k-19.el7.x86_64.rpm --force
rpm -ivh ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm --force
rpm -ivh ntp-4.2.6p5-29.el7.centos.2.x86_64.rpm
- 首先我们要清楚,NTP服务器默认是会使用udp的123端口的,所以我们的第一步就是开放123端口,命令如下:
firewall-cmd --permanent --zone=public --add-port=123/udp
//增加端口后当然要重新加载防火墙,让配置生效了
firewall-cmd --state
- Centos7默认通过chronyd服务实现时钟同步,我们需要关闭chronyd服务并使其开机不自启,同时启动ntpd并将其加入开机自启:
systemctl stop chronyd
systemctl disable chronyd
systemctl enable ntpd
systemctl start ntpd
- 修改ntp配置文件
vi /etc/ntp.conf
//1 把下边这行注释掉
// restrict default nomodify notrap nopeer noquery
//2 删除掉原有的4行server,增加下边的两行,127.127.1.0代表把本机作为时间服务器
server 127.127.1.0 #新增上级时间服务器为本机
fudge 127.127.1.0 stratum 10 #设置时间服务器的层次
服务端完整配置文件
vi /etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
server 127.127.1.0
fudge 127.127.1.0 stratum 10
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
- 重启
systemctl restart ntpd
- ntp服务端搭建完成,客户端同步服务端时间命令
ntpdate 服务端
Centos7客户端搭建ntp
前面几步安装步骤是相同的
- 修改ntp配置文件
vim /etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
server 10.148.151.0
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
- 启动服务
systemctl restart ntpd
- 同步时间命令
ntpdate 10.148.151.0
- 如果报ntpdate[20136]: the NTP socket is in use, exiting
原因:由于 xntpd 已经绑定到了该 Socket。运行 ntpdate 时,它会首先进行广播,然后侦听端口 123。如果 xntpd 正在运行,而有一个进程已经在侦听该端口了,则会使 ntpdate 无法在上面运行(注意服务端不能停止该进程,服务端如果停止该进程,客户端会报:no server suitable for synchronization found)
解决:kill改进程
#查询被占用的端口的pid
lsof -i:123
kill -9 pid
-
再次执行同步时间命令,成功
-
客户端添加定时同步任务
vim /etc/crontab
#添加如下定时任务,每分钟执行一次
* * * * * root /usr/sbin/ntpdate 10.148.151.0
评论区