天天看点

Linux Centos NTP同步时间,以及搭建NTP时间服务器

一、在客户端与服务器端的NTP同步的手段

    1.安装chrony软件包

                [[email protected] ~]#yum -y install chrony

    2.修改chrony的配置文件

                [[email protected] ~]#vim /etc/chrony.conf

                看到第一部分,将四条默认server注释掉,并加入一条新的配置

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

# Ignore stratum in source selection.
stratumweight 0

# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift

# Enable kernel RTC synchronization.
rtcsync
           

 我连的是中国邮电大学的ntp服务  

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#server 0.rhel.pool.ntp.org iburst
#server 1.rhel.pool.ntp.org iburst
#server 2.rhel.pool.ntp.org iburst
#server 3.rhel.pool.ntp.org iburst
server s1a.time.edu.cn iburst

# Ignore stratum in source selection.
stratumweight 0

# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift

# Enable kernel RTC synchronization.
rtcsync
           

       3.启动ntp服务

                [[email protected] ~]#timedatectl   #查看ntp是否启动 

                  --------

                  NTP enabled: no

                  NTP synchronized: yes

                  ----------

                 [[email protected] ~]#timedatectl set-ntp yes #启动ntp

                 --------

                  NTP enabled: yes

                  NTP synchronized: yes

                  ----------

     4.重启chrony软件

                [[email protected] ~]#systemctl restart chronyd

                [[email protected] ~]#systemctl enable chronyd

       5.测试

               [[email protected] ~]#date       #查看date

                  2019年 07月 11日 星期四 19:12:48 CST

               [[email protected] ~]#date -s '1111-11-11'  #修改date

               [[email protected] ~]#date        #查看date

                  1111年 11月 11日 星期六 00:00:00 LMT

               [[email protected] ~]#systemctl restart chronyd

               [[email protected] ~]#systemctl enable chronyd       #重启chrony

               [[email protected] ~]#date   #查看date

                   2019年 07月 11日 星期四 19:15:48 CST

二、国内常用的ntp服务地址

配置的时候 server s1a.time.edu.cn

ntp.sjtu.edu.cn 202.120.2.101 (上海交通大学网络中心NTP服务器地址)  

s1a.time.edu.cn 北京邮电大学  

s1b.time.edu.cn 清华大学  

s1c.time.edu.cn 北京大学  

s1d.time.edu.cn 东南大学  

s1e.time.edu.cn 清华大学  

s2a.time.edu.cn 清华大学  

s2b.time.edu.cn 清华大学  

s2c.time.edu.cn 北京邮电大学  

s2d.time.edu.cn 西南地区网络中心  

s2e.time.edu.cn 西北地区网络中心  

s2f.time.edu.cn 东北地区网络中心  

s2g.time.edu.cn 华东南地区网络中心  

s2h.time.edu.cn 四川大学网络管理中心  

s2j.time.edu.cn 大连理工大学网络中心  

s2k.time.edu.cn CERNET桂林主节点  

s2m.time.edu.cn 北京大学

三、搭建共享NTP服务器

如果要保证多台终端时间同步,我们最好的做法是配置一台NTP服务器并且以这台服务器的时间为参照,让其他终端的时间与此台机器匹配,就可以实现多台终端时间同步的目的,NTP时间同步是很多技术的基础,也是造成许多故障的原因,如Ceph集群服务器,数据库集群服务器等多台终端同时工作的情况 

1.拓扑图

目的:创建NTP服务器,让所有PC的时间都与NTP时间同步

Linux Centos NTP同步时间,以及搭建NTP时间服务器

Network Time Protocol(网络时间协议)采用的是分层设计,如图-9所示,Stratum层的总数限制在15以内(包括15)

Linux Centos NTP同步时间,以及搭建NTP时间服务器

1.NTP服务器配置

  1. [[email protected] ~]# yum -y install chrony    //安装NTP支持软件 
  2. [[email protected] ~]# rpm -qc chrony                        //查看配置文件列表
  3. /etc/chrony.conf
  4. /etc/chrony.keys
  5. .. ..
  6. [[email protected] ~]# cat /etc/chrony.conf
  7. .. ..
  8. server 0.centos.pool.ntp.org iburst //server用户客户端指向上层NTP服务器
  9. allow 192.168.4.0/24                        //允许那个IP或网络访问NTP,允许192.168.4.0网段匹配
  10. #deny 192.168.4.1                        //拒绝那个IP或网络访问NTP
  11. local stratum 10                            //设置NTP服务器的层数量
  12. .. ..
  13. [[email protected] ~]# systemctl restart chronyd  #centos的服务是chronyd ubuntu是chrony
  14. [[email protected] ~]# systemctl enable chronyd      #开机自启

2.客户端配置

  1. [[email protected] ~]# yum -y install chrony
  2. [[email protected] ~]# vim /etc/chrony.conf
  3. server 192.168.4.5 iburst                //设置与哪台服务器同步数据
  4.                                         //iburst参数设置重启服务后尽快同步时间
  5. [[email protected] ~]# date -s "hour:minute"         //调整时间(小时:分钟)
  6. [[email protected] ~]# date                            //查看修改后的时间
  7. [[email protected] ~]# systemctl restart chronyd
  8. [[email protected] ~]# date                            //多执行几次查看结果

继续阅读