Linux下配置NTP 本地时间服务器

ntp介绍

NTP是用来使系统和一个精确的时间源保持时间同步的协议.在Linux下,我们可以通过自带的NTP(Network Time Protocol)协议通过网络使自己的系统保持精确的时间.

ntp服务端

#调整时区
timedatectl set-timezone Asia/Shanghai
#(1)安装ntp
[root@localhost ~]# yum install ntp -y
[root@localhost ~]# mkdir /var/lib/ntp/
[root@localhost ~]# mkdir /var/log/ntp/

#(2)修改 /etc/ntp.conf
[root@localhost ~]# cp /etc/ntp.conf{,.bak}
[root@localhost ~]# vim /etc/ntp.conf
driftfile /var/lib/ntp/                    #记录当前时间服务器,与上游服务器的时间差的文件
#restrict default nomodify notrap nopeer noquery
restrict 192.168.17.0 mask 255.255.255.0 nomodify #允许192.168.17.0/24网段的所有主机使用该时间服务器进行时间同步
restrict 10.10.10.0 mask 255.255.255.0 nomodify #允许10.10.10.0/24网段的所有主机使用该时间服务器进行时间同步
restrict 127.0.0.1                      # 允许本机使用这个时间服务器
server 114.118.7.161 prefer             # ntp.ntsc.ac.cn解析后的地址114.118.7.161优先
server 114.118.7.163                    # ntp.ntsc.ac.cn解析后的地址114.118.7.163
server 203.107.6.88                     # ntp.aliyun.com解析后的地址203.107.6.88
#server 127.127.1.0                     #以本机作为时间服务器
#fudge 127.127.1.0 startnum 10          #设置服务器层级
logfile /var/log/ntp/ntp.log            #指定日志文件位置,需要手动创建
keys /etc/ntp/keys

#(3)启动ntp服务
[root@localhost ~]# systemctl start ntpd
[root@localhost ~]# systemctl enable ntpd

#(4)检查进程及同步状态
pgrep ntpd
ntpstat
ntpq -p
netstat -ntlup   #查看端口使用情况

[root@localhost ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 ntp.ntsc.ac.cn  .OLEG.           1 u   54   64   17    4.321    2.785   1.758
*ntp.ntsc.ac.cn  123.139.33.3     2 u   56   64   17    3.794    5.257   1.091
 ntp.aliyun.com  100.107.25.114   2 u   54   64   17   18.245    1.627   0.776

#(5)同步硬件时间
[root@localhost ~]# hwclock --systohc
[root@localhost ~]# vim /etc/sysconfig/ntpd
SYNC_HWCLOCK=yes

ntp客户端

#调整时区
timedatectl set-timezone Asia/Shanghai
#(1)安装
[root@agent ~]# yum install ntp ntpdate -y
[root@agent  ~]# mkdir /var/log/ntp
[root@agent  ~]# touch /var/log/ntp/ntp.log

#(2)修改配置文件
[root@agent  ~]# cp /etc/ntp.conf{,.bak}
[root@agent  ~]# vim /etc/ntp.conf
restrict default nomodify notrap nopeer noquery
#restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1 
restrict ::1
server 192.168.17.18 prefer  #改成ntp服务端IP
server ntp.aliyun.com iburst
server    127.127.1.0
fudge     127.127.1.0 stratum 5
driftfile /var/lib/ntp/drift
keys /etc/ntp/keys
disable monitor
broadcastdelay 0.008
logfile /var/log/ntp/ntp.log  #指定日志文件位置,需要手动创建

#(3)先执行一次ntpdate时间同步
[root@agent  ~]# ntpdate 192.168.17.18 #改成ntp服务端IP

#(4)启动ntpd
[root@agent  ~]# systemctl start ntpd

#(5)检查状态
[root@agent ~]#  ntpstat
synchronised to local net (127.127.1.0) at stratum 6
   time correct to within 7948 ms
   polling server every 64 s
# 或者
[root@agent  ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 192.168.17.18   114.118.7.161    2 u   33   64    1    0.998    1.510   0.000
 ntp.aliyun.com  10.137.38.86     2 u   30   64    1   17.051    0.100   0.019
*LOCAL(0)        .LOCL.           5 l   39   64    1    0.000    0.000   0.000

补充说明
在工作中我们一般都是使用ntpdate+ntp来完成时间同步,因为单独使用ntpdate同步时间虽然简单快捷但是会导致时间不连续,而时间不连续在数据库业务中影响是很大的,单独使用ntp做时间同步时,当服务器与时间服务器相差大的时候则无法启动ntpd来同步时间。由于ntpd做时间同步时是做的顺滑同步(可以简单理解为时间走得快,以便将落后的时间赶过来),所以同步到时间服务器的的时间不是瞬间完成的,开启ntpd之后稍等三五分钟就能完成时间同步。

# 用ntpq -p查看状态时的各种参数解释

remote	上游的时间服务器的ip或者主机名,如果是*表示本机就是做为上游服务器工作
refid	“.LOCL.”表示基于当前主机提供时间同步服务,如果是IP地址表示基于一个上游服务器提供时间同步服务。
st	表示remote远程服务器的层级编号
when	表示几秒之前做过一次时间同步
poll	表示每隔多少秒做一次时间同步
reach	表示向上游服务器成功请求时间同步的次数
delay	从本地机发送同步要求到ntp服务器的时间延迟
offset	主机通过NTP时钟同步与所同步时间源的时间偏移量,单位为毫秒(ms)。offset越接近于0,主机和ntp服务器的时间越接近
jitter	这是一个用来做统计的值. 它统计了在特定个连续的连接数里offset的分布情况. 简单地说这个数值的绝对值越小,主机的时间就越精确

猜你喜欢

转载自blog.csdn.net/change_can/article/details/129487534