Linux ntp服务安装教程(离线)

1.常用命令

      rpm -qa | grep ntp     #查询已安装的ntp版本信息等

      service ntpd status    #查询ntp服务状态

      service ntpd start      #启动

      service ntpd stop      #停止

      service ntpd restart   #重启

      ntpq -p   #查看ntp服务器与上层ntp的状态

      ntpstat     #命令查看时间同步状态,这个一般需要5-10分钟后才能成功连接和同步。所以,服务器启动后需要稍等下。
      
     ntpdate命令:将客户端时间同步为时间服务器的时间

     ntpdate -u 11.10.10.111   #客户端时间同步111时间服务器

     ntpdate -u cn.pool.ntp.org #指定名字同步时间

     hwclock --systohc(-w)   #将系统时间同步到硬件时钟

刚启动的时候,一般是:

# ntpstat 
 
  unsynchronised
 
  time server re-starting
 
  polling server every 64 s

连接同步后:

synchronised to NTP server (202.120.2.101) at stratum 4 
 
   time correct to within 1192 ms
 
   polling server every 64 s

2.下载包(分在线和离线两种)

若用下面这个命令,发现有服务包,但是只有ntpdate这种就是不全,若是和下面一样才能说明是全的,不然到时候启动会报依赖或者服务不存在的错误,因此有的话最好先卸载掉再重新安装。

rpm -qa | grep ntp

ntpdate-4.2.6p5-1.el6.x86_64
ntp-4.2.6p5-1.el6.x86_64
fontpackages-filesystem-1.41-1.1.el6.noarch
 

卸载命令,从remove后面跟的就是刚才上步骤查询出来的服务全称。

yum -y remove ntpdate-4.2.6p5-1.el6.x86_64

然后安装命令,有yum源的线上下载命令
安装 ntp :

yum -y install ntp

没有的话就得去网上下了,资源都有我的传不上去了,下面跟解压命令和安装命令:

unzip ntp离线安装包
rpm -ivh autogen-libopts-5.18-5.el7.x86_64.rpm 
rpm -ivh ntpdate-4.2.6p5-18.el7.centos.x86_64.rpm 
rpm -ivh ntp-4.2.6p5-18.el7.centos.x86_64.rpm

若出现找不到依赖则使用下面的命令:
rpm -ivh ntp-4.2.6p5-18.el7.centos.x86_64.rpm --nodeps --force

若觉得版本低了可以去找版本高的,
http://mirrors.163.com/
http://mirrors.163.com/centos/7/os/x86_64/Packages/
这里是网易开源的镜像,阿里的也有我没找过,有热心的网友可以贴到下面评论后我贴上来也行

3.配置ntp服务

1)配置前检查

配置之前确认时间和当前时间一致,可通过date命令手动进行设置

date -s 'yyyy-MM-dd HH-mm-ss'    #设置时间

hwclock -w    #系统时间同步到硬件

ntp端口为123,通过netstat命令查看端口对应的进程,特别注意防火墙是否有打开123端口,最好的处理办法就是关闭防火墙,如:

netstat -ntlp | grep 123    #查看123端口对应的进程

systemctl stop firewallds 和 systemctl disable firewallds    #关闭防火墙和开机自启动

2) server端配置
如有两台Linux服务器,IP分别为11.10.10.111和11.10.10.222, 111的服务器做ntp server,222的服务器做ntp client。

vim /etc/ntp.conf
 
#与上级服务器所花费的时间存入指定文件
driftfile /var/lib/ntp/drift
 
#对ip设定权限 default为所有ip
restrict default nomodify notrap nopear noquery
 
restrict 11.10.10.111 nomodify notrap nopear noquery
restrict 11.10.10.201 mask 255.255.255.0 nomodify notrap nopear noquery
 
restrict 127.0.0.1
restrict ::1
 
#设定上级时间服务器
#国内时间服务器
server 0.cn.pool.ntp.org prefer
server 1.cn.pool.ntp.org
server 2.cn.pool.ntp.org
server 3.cn.pool.ntp.org
#centos系统默认上级时间服务器
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
#本机时间作为时间服务
server 127.127.1.0 #local clock
fudge 127.127.1.0 stratum 10
 
#
includefile /etc/ntp/crypto/pw
 
keys /etc/ntp/keys
 
disable monitor 

启动ntpd服务并配置开机自启

systemctl start ntpd  #启动ntpd服务
systemctl enable ntpd  #配置开机自启

note: ntpd启动后, 客户端需要等待几分钟再同步时间服务器。

3)配置ntp client端

vim /etc/ntp.conf
 
#与上级服务器所花费的时间存入指定文件
driftfile /var/lib/ntp/drift
 
#对ip设定权限 default为所有ip
restrict default nomodify notrap nopear noquery
 
restrict 10.10.10.222 nomodify notrap nopear noquery
restrict 10.10.10.201 mask 255.255.255.0 nomodify notrap nopear noquery
 
restrict 127.0.0.1
restrict ::1
 
#同步111时间服务器
server 10.10.10.111 minpoll 4 maxpoll 6 iburst
fudge 10.10.10.111 stratum 10
 
#
includefile /etc/ntp/crypto/pw
 
keys /etc/ntp/keys
 
disable monitor 

note: 先关闭客户端ntpd服务,手动同步时间,再启动ntpd服务

systemctl stop ntpd    #停止ntpd服务
ntpdate -u 11.10.10.111   #同步时间服务器
systemctl start ntpd
systemctl enable ntpd

4.服务测试(ntpstat一般是过个5-10分钟才能同步)

(1)服务器:

