CentOS7 NTP客户端和服务器安装和使用

本文测试环境如下:

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[root@localhost ~]# 
[root@localhost ~]# yum install ntp
#等待安装完成后执行
[root@localhost ~]# rpm -qa | grep "ntp"
ntpdate-4.2.6p5-28.el7.centos.x86_64
ntp-4.2.6p5-28.el7.centos.x86_64
[root@localhost ~]# 

首先演示ntp 客户端安装。

#部分系统需要安装
[root@localhost ~]# yum install ntpdate

本文推荐两个ntp服务器。

#国内
ntp.aliyun.com

ntp1.aliyun.com
ntp2.aliyun.com
ntp3.aliyun.com

time1.aliyun.com
time2.aliyun.com

#国外
cn.ntp.org.cn

0.cn.ntp.org.cn
1.cn.ntp.org.cn
2.cn.ntp.org.cn
3.cn.ntp.org.cn


pool.ntp.org

asia.pool.ntp.org

安装完成后即可使用 ntpdate 同步时间。

[root@localhost ~]# ntpdate ntp.aliyun.com
22 Feb 10:32:03 ntpdate[7535]: adjust time server 203.107.6.88 offset -0.005418 sec
[root@localhost ~]# 

可以将同步命令作为一个定时执行任务。命令如下

下文演示搭建ntp服务器。

#检查ntp 安装情况,不是ntpdate
[root@localhost ~]# rpm -qa |grep ntp
ntpdate-4.2.6p5-28.el7.centos.x86_64
[root@localhost ~]# 

将时间同步作为定时任务。

#10点38分(不能颠倒,否则报错) ***(每年每月每天) 命令所在位置 服务器域名或ntp服务器ip地址 输出日志
38 10 * * * /usr/sbin/ntpdate ntp.aliyun.com >>/usr/local/logs/crontab/ntpdate.log

下文演示ntp服务器搭建。

[root@localhost ~]# yum install ntp
#等待安装完成执行命令
[root@localhost ~]# rpm -qa | grep "ntp"
ntpdate-4.2.6p5-28.el7.centos.x86_64
ntp-4.2.6p5-28.el7.centos.x86_64
[root@localhost ~]# 

重启服务。

[root@localhost ~]# service ntpd restart
Redirecting to /bin/systemctl restart ntpd.service
#显示NTP服务器节点
[root@localhost ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 203.107.6.88    100.107.25.114   2 u    2   64    1   54.932   21.875   0.000
 106.247.248.106 141.223.182.106  2 u    2   64    1   71.810   16.884   0.000
*LOCAL(0)        .LOCL.           5 l    2   64    1    0.000    0.000   0.000
[root@localhost ~]# 

关闭防火墙或添加规则,ntp 默认使用UDP 123端口,关闭SELinux。

[root@localhost ~]# service firewalld stop
#添加规则
[root@localhost ~]# firewall-cmd --zone=public --add-port=123/udp --permanent
success
[root@localhost ~]# service firewalld restart
Redirecting to /bin/systemctl restart firewalld.service
[root@localhost ~]# service firewalld status
Redirecting to /bin/systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since 五 2019-02-22 12:23:22 CST; 3s ago
     Docs: man:firewalld(1)
 Main PID: 7888 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─7888 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

2月 22 12:23:22 localhost.localdomain systemd[1]: Stopped firewalld - dynamic firewall daemon.
2月 22 12:23:22 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
2月 22 12:23:22 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
[root@localhost ~]# 

使用客户端同步,客户端IP为192.168.216.136。

[root@localhost ~]# ntpdate -d 192.168.216.137
22 Feb 12:17:06 ntpdate[7901]: ntpdate 4.2.6p5@1.2349-o Fri Apr 13 12:52:28 UTC 2018 (1)
Looking for host 192.168.216.137 and service ntp
host found : 192.168.216.137
transmit(192.168.216.137)
receive(192.168.216.137)
transmit(192.168.216.137)
receive(192.168.216.137)
transmit(192.168.216.137)
receive(192.168.216.137)
transmit(192.168.216.137)
receive(192.168.216.137)
server 192.168.216.137, port 123
stratum 6, precision -25, leap 00, trust 000
refid [192.168.216.137], delay 0.02591, dispersion 0.00000
transmitted 4, in filter 4
reference time:    e019f558.93ebd55b  Fri, Feb 22 2019 12:13:12.577
originate timestamp: e019f64a.8635f3c7  Fri, Feb 22 2019 12:17:14.524
transmit timestamp:  e019f64a.8f182003  Fri, Feb 22 2019 12:17:14.558
filter delay:  0.02597  0.02600  0.02591  0.02603 
         0.00000  0.00000  0.00000  0.00000 
filter offset: -0.03505 -0.03503 -0.03503 -0.03500
         0.000000 0.000000 0.000000 0.000000
delay 0.02591, dispersion 0.00000
offset -0.035035

22 Feb 12:17:14 ntpdate[7901]: adjust time server 192.168.216.137 offset -0.035035 sec
[root@localhost ~]# ntpdate 192.168.216.137
22 Feb 12:17:28 ntpdate[7902]: adjust time server 192.168.216.137 offset -0.035002 sec
[root@localhost ~]# 
#重启防火墙
[root@localhost ~]# systemctl restart firewalld.service
[root@localhost ~]#
[root@localhost ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since 五 2019-02-22 12:23:22 CST; 9min ago
     Docs: man:firewalld(1)
 Main PID: 7888 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─7888 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

2月 22 12:23:22 localhost.localdomain systemd[1]: Stopped firewalld - dynamic firewall daemon.
2月 22 12:23:22 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
2月 22 12:23:22 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
[root@localhost ~]# 

客户端添加自动执行任务。


[root@localhost ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@localhost ~]# 
#规则
0 0 * * * /usr/sbin/ntpdate 192.168.216.137 >>/data/logs/ntp.log 2>&1

 

转载于:https://my.oschina.net/u/1011130/blog/3013507

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值