CentOS7之NTP服务器简单配置

本文介绍如何在CentOS环境中部署NTP服务器和chrony服务来实现网络时间同步。内容包括NTP服务器的安装、配置及客户端设置,以及使用chrony作为替代方案的方法。

时间服务器
NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议。
NTP服务器就是利用NTP协议提供时间同步服务的。

ntp软件(支持ntp协议) CentOS6自带
CentOS7需要安装的


chrony软件(支持ntp协议) CentOS7自带


环境准备
[root@Cobbler ~]# getenforce
Disabled
[root@Cobbler ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
[root@m02 ~]# hostname -I
10.0.0.62 172.16.1.62

第一种方法:NTP

服务端部署

  1. 下载软件
    [root@m02 ~]# yum install -y ntp
    [root@m02 ~]# rpm -qa ntp
    ntp-4.2.6p5-25.el7.centos.2.x86_64

  2. 配置ntp文件
    备份
    [root@m02 ~]# cp /etc/ntp.conf{,.ori}
    修改
    [root@m02 ~]# vim /etc/ntp.conf

8 #restrict default nomodify notrap nopeer noquery
9 restrict default nomodify

21 #server 0.centos.pool.ntp.org iburst
22 #server 1.centos.pool.ntp.org iburst
23 #server 2.centos.pool.ntp.org iburst
24 #server 3.centos.pool.ntp.org iburst
25 server ntp1.aliyun.com

  1. 与备份文件对比
    [root@m02 ~]# diff /etc/ntp.conf{,.ori}
    8,9c8,9
    < #restrict default nomodify notrap nopeer noquery
    < restrict default nomodify

restrict default nomodify notrap nopeer noquery

21,25c21,25
< #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 ntp1.aliyun.com

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

  1. 启动服务

[root@m02 ~]# systemctl start ntpd

  1. 检查ntp是否能同步
    [root@m02 ~]# ntpq -p
    remote refid st t when poll reach delay offset jitter

time5.aliyun.co 10.137.38.86 2 u 36 64 1 52.686 -1371.9 0.000

[root@m02 ~]# ntpstat
synchronised to NTP server (182.92.12.11) at stratum 3
time correct to within 982 ms
polling server every 64 s

客户端部署

  1. 下载软件
    [root@Cobbler ~]# yum -y install ntpdate

  2. 测试是否同步
    [root@Cobbler ~]# ntpdate 172.16.1.62
    10 Jan 09:07:47 ntpdate[1504]: adjust time server 172.16.1.62 offset -0.001513 sec

  3. 定时任务

[root@Cobbler ~]# crontab -l
#时间同步
/5 * /usr/sbin/ntpdate 172.16.1.62 &>/dev/null

第二种方法:chrony

服务端部署

  1. 修改chrony配置文件
    [root@m02 ~]# vim /etc/chrony.conf
    1 # Use public servers from the pool.ntp.org project.
    2 # Please consider joining the pool
    (http://www.pool.ntp.org/join.html).
    3 #server 0.centos.pool.ntp.org iburst
    4 #server 1.centos.pool.ntp.org iburst
    5 #server 2.centos.pool.ntp.org iburst
    6 #server 3.centos.pool.ntp.org iburst
    7 server ntp1.aliyun.com

    26 # Allow NTP client access from local network.
    27 #allow 192.168.0.0/16
    28 allow 172.16.1.0/24

  2. 开启服务
    [root@m02 ~]# systemctl start chronyd
    客户端部署
  3. 修改配置文件
    [root@Cobbler ~]# vim /etc/chrony.conf

1 # Use public servers from the pool.ntp.org project.
2 # Please consider joining the pool (http://www.pool.ntp.org/join.html).
3 #server 0.centos.pool.ntp.org iburst
4 #server 1.centos.pool.ntp.org iburst
5 #server 2.centos.pool.ntp.org iburst
6 #server 3.centos.pool.ntp.org iburst
7 server 172.16.1.62

  1. 开启服务
    [root@Cobbler ~]# systemctl start chronyd

  2. 修改时间测试
    [root@Cobbler ~]# date -s 1111
    Wed Jan 10 11:11:00 CST 2018

  3. 查看日志
    [root@Cobbler ~]# tailf /var/log/messages
    Jan 10 09:30:26 Cobbler chronyd[1593]: Selected source 172.16.1.62
    Jan 10 09:30:26 Cobbler chronyd[1593]: System clock wrong by -6348.794966 seconds, adjustment started
    Jan 10 09:30:26 Cob[root@Cobbler ~]# date -s 1111
    Wed Jan 10 11:11:00 CST 2018

5.查看时间
[root@Cobbler ~]# date
Wed Jan 10 11:15:47 CST 2018
[root@Cobbler ~]# date
Wed Jan 10 09:30:37 CST 2018

转载于:https://blog.51cto.com/13554498/2059429

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值