(7)centos7 同步服务器时间

r囧r小猫 2022-12-27 12:55 298阅读 0赞

一、服务器配置

1、查看服务器、客户端操作系统版本

  1. [root@hadoop101 ~]# cat /etc/redhat-release
  2. CentOS Linux release 7.6.1810 (Core)

2、查看服务器是否安装ntp,系统默认安装ntpdate;

  1. [root@hadoop101 ~]# rpm -qa | grep ntp
  2. fontpackages-filesystem-1.44-8.el7.noarch
  3. ntpdate-4.2.6p5-28.el7.centos.x86_64
  4. python-ntplib-0.3.2-1.el7.noarch
  5. ntp-4.2.6p5-28.el7.centos.x86_64

3、安装ntp ntpdate,其中ntpdate默认安装,可以只安装ntp;

  1. yum install ntp ntpdate -y

4、查看是否已安装完成,与第2步对比

  1. [root@hadoop101 mysql-libs]# rpm -qa | grep ntp
  2. fontpackages-filesystem-1.44-8.el7.noarch
  3. ntpdate-4.2.6p5-28.el7.centos.x86_64
  4. ntp-4.2.6p5-28.el7.centos.x86_64
  5. python-ntplib-0.3.2-1.el7.noarch

5、查看ntp服务器状态,两条命令效果一样

  1. [root@hadoop101 ~]# systemctl status ntpd
  2. ntpd.service - Network Time Service
  3. Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
  4. Active: inactive (dead)

  1. [root@hadoop101 ~]# service ntpd status
  2. Redirecting to /bin/systemctl status ntpd.service
  3. ntpd.service - Network Time Service
  4. Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
  5. Active: inactive (dead)

6、修改配置文件,使该NTP服务器在不联网的情况下,使用本服务器的时间作为同步时间

  1. vim /etc/ntp.conf

把如下四行代码注释掉

  1. #server 0.centos.pool.ntp.org iburst
  2. #server 1.centos.pool.ntp.org iburst
  3. #server 2.centos.pool.ntp.org iburst
  4. #server 3.centos.pool.ntp.org iburst

在下面再添加两行

  1. server 127.127.1.0
  2. fudge 127.127.1.0 stratum 0

配置后:

复制代码

  1. [root@hadoop101 ~]# vim /etc/ntp.conf
  2. # For more information about this file, see the man pages
  3. # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
  4. driftfile /var/lib/ntp/drift
  5. # Permit time synchronization with our time source, but do not
  6. # permit the source to query or modify the service on this system.
  7. restrict default nomodify notrap nopeer noquery
  8. # Permit all access over the loopback interface. This could
  9. # be tightened as well, but to do so would effect some of
  10. # the administrative functions.
  11. restrict 127.0.0.1
  12. restrict ::1
  13. # Hosts on local network are less restricted.
  14. #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
  15. # Use public servers from the pool.ntp.org project.
  16. # Please consider joining the pool (http://www.pool.ntp.org/join.html).
  17. #server 0.centos.pool.ntp.org iburst
  18. #server 1.centos.pool.ntp.org iburst
  19. #server 2.centos.pool.ntp.org iburst
  20. #server 3.centos.pool.ntp.org iburst
  21. server 127.127.1.0
  22. fudge 127.127.1.0 stratum 0
  23. #broadcast 192.168.1.255 autokey # broadcast server
  24. #broadcastclient # broadcast client
  25. #broadcast 224.0.1.1 autokey # multicast server
  26. #multicastclient 224.0.1.1 # multicast client
  27. #manycastserver 239.255.254.254 # manycast server
  28. #manycastclient 239.255.254.254 autokey # manycast client
  29. # Enable public key cryptography.
  30. #crypto
  31. includefile /etc/ntp/crypto/pw
  32. # Key file containing the keys and key identifiers used when operating
  33. # with symmetric key cryptography.
  34. keys /etc/ntp/keys
  35. # Specify the key identifiers which are trusted.
  36. #trustedkey 4 8 42
  37. # Specify the key identifier to use with the ntpdc utility.
  38. #requestkey 8
  39. # Specify the key identifier to use with the ntpq utility.
  40. #controlkey 8
  41. # Enable writing of statistics records.

复制代码

7、启动ntp服务

  1. systemctl start ntpd

  1. service ntpd start

8、再次查看服务器状态是否配置成功

复制代码

  1. [root@hadoop101 mysql-libs]# systemctl status ntpd
  2. ntpd.service - Network Time Service
  3. Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
  4. Active: active (running) since Fri 2019-08-16 18:33:01 CST; 13s ago
  5. Process: 8792 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
  6. Main PID: 8794 (ntpd)
  7. Tasks: 1
  8. CGroup: /system.slice/ntpd.service
  9. └─8794 /usr/sbin/ntpd -u ntp:ntp -g
  10. Aug 16 18:33:01 hadoop101.com ntpd[8794]: Listen normally on 3 ...
  11. Aug 16 18:33:01 hadoop101.com ntpd[8794]: Listen normally on 4 ...
  12. Aug 16 18:33:01 hadoop101.com ntpd[8794]: Listen normally on 5 ...
  13. Aug 16 18:33:01 hadoop101.com ntpd[8794]: Listen normally on 6 ...
  14. Aug 16 18:33:01 hadoop101.com ntpd[8794]: Listening on routing ...
  15. Aug 16 18:33:01 hadoop101.com ntpd[8794]: 0.0.0.0 c016 06 restart
  16. Aug 16 18:33:01 hadoop101.com ntpd[8794]: 0.0.0.0 c012 02 freq_...
  17. Aug 16 18:33:01 hadoop101.com ntpd[8794]: 0.0.0.0 c011 01 freq_...
  18. Aug 16 18:33:01 hadoop101.com systemd[1]: Started Network Time ...
  19. Aug 16 18:33:02 hadoop101.com ntpd[8794]: 0.0.0.0 c514 04 freq_...
  20. Hint: Some lines were ellipsized, use -l to show in full.