[

root@localhost etc]# ntpq -p
 
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*LOCAL(0)        .LOCL.           8 l   36   64  377    0.000    0.000   0.000
 
 
[root@localhost etc]# ntpstat
 
synchronised to local net at stratum 9 
   time correct to within 12 ms
   polling server every 64 s

(2)客户端:

[root@localhost etc]# ntpq -p
 
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*10.173.24.27    LOCAL(0)         9 u   40   64  377    1.108    2.560   0.764
 
 
[root@localhost etc]# ntpstat
 
synchronised to NTP server (10.173.24.27) at stratum 10 
   time correct to within 461 ms
   polling server every 64 s

5.日志设置

[root@localhost sysconfig]# pwd
/etc/sysconfig
 
[root@localhost sysconfig]#cat ntpd
 
# Command line options for ntpd
OPTIONS="-g -l /var/log/ntpstats/ntpd.log"
 
[root@localhost ntpstats]# service ntpd stop
 
[root@localhost ntpstats]# service ntpd start 
 
[root@localhost ntpstats]# service ntpd status
 
[root@localhost ntpstats]# tail -200f /var/log/ntpstats/ntpd.log

同上已经全部配置完成了,下面启动命令以及状态命令查询

[root]# service ntpd start

[root]# service ntpd status
Redirecting to /bin/systemctl status ntpd.service
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
   Active: active (running) since 三 2021-03-10 09:59:33 CST; 6h ago
  Process: 32597 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 32598 (ntpd)
   CGroup: /system.slice/ntpd.service
           └─32598 /usr/sbin/ntpd -u ntp:ntp -g

310 09:59:33 node02 ntpd[32598]: Listen normally on 5 ens160 fe80::57cc:8016:8ab7:7fb9 UDP 123
310 09:59:33 node02 ntpd[32598]: Listening on routing socket on fd #22 for interface updates
310 09:59:33 node02 ntpd[32598]: 0.0.0.0 c016 06 restart
310 09:59:33 node02 ntpd[32598]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM
310 09:59:33 node02 ntpd[32598]: 0.0.0.0 c011 01 freq_not_set
310 09:59:34 node02 ntpd[32598]: 0.0.0.0 c61c 0c clock_step +0.571381 s
310 09:59:35 node02 ntpd[32598]: 0.0.0.0 c614 04 freq_mode
310 09:59:36 node02 ntpd[32598]: 0.0.0.0 c618 08 no_sys_peer
310 10:15:10 node02 ntpd[32598]: 0.0.0.0 c612 02 freq_set kernel 16.170 PPM
310 10:15:10 node02 ntpd[32598]: 0.0.0.0 c615 05 clock_sync

6.ntp配置文件以及参数介绍

ntp.conf配置文件意义

driftfile /var/lib/ntp/drift    //和上层NTP服务器频率误差的记录文件
restrict IP mask . nomodify ... //指定可以和NTP通信的IP或者网段
server <ip|fqdn> [prefer]      //指定为本地服务器提供时间的上层NTP服务器的IP地址或者是FQDN。在指定多个上层NTP服务器时,使用prefer参数的服务器的优先级更高,将优先使用。如果没有指定prefer的话,则按照顺序从上往下,由高到低。如果要以本地的时间作为时间源的话,则是server 127.127.1.0
logfile /var/log/ntp            //指定日志文件
logconfig all                   //指定日志级别, info, event, all三种可以选择
broadcastclient key 4           //接收NTP广播消息
broadcast 192.168.0.1           //使用广播模式, 指定广播地址
crypto                          //启用公钥加密
key /etc/ntp/keys               //私钥文件
minpoll n                       //最小轮询时间,2的n次方(广播时每个客户端的轮询时间)
fudge <ip|fqdn> stratum <1-15>  //修改NTP服务器的stratum信息。如果stratum为16就认为该NTP服务器不可用。

restrict参数意义

restrict [ 客户端IP ]  mask  [ IP掩码 ]  [参数]

“客户端IP” 和 “IP掩码” 指定了对网络中哪些范围的计算机进行控制,如果使用default关键字,则表示对所有的计算机进行控制,参数指定了具体的限制内容,常见的参数如下:

◆ ignore:拒绝连接到NTP服务器
◆ nomodiy: 客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
◆ noquery: 不提供客户端的时间查询
◆ notrap: 不提供trap远程登录功能,trap服务是一种远程时间日志服务。
◆ notrust: 客户端除非通过认证,否则该客户端来源将被视为不信任子网 。
◆ nopeer: 提供时间服务,但不作为对等体。
◆ kod: 向不安全的访问者发送Kiss-Of-Death报文。

server参数意义

server host  [ key n ] [ version n ] [ prefer ] [ mode n ] [ minpoll n ] [ maxpoll n ] [ iburst ]

其中host是上层NTP服务器的IP地址或域名,随后所跟的参数解释如下所示:

◆ key: 表示所有发往服务器的报文包含有秘钥加密的认证信息,n是32位的整数,表示秘钥号。
◆ version: 表示发往上层服务器的报文使用的版本号,n默认是3,可以是1或者2。
◆ prefer: 如果有多个server选项,具有该参数的服务器优先使用。
◆ mode: 指定数据报文mode字段的值。
◆ minpoll: 指定与查询该服务器的最小时间间隔为2的n次方秒,n默认为6,范围为4-14。
◆ maxpoll:  指定与查询该服务器的最大时间间隔为2的n次方秒,n默认为10,范围为4-14。
◆ iburst: 当初始同步请求时,采用突发方式接连发送8个报文,时间间隔为2
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