配置ntp服务器

  1. 服务端
  2. 客户端
  3. 最后
    1. 生产遇到的问题:
    2. 生产遇到的问题2:

背景 web服务器可以上网 而数据库服务器禁网。但需要同步网络时间,本地又没有ntp服务器可以同步。
思路:web同步网络时间且做ntp服务端

       数据库同步web机器的时间
  • A:192.168.15.173 (ntp服务端)
  • B:192.168.15.174 (ntp客户端)

安装下ntp包 其实默认是有安装的。

yum install -y ntp  #A与B机器都要安装

服务端

1、修改配置vi /etc/ntp.conf
注释掉这个行

restrict default kod nomodify notrap nopeer noquery

解释:上面那行表示:拒绝客户端的所有操作

字段 解释
default 就是指所有的IP
ignore 关闭所有的 NTP 联机服务
nomodify 客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
notrust 客户端除非通过认证,否则该客户端来源将被视为不信任子网
noquery 不提供客户端的时间查询
notrap 不提供trap远端登陆,拒绝为匹配的主机提供模式 6 控制消息陷阱服务。
陷阱服务是 ntpdq 控制消息协议的子系统,用于远程事件日志记录程序

2、添加这两行:

restrict -4 default kod notrap nomodify
restrict -6 default kod notrap nomodify

-4 与 -6 表示 IPV4与IPV6


3、将第一server 修改为阿里的ntp服务器 将其配置为上层ntp

server ntp1.aliyun.com iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
如果你是自己是上层ntp的话,则将上面的 server 注释掉
然后添加:
server 127.127.1.0
fudge 127.127.1.0 stratum 10

4、重启ntp服务

service ntpd restart

5、查看端口 可以看出 ntp使用了123端口 协助为UDP

[root@A机器 ~]# netstat -tuplan | grep 123
udp        0      0 192.168.15.173:123          0.0.0.0:*                               1293/ntpd           
udp        0      0 127.0.0.1:123               0.0.0.0:*                               1293/ntpd           
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               1293/ntpd           
udp        0      0 ::1:123                     :::*                                    1293/ntpd           
udp        0      0 fe80::250:56ff:febe:eb5:123 :::*                                    1293/ntpd           
udp        0      0 :::123                      :::*                                    1293/ntpd           

6、查看ntp到上层ntp服务器状态如何

[root@A机器 ~]# ntpstat 
synchronised to NTP server (120.25.115.20) at stratum 3  #stratum 3 层数在3
   time correct to within 35 ms  #花费的时间
   polling server every 1024 s

客户端

方式一:
1、B机器 可以简单粗暴的使用 ntpdate 命令同步

[root@B机器 ~]# ntpdate 192.168.15.173
11 Sep 13:39:47 ntpdate[30768]: adjust time server 192.168.15.173 offset 0.001383 sec

2、结合cron定时任务来同步(注意要使用root):

0 */1 * * * /usr/sbin/ntpdate  192.168.15.173

方式二:
1、配置 vi /etc/ntp.conf
将第一个server设置为:

server 192.168.15.173

2、重启ntpd服务后 时间过一会儿就会同步

[root@B机器 ~]# date -s "20180302 11:00:00"   #设置个错误的时间
2018年 03月 02日 星期五 11:00:00 CST
[root@B机器 ~]# systemctl  restart ntpd
[root@B机器 ~]# date
2018年 03月 02日 星期五 11:00:43 CST
[root@B机器 ~]# date
2018年 03月 02日 星期五 11:00:49 CST
[root@B机器 ~]# date
2019年 09月 11日 星期三 16:18:55 CST #同步成功
[root@B机器 ~]# date
2019年 09月 11日 星期三 16:18:56 CST

[root@B机器 ~]# systemctl  status ntpd 
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
   Active: active (running) since 五 2018-03-02 11:00:41 CST; 1 years 6 months ago
  Process: 30993 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 30994 (ntpd)
   CGroup: /system.slice/ntpd.service
           └─30994 /usr/sbin/ntpd -u ntp:ntp -g

3月 02 11:00:41 B机器 ntpd[30994]: Listening on routing socket on fd #22 for interface updates
3月 02 11:00:45 B机器 ntpd[30994]: Deferring DNS for 3.centos.pool.ntp.org 1
3月 02 11:00:45 B机器 ntpd[30994]: 0.0.0.0 c016 06 restart
3月 02 11:00:45 B机器 ntpd[30994]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM
3月 02 11:00:45 B机器 ntpd[30994]: 0.0.0.0 c011 01 freq_not_set
3月 02 11:00:45 B机器 ntpd[30996]: signal_no_reset: signal 17 had flags 4000000
3月 02 11:00:47 B机器 ntpd_intres[30996]: DNS 3.centos.pool.ntp.org -> 193.228.143.22
3月 02 11:00:50 B机器 ntpd[30994]: 0.0.0.0 c61c 0c clock_step +48230285.315134 s
9月 11 16:18:55 B机器 ntpd[30994]: 0.0.0.0 c614 04 freq_mode
9月 11 16:18:57 B机器 ntpd[30994]: 0.0.0.0 c618 08 no_sys_peer

