一、简单介绍:

NTPNetwork Time Protocol,网络时间协议,服务器时间同步的一种协议,它可以使服务器对其它服务器或时钟源(如石英钟、GPS等等)做同步化,它可以提供高精准度的时间校正(LAN局域网上与标准间差小于1毫秒,WAN互联网上与标准间差几十毫秒),且可介由加密确认的方式来防止恶毒的协议***。ntpdNTP的守护进程,它通过周期性地查询公共时间服务器来调整系统时钟的时间偏移量,该进程只花费少量的进程资源和内存。

 

二、演示环境:

IP

主机名

操作系统

服务器角色

描述

时间同步方式

192.168.1.220

ntp-server

CentOS   6.10

内网NTP Server

与阿里云提供的公网NTP服务器同步时间

作为内网NTP Server,内网中的其它服务器向其同步时间

ntpd服务平滑同步

192.168.1.221

ntp-client1

CentOS   6.10

内网NTP Client

向内网NTP Server 192.168.1.220同步时间

ntpd服务平滑同步

192.168.1.222

ntp-client2

CentOS   6.10

内网NTP Client

向内网NTP Server 192.168.1.220同步时间

crontab   + ntpdate强制同步

备注:使用ntpd服务平滑同步时间的方式要优于crontab + ntpdate,因为ntpdate同步时间会造成时间的跳跃,对一些依赖时间的程序和服务会造成影响,例如:sleeptimer等,且ntpd服务可以在修正时间的过程中同时修正CPU tick

 

三、192.168.1.220配置内网NTP Server

1、关闭iptables防火墙或者开放UDP协议的123端口

2、关闭SELinux

3、配置主机名ntp-server

# vim /etc/hosts --> 192.168.1.220 ntp-server

# vim /etc/sysconfig/network --> HOSTNAME=ntp-server

# hostname ntp-server

# logout

Ctrl + Shift + r

# hostname

4、安装NTP# yum -y install ntp

5、手动同步服务器时间:

(1)查看当前服务器时间:# date

image.png

(2)手动同步服务器时间,可以执行2次:# ntpdate ntp1.aliyun.com

image.png

备注:需要手动同步服务器时间是因为如果当前服务器时间与目标NTP服务器时间误差过大时,ntpd是不会进行时间同步的。

(3)查看同步后的服务器时间:# date

image.png

特别注意:使用ntpdate命令手动同步时间之前不能启动ntpd服务,否则会提示端口被占用。

image.png

6、修改主配置文件/etc/ntp.conf

# mv /etc/ntp.conf /etc/ntp.conf.bak

# vim /etc/ntp.conf,新增如下代码:

driftfile /var/lib/ntp/drift

logfile /var/log/ntp.log

pidfile /var/run/ntpd.pid

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

# 允许任何IP的客户端进行时间同步,但不允许修改NTP服务端参数,default类似于0.0.0.0

restrict default kod nomodify notrap nopeer noquery

restrict -6 default kod nomodify notrap nopeer noquery

# 允许通过本地回环接口进行所有访问

restrict 127.0.0.1

restrict -6 ::1

# 只允许192.168.1网段的客户端进行时间同步,但不允许修改NTP服务端参数

restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap nopeer noquery

# 指定上层NTP服务器为阿里云提供的公网NTP服务器,并设置访问权限

restrict ntp1.aliyun.com nomodify notrap nopeer noquery

restrict ntp2.aliyun.com nomodify notrap nopeer noquery

restrict ntp3.aliyun.com nomodify notrap nopeer noquery

restrict ntp4.aliyun.com nomodify notrap nopeer noquery

restrict ntp5.aliyun.com nomodify notrap nopeer noquery

restrict ntp6.aliyun.com nomodify notrap nopeer noquery

restrict ntp7.aliyun.com nomodify notrap nopeer noquery

server ntp1.aliyun.com iburst minpoll 4 maxpoll 10