复制代码

  1. [root@hadoop101 ~]# service ntpd status

9、查看是否同步

  1. [root@hadoop101 ~]# ntpq -p
  2. remote refid st t when poll reach delay offset jitter
  3. ==============================================================================
  4. *LOCAL(0) .LOCL. 5 l 20 64 7 0.000 0.000 0.000

10、设置开机启动

  1. [root@hadoop101 ~]# systemctl enable ntpd
  2. Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

11、设置防火墙,打开udp123端口(如果防火墙关了就不用设置了)

  1. [root@hadoop101 ~]# firewall-cmd --permanent --add-port=123/udp
  2. success
  3. [root@hadoop101 ~]# firewall-cmd --reload
  4. success

12、查看防火墙已打开端口

  1. iptables -L -n

二、客户端配置(其他节点)

前5步与服务器一致

6、修改配置文件,将刚刚搭建好的NTP服务器作为客户端上游时间服务器

复制代码

  1. vim /etc/ntp.conf
  2. #注释掉其他上游时间服务器
  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. #配置上游时间服务器为本地的ntpd Server服务器
  8. server hadoop101.com
  9. fudge hadoop101.com stratum 0
  10. #配置允许上游时间服务器主动修改本机的时间
  11. restrict hadoop101.com nomodify notrap noquery

复制代码

配置后:

复制代码

  1. [root@hadoop103 ~]# vi /etc/ntp.conf
  2. # For more information about this file, see the man pages
  3. # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
  4. driftfile /var/lib/ntp/drift
  5. # Permit time synchronization with our time source, but do not
  6. # permit the source to query or modify the service on this system.
  7. #restrict default nomodify notrap nopeer noquery
  8. # Permit all access over the loopback interface. This could
  9. # be tightened as well, but to do so would effect some of
  10. # the administrative functions.
  11. #restrict 127.0.0.1
  12. #restrict ::1
  13. # Hosts on local network are less restricted.
  14. #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
  15. restrict hadoop101.com nomodify notrap noquery
  16. # Use public servers from the pool.ntp.org project.
  17. # Please consider joining the pool (http://www.pool.ntp.org/join.html).
  18. #server 0.centos.pool.ntp.org iburst
  19. #server 1.centos.pool.ntp.org iburst
  20. #server 2.centos.pool.ntp.org iburst
  21. #server 3.centos.pool.ntp.org iburst
  22. server hadoop101.com
  23. fudge hadoop101.com stratum 0
  24. #broadcast 192.168.1.255 autokey # broadcast server
  25. #broadcastclient # broadcast client
  26. #broadcast 224.0.1.1 autokey # multicast server
  27. #multicastclient 224.0.1.1 # multicast client
  28. #manycastserver 239.255.254.254 # manycast server
  29. #manycastclient 239.255.254.254 autokey # manycast client
  30. # Enable public key cryptography.
  31. #crypto
  32. includefile /etc/ntp/crypto/pw
  33. # Key file containing the keys and key identifiers used when operating
  34. # with symmetric key cryptography.
  35. keys /etc/ntp/keys
  36. # Specify the key identifiers which are trusted.
  37. #trustedkey 4 8 42
  38. # Specify the key identifier to use with the ntpdc utility.
  39. #requestkey 8
  40. # Specify the key identifier to use with the ntpq utility.

复制代码

7、与本地ntpd Server同步一下

  1. [root@hadoop102 ~]# ntpdate -u hadoop101.com
  2. 15 Aug 11:33:35 ntpdate[8768]: adjust time server 192.168.1.101 offset 0.004621 sec

8、启动ntp服务

  1. systemctl start ntpd

  1. service ntpd start

9、设置开机启动

  1. [root@hadoop102 ~]# systemctl enable ntpd
  2. Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

10、查看状态

  1. [root@hadoop103 ~]# ntpq -p
  2. remote refid st t when poll reach delay offset jitter
  3. ==============================================================================
  4. hadoop101.com .LOCL. 1 u 13 64 1 8.148 -2.581 0.000

发表评论

表情:
评论列表 (有 0 条评论,298人围观)

还没有评论,来说两句吧...

相关阅读

    相关 centos7 ntp时间同步

    一 应用场景 公司分配给你两台服务器,要求你使他们时间同步,有人问为什么要时间同步?如果一个集群中,时间相差很大,那么会出现很多诡异的问题,你也不想在一个无法解决的问题上浪费

    相关 CentOS 7同步系统时间

            在Linux中有硬件时钟与系统时钟等两种时钟。硬件时钟是指主机板上的时钟设备,也就是通常可在BIOS画面设定的时钟。系统时钟则是指kernel中的时钟。当Lin

    相关 centos7 ntp设置 时间同步

    一 应用场景 公司分配给你两台服务器,要求你使他们时间同步,有人问为什么要时间同步?如果一个集群中,时间相差很大,那么会出现很多诡异的问题,你也不想在一个无法解决的问题上浪费