最后

设置 ntp服务器开启自启

centos6.x:

chkconfig ntpd on

centos7.x:

[root@nessus ~]# systemctl list-unit-files  | grep ntpd
ntpd.service                                  disabled
[root@nessus ~]# systemctl enable ntpd                 
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
[root@nessus ~]# systemctl list-unit-files  | grep ntpd
ntpd.service                                  enabled 

上述教程完毕

#其他

生产遇到的问题:

[root@ruiweb ~]# ntpdate  ntp1.aliyun.com 
11 Sep 16:29:44 ntpdate[27682]: the NTP socket is in use, exiting
[root@ruiweb ~]# 

说明NTP服务启动了 直接使用ntpdate是不行的。想要手工执行ntpdate,就要关闭ntpd服务

生产遇到的问题2:

网络到阿里的时间服务器是通的 但网络时间同步不了

[root@ruidb ~]# ping ntp1.aliyun.com
PING ntp1.aliyun.com (120.25.115.20) 56(84) bytes of data.
64 bytes from 120.25.115.20: icmp_seq=1 ttl=46 time=35.1 ms
64 bytes from 120.25.115.20: icmp_seq=2 ttl=46 time=35.0 ms
64 bytes from 120.25.115.20: icmp_seq=3 ttl=46 time=34.7 ms
64 bytes from 120.25.115.20: icmp_seq=4 ttl=46 time=34.6 ms
^C
--- ntp1.aliyun.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3324ms
rtt min/avg/max/mdev = 34.698/34.909/35.130/0.230 ms

[root@ruidb ~]# ntpdate  ntp1.aliyun.com 
11 Sep 16:33:09 ntpdate[13663]: no server suitable for synchronization found
[root@ruidb ~]# 

通过 -d 参数来显示过程
-d : 开启调试模式

[root@ruidb ~]# ntpdate  -d ntp1.aliyun.com  
11 Sep 16:36:32 ntpdate[13721]: ntpdate 4.2.6p5@1.2349-o Wed Dec 19 20:22:35 UTC 2018 (1)
Looking for host ntp1.aliyun.com and service ntp
host found : 120.25.115.20
transmit(120.25.115.20)
transmit(120.25.115.20)
transmit(120.25.115.20)
transmit(120.25.115.20)
transmit(120.25.115.20)
120.25.115.20: Server dropped: no data
server 120.25.115.20, port 123
stratum 0, precision 0, leap 00, trust 000
refid [120.25.115.20], delay 0.00000, dispersion 64.00000
transmitted 4, in filter 4
reference time:    00000000.00000000  Mon, Jan  1 1900  8:05:43.000
originate timestamp: 00000000.00000000  Mon, Jan  1 1900  8:05:43.000
transmit timestamp:  e1233093.43204c07  Wed, Sep 11 2019 16:36:35.262
filter delay:  0.00000  0.00000  0.00000  0.00000 
         0.00000  0.00000  0.00000  0.00000 
filter offset: 0.000000 0.000000 0.000000 0.000000
         0.000000 0.000000 0.000000 0.000000
delay 0.00000, dispersion 64.00000
offset 0.000000

11 Sep 16:36:36 ntpdate[13721]: no server suitable for synchronization found
[root@ruidb ~]# 

###后来发现是内部网络中限制123端口的数据包

这个是错误的请求过程

transmit(120.25.115.20)
transmit(120.25.115.20)
transmit(120.25.115.20)
transmit(120.25.115.20)
transmit(120.25.115.20)

这个是正确的请求过程

transmit(120.25.115.20)
receive(120.25.115.20)
transmit(120.25.115.20)
receive(120.25.115.20)
transmit(120.25.115.20)
receive(120.25.115.20)
transmit(120.25.115.20)
receive(120.25.115.20)

transmit:发射
receive:接收


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。

文章标题:配置ntp服务器

本文作者:wangzhirui

发布时间:2019-09-12, 12:00:53

最后更新:2025-02-27, 02:04:02

原始链接:https://wangzhirui.com/2019/09/12/配置ntp服务器/

转载请保留原文链接及作者。

目录