server ntp2.aliyun.com iburst minpoll 4 maxpoll 10

server ntp3.aliyun.com iburst minpoll 4 maxpoll 10

server ntp4.aliyun.com iburst minpoll 4 maxpoll 10

server ntp5.aliyun.com iburst minpoll 4 maxpoll 10

server ntp6.aliyun.com iburst minpoll 4 maxpoll 10

server ntp7.aliyun.com iburst minpoll 4 maxpoll 10

# 阿里云提供的公网NTP服务器不可用时,采用本地时间作为同步标准

server 127.127.1.0

fudge 127.127.1.0 stratum 10

备注:常用指令说明

Ø  kod:访问违规时发送KOD

Ø  nomodify:客户端不能更改服务端的参数信息,但是客户端可以通过服务端进行网络校时

Ø  notrap:不提供trap远程事件登录

Ø  nopeer:用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟

Ø  noquery:不提供客户端时间查询,客户端不能使用ntpqntpdc等命令来查询NTP服务器

Ø  notrust:客户端除非通过认证,否则该客户端来源将被视为不信任网域

Ø  iburst:如果服务端在一个标准轮询间隔内没有应答,客户端会发送一定数量的包(8个而不是通常的1个)给NTP服务端,如果在短时间内呼叫NTP服务端几次,都没有出现可辨识的应答,那么本地时间将不会发生变化

Ø  minpollNTP消息轮询间隔的最小值,默认为664秒)

Ø  maxpollNTP消息轮询间隔的最大值,默认为101024秒)

7、修改配置文件/etc/sysconfig/ntpd,末尾新增代码:SYNC_HWCLOCK=yes

备注:默认情况下,CentOS系统时间和硬件时间并不会自动同步。在CentOS运行过程中,系统时间和硬件时间以异步的方式运行,互不干扰。硬件时间的运行是靠BIOS电池来维持,而系统时间是靠CPU tick来维持。在系统开机时,会自动从BIOS中读取硬件时间,并设置为系统时间。NTP一般只会同步系统时间,如果需要使硬件时间和系统时间同步,就需要修改配置文件/etc/sysconfig/ntpd

8、修改用于初始同步的服务器列表配置文件/etc/ntp/step-tickers

# vim /etc/ntp/step-tickers,新增如下代码:

ntp1.aliyun.com

ntp2.aliyun.com

ntp3.aliyun.com

ntp4.aliyun.com

ntp5.aliyun.com

ntp6.aliyun.com

ntp7.aliyun.com

9、启动ntpd服务:

# service ntpd start

# ss -tunlp | grep -w :123

image.png

# yum -y install lsof

# lsof -i :123

image.png

# chkconfig ntpd on

# chkconfig --list ntpd

10、5~10分钟后查看时间同步状态:# ntpstat

image.png

备注:

Ø  120.25.115.20ntp1.aliyun.com域名解析后的地址

Ø  本地服务器与上层服务器的时间偏差小于28ms

Ø  本地服务器每隔64s主动同步一次时间

11、查看所有作为时钟源校正过本地NTP服务器的上层NTP服务器列表:# ntpq -p

image.png

备注:字段意义如下所示

Ø  remote:本地服务器连接的远程NTP服务器IP120.25.115.20ntp1.aliyun.com域名解析后的地址,203.107.6.88ntp2.aliyun.com~ntp7.aliyun.com域名解析后的地址

Ø  +:正在使用的上层NTP服务器IP

Ø  *:作为备用服务器提供时间更新的NTP服务器IP

Ø  refid:为远程NTP服务器提供时间同步的NTP服务器IP

Ø  st:远程NTP服务器级别,即Stratum阶层,服务器在逻辑上形成阶梯式的架构相互连接

Ø  t:本地服务器与远程NTP服务器的通信方式,u:单播、b:广播、i:本地

Ø  when:上一次时间同步成功距离现在的秒数

