CentOS 7.2 搭建内网ntp时间服务器

时间服务器说明

前面在系统的基础优化里说到了时间同步需要同步aliyun的时间,这样我们所有的服务器都需要到公网去同步时间,浪费很多网络资源,这里我们来搭建一下内网的时间服务器。

环境说明

1、时间服务器环境介绍

# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)

# uname -r
3.10.0-327.el7.x86_64

# hostname -I
192.168.56.100 172.16.1.100

2、内网需要同步时间的服务器环境介绍

# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core) 

[root@linux-node2 ~]# uname -r
3.10.0-327.el7.x86_64

[root@linux-node2 ~]# hostname -I
192.168.56.12 172.16.1.12

yum安装ntp时间服务

# yum install -y ntp
# rpm -qa ntp        #下载后验证
ntp-4.2.6p5-25.el7.centos.2.x86_64

修改配置文件

[root@web01 ~]# grep -n "^[a-z]" /etc/ntp.conf    #过滤配置文件
4:driftfile /var/lib/ntp/drift       #默认的
9:restrict default nomodify      #注释掉地8行,添加这行,表示nomodify客户端可以同步时间。
14:restrict 127.0.0.1          #默认的
15:restrict ::1                     #默认的
26:server ntp1.aliyun.com       #添加时间源
27:server time.nist.gov            #添加时间源
39:includefile /etc/ntp/crypto/pw      #以下都是默认的
43:keys /etc/ntp/keys
61:disable monitor

启动时间服务器

# systemctl start ntpd
# netstat -lnuto|grep 123           #查看端口
udp        0      0 172.16.1.100:123        0.0.0.0:*                           off (0.00/0/0)
udp        0      0 192.168.56.100:123      0.0.0.0:*                           off (0.00/0/0)
udp        0      0 127.0.0.1:123           0.0.0.0:*                           off (0.00/0/0)
udp        0      0 0.0.0.0:123             0.0.0.0:*                           off (0.00/0/0)
udp6       0      0 fe80::20c:29ff:fe31:123 :::*                                off (0.00/0/0)
udp6       0      0 fe80::20c:29ff:fe31:123 :::*                                off (0.00/0/0)
udp6       0      0 ::1:123                 :::*                                off (0.00/0/0)
udp6       0      0 :::123                  :::*                                off (0.00/0/0)

启动后确认:

# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 time5.aliyun.co 10.137.38.86     2 u   11   64    1    5.414   -0.946   0.000
 time-c-wwv.nist .STEP.          16 u    -   64    0    0.000    0.000   0.000
发现有两个可用的时间源

稍等几分钟后,在客户端同步时间

客户端如果有同步时间的定时任务需要取消掉。

1、到客户端查看当前时间

# date
Fri Jan 12 21:06:00 CST 2018

2、与时间服务器同步时间

# ntpdate -d 192.168.56.100
12 Jan 22:12:00 ntpdate[6677]: ntpdate 4.2.6p5@1.2349-o Wed Apr 12 21:24:06 UTC 2017 (1)
Looking for host 192.168.56.100 and service ntp
host found : linxu-node4
transmit(192.168.56.100)
receive(192.168.56.100)
transmit(192.168.56.100)
receive(192.168.56.100)
transmit(192.168.56.100)
receive(192.168.56.100)
transmit(192.168.56.100)
receive(192.168.56.100)
server 192.168.56.100, port 123
stratum 3, precision -24, leap 00, trust 000
refid [192.168.56.100], delay 0.02617, dispersion 0.00076
transmitted 4, in filter 4
reference time:    de02c1e5.530e0c26  Fri, Jan 12 2018 13:11:01.324
originate timestamp: de02c227.63d8db0f  Fri, Jan 12 2018 13:12:07.390
transmit timestamp:  de0340b6.c05f3381  Fri, Jan 12 2018 22:12:06.751
filter delay:  0.02617  0.02634  0.02634  0.02635 
         0.00000  0.00000  0.00000  0.00000 
filter offset: -32399.3 -32399.3 -32399.3 -32399.3
         0.000000 0.000000 0.000000 0.000000
delay 0.02617, dispersion 0.00076
offset -32399.360102

12 Jan 22:12:06 ntpdate[6677]: step time server 192.168.56.100 offset -32399.360102 sec

3、此时再查看客户端的时间

# date
Fri Jan 12 22:13:01 CST 2018

4、设置定时任务同步时间即可

# crontab -l
# tine rsync   time:2018/1/12
*/5 * * * * ntpdate 192.168.56.100 >/dev/null 2>&1

至此,内网的时间服务器就安装完成了,所有的内网服务器都可以到时间服务器来同步时间,不需要到公网上去同步时间了。

标签:Centos 发布于:2019-10-30 02:05:22