
-
搭建环境
Centos7.x
ntpdate-4.2.6p5-29.el7.centos.2.x86_64 #客户端
ntp-4.2.6p5-29.el7.centos.2.x86_64 #服务端
-
NTP 服务搭建
查询 ntp 服务是否安装
rpm -qa | grep ntp如果未安装,需要先安装 ntp
yum install ntp -y -
配置 NTP
只需要修改部分配置
vim /etc/ntp.conf #默认Centos提供了4个时间同步服务器,以下之使用一个时间服务器 #server 0.centos.pool.ntp.org iburst #server 1.centos.pool.ntp.org iburst #server 2.centos.pool.ntp.org iburst #server 3.centos.pool.ntp.org iburst server 0.cn.pool.ntp.org iburst #server 0.cn.pool.ntp.org iburst 国际通用 server 127.127.1.0 #如果国际通用时间服务器不能同步,则自动会按照本级时间进行同步 fudge 127.127.1.0 stratum 10 #指定127.127.1.0 为第10层。ntp和127.127.1.0同步完后,就变成了11层。ntp是层次阶级的 logfile /var/log/ntp.log #配置日志目录 -
NTP 服务自启
使用 server 进行启动 ntp
[root@localhost ~]# service ntpd start #启动ntp服务 [root@localhost ~]# chkconfig ntpd on #设置为开机自启 [root@localhost ~]# netstat -anp | grep ntpd udp 0 0 10.10.80.202:123 0.0.0.0:* 6012/ntpd udp 0 0 127.0.0.1:123 0.0.0.0:* 6012/ntpd udp 0 0 0.0.0.0:123 0.0.0.0:* 6012/ntpd udp6 0 0 fe80::12e3:8d92:d08:123 :::* 6012/ntpd udp6 0 0 ::1:123 :::* 6012/ntpd udp6 0 0 :::123 :::* 6012/ntpd unix 2 [ ] DGRAM 527750 6012/ntpd -
设置防火情开放端口
-
如果使用 iptables 的用户使用如下
iptables -I INPUT -p udp --dport 123 -j ACCEPT systemctl restart iptables.service -
如果使用 firewall 的用户使用如下
firewall-cmd --permanent --add-service=ntp firewall-cmd --reload
-
-
查看同步状态
[root@localhost ~]# ntpstat synchronised to NTP server (144.76.76.107) at stratum 3 time correct to within 172 ms polling server every 64 s -
客户端同步
-
使用 netnate 进行同步
[root@localhost ~]# date -s "2021-10-30 19:27:16" #随便设置一个日期 [root@localhost ~]# date #查看本机目前时间 2021年 10月 30日 星期六 19:28:06 CST #需要先关闭ntpd服务不然有可能会出现同步出错的问题 [root@localhost ~]# service ntpd stop Redirecting to /bin/systemctl stop ntpd.service #进行同步 NTP服务器端IP [root@localhost ~]# ntpdate ip 16 Sep 14:58:00 ntpdate[14880]: step time server ipoffset 27718128.296978 sec #再次查看本机时间 [root@localhost ~]# date 2022年 09月 16日 星期五 14:58:02 CST #启动ntpd服务 [root@localhost ~]# service ntpd start Redirecting to /bin/systemctl start ntpd.service -
可以设置一个定时任务进行同步
[root@localhost ~]# crontab -e 0 12 * * * /usr/sbin/ntpdate 192.168.11.23
-
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于