Ø  poll:本地服务器和远程NTP服务器多久进行一次时间同步,单位秒

Ø  reach:已经向上层NTP服务器请求更新的次数,是一个八进制数字,每成功一次它的值就会增加

Ø  delay:网络传输过程中RTTRound-Trip Time,往返时间)的延迟

Ø  offset:服务器和所同步时间源的时间偏移量,offset越接近于0,本地服务器和NTP服务器的时间越接近

Ø  jitter:统计在特定个连续的连接数里offset的分布情况,数值绝对值越小,本地服务器时间就越精确

 

四、192.168.1.221配置内网NTP Client

1、关闭iptables防火墙

2、关闭SELinux

3、配置主机名ntp-client1

# vim /etc/hosts --> 192.168.1.221 ntp-client1

# vim /etc/sysconfig/network --> HOSTNAME=ntp-client1

# hostname ntp-client1

# logout

Ctrl + Shift + r

# hostname

4、安装NTP# yum -y install ntp

5、手动同步服务器时间:

(1)查看当前服务器时间:# date

image.png

(2)手动同步服务器时间,可以执行2次:# ntpdate 192.168.1.220

image.png

(3)查看同步后的服务器时间:# date

image.png

6、修改主配置文件/etc/ntp.conf

# mv /etc/ntp.conf /etc/ntp.conf.bak

# vim /etc/ntp.conf,新增如下代码:

driftfile /var/lib/ntp/drift

logfile /var/log/ntp.log

pidfile /var/run/ntpd.pid

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

restrict 127.0.0.1

restrict -6 ::1

restrict 192.168.1.220 nomodify notrap nopeer noquery

server 192.168.1.220

server 127.127.1.0

fudge 127.127.1.0 stratum 10

7、修改配置文件/etc/sysconfig/ntpd,末尾新增代码:SYNC_HWCLOCK=yes

8、修改用于初始同步的服务器列表配置文件/etc/ntp/step-tickers,新增代码:192.168.1.220

9、启动ntpd服务:

# service ntpd start

# ss -tunlp | grep -w :123

image.png

# yum -y install lsof

# lsof -i :123

image.png

# chkconfig ntpd on

# chkconfig --list ntpd

10、5~10分钟后查看时间同步状态:# ntpstat

image.png

11、查看所有作为时钟源校正过本地NTP服务器的上层NTP服务器列表:# ntpq -p

image.png


五、192.168.1.222配置内网NTP Client

1、关闭iptables防火墙

2、关闭SELinux

3、配置主机名ntp-client2

# vim /etc/hosts --> 192.168.1.222 ntp-client2

# vim /etc/sysconfig/network --> HOSTNAME=ntp-client2

# hostname ntp-client2

# logout

Ctrl + Shift + r

# hostname

4、安装ntpdate# yum -y install ntpdate

5、手动同步服务器时间:

(1)查看当前服务器时间:# date

image.png

(2)手动同步服务器时间,可以执行2次:# ntpdate 192.168.1.220

image.png

(3)查看同步后的服务器时间:# date

image.png

6、修改用于初始同步的服务器列表配置文件/etc/ntp/step-tickers,新增代码:192.168.1.220

7、编写定时任务,实现每3分钟向192.168.1.220同步一次时间,并将系统时间设置为硬件时间

# crontab -e --> */3 * * * * /usr/sbin/ntpdate 192.168.1.220 &> /dev/null;/sbin/hwclock -w

# crontab -l

# chkconfig crond on

# chkconfig --list crond

8、测试定时任务:将时间调慢,观察3分钟后时间是否会自动同步

image.png


六、Windows 10自动定期同步阿里云Windows公共NTP服务器:

Win + r --> control panel --> 查看方式选择“类别”--> 时钟和区域 --> 日期和时间 --> Internet时间 --> 更改设置 --> 输入阿里云Windows公共NTP服务器地址time.pool.aliyun.com --> 立即更新

image.png

image.png

image.png