Centos6.9 搭建NTP时间同步服务

目录

1 前言

在内部局域网不能联网进行时间同步的时候我们就需要搭建NTP时间服务器使局域网的机器时间同步。此时局域网的机器只有一个或多个内网网卡而没有外部网卡,而搭建NTP服务的机器至少有一个外部网卡和一个与局域网同网段的内网网卡,外网网卡用于本机时间同步,内网网卡用于连通局域网。下边就可以搭建一下NTP服务

环境:

NTP(Server)
    hostname: m01
    eth0: 10.0.0.61
    eth1: 172.16.1.61
NTP(Client)
    eth0: 172.16.1.31
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2 Server端的安装及配置

2.1 安装ntp服务组件

yum install ntp -y
rpm -qa ntp
  • 1
  • 2
  • 3

2.2 配置文件详解

# 默认配置文件
[root@m01 ~]# egrep -v "^$|#" /etc/ntp.conf  # 过滤掉注释行
driftfile /var/lib/ntp/drift
restrict default kod nomodify notrap nopeer noquery  # 对默认的客户端拒绝所有操作
restrict -6 default kod nomodify notrap nopeer noquery # 针对ipv6地址的权限
restrict 127.0.0.1     # 允许本地地址的一切操作
restrict -6 ::1        # 允许ipv6地址的一切操作
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
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

2.3 restrict介绍

权限的设定主要是由restrict的参数决定的。 
restrict语法

restrict IP地址 mask 子网掩码 参数
  • 1
  • 2

其中 IP 可以是IP地址,也可以是 default ,default 就是指所有的IP 
参数有以下几个: 
ignore:关闭所有的 NTP 联机服务 
nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。 
notrust:客户端除非通过认证,否则该客户端来源将被视为不信任子网 
noquery:不提供客户端的时间查询 
注意:如果参数没有设定,那就表示该 IP (或子网)没有任何限制!

设定局域网内部网段所有客户端可以连接到本地进行时间同步,但是拒绝他们修改服务器上的时间

restrict 172.16.1.0 mask 255.255.255.0 nomodify
  • 1
  • 2

2.4 修改配置文件如下

[root@m01 ~]# cat /etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict 172.16.1.0 mask 255.255.255.0 nomodify
restrict -6 ::1
server ntp1.aliyun.com     # 配置上层时间同步服务器
server 127.0.0.1
fudge 127.0.0.1 stratum 10  # 设置stratum级别为10
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

3 Client配置

1、先安装ntp,一般虚拟机默认都安装过了。 
2、只需要在客户端的配置文件/etc/ntp.conf中添加我们刚才配置的时间服务器ip地址或主机名(需要客户端本地配置dns解析,修改/etc/hosts文件)

[root@client ~]# cat /etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
server m01        #<==添加自己的时间服务器
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

4 服务端启动服务并测试

# Server Start Service
[root@m01 ~]# /etc/init.d/ntpd start

# Client Test
[root@client ~]# ntpdate m01
11 Apr 10:49:37 ntpdate[3523]: step time server 172.16.1.61 offset 40976.883166 sec
[root@client ~]# date
Wed Apr 11 10:49:40 CST 2018
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

5 扩展知识

5.1 修改本地时区

[root@m01 ~]# date   # 有可能我们看到的时区不是CST(中国的默认时区)
Wed Apr 11 11:54:18 EDT 2018

# 修改本地时区,把/usr/share/zoneinfo/目录下我们想要的timezone拷贝到/etc/localtime文件即可(覆盖)
[root@m01 ~]# \cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@m01 ~]# date
Wed Apr 11 11:55:21 CST 2018
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

5.2 配置文件中的driftfile是什么

我们每一个system clock的频率都有小小的误差,这个就是为什么机器运行一段时间后会不精确. NTP会自动来监测我们时钟的误差值并予以调整.但问题是这是一个冗长的过程,所以它会把记录下来的误差先写入driftfile.这样即使你重新开机以后之前的计算结果也就不会丢失了

5.3 同步硬件时钟

# 在/etc/sysconfig/ntpd配置文件中添加SYNC_HWCLOCK=yes即可
cat >>/etc/sysconfig/ntpd<<EOF
SYNC_HWCLOCK=yes
EOF
# 或者使用hwclock -w命令
  • 1
  • 2
  • 3
  • 4
  • 5

5.4 配置同步定时任务

# Client 每五分钟同步一下
cat >>/var/spool/cron/root<<EOF
#time sync by rsq
*/5 * * * * /usr/sbin/ntpdate m01 > /dev/null 2>&1
EOF
  • 1
  • 2
  • 3
  • 4
  • 5

5.5 设置开机自启动

[root@m01 ~]# chkconfig ntpd on
[root@m01 ~]# chkconfig --list ntpd
ntpd            0:off   1:off   2:on    3:on    4:on    5:on    6:off
  • 1
  • 2
  • 3

参考博客: 
https://blog.csdn.net/iloli/article/details/6431757 
http://blog.51cto.com/13178102/2052175


转载至https://blog.csdn.net/mr_rsq/article/details/79893632


猜你喜欢

转载自blog.csdn.net/vic_qxz/article/details/80537793