(7)centos7 同步伺服器時間
阿新 • • 發佈:2020-12-13
技術標籤:大資料技術開發
一、伺服器配置
1、檢視伺服器、客戶端作業系統版本
[[email protected] ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
2、檢視伺服器是否安裝ntp,系統預設安裝ntpdate;
[[email protected] ~]# rpm -qa | grep ntp fontpackages-filesystem-1.44-8.el7.noarch ntpdate-4.2.6p5-28.el7.centos.x86_64 python-ntplib-0.3.2-1.el7.noarch ntp-4.2.6p5-28.el7.centos.x86_64
3、安裝ntp ntpdate,其中ntpdate預設安裝,可以只安裝ntp;
yum install ntp ntpdate -y
4、檢視是否已安裝完成,與第2步對比
[[email protected] mysql-libs]# rpm -qa | grep ntp
fontpackages-filesystem-1.44-8.el7.noarch
ntpdate-4.2.6p5-28.el7.centos.x86_64
ntp-4.2.6p5-28.el7.centos.x86_64
python-ntplib-0.3.2-1.el7.noarch
5、檢視ntp伺服器狀態,兩條命令效果一樣
[[email protected] ~]# systemctl status ntpd
● ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
或
[[email protected] ~]# 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: inactive (dead)
6、修改配置檔案,使該NTP伺服器在不聯網的情況下,使用本伺服器的時間作為同步時間
vim /etc/ntp.conf
把如下四行程式碼註釋掉
#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
fudge 127.127.1.0 stratum 0
配置後:
[[email protected] ~]# vim /etc/ntp.conf
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default nomodify notrap nopeer noquery
# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict ::1
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#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
fudge 127.127.1.0 stratum 0
#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 autokey # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 autokey # manycast client
# Enable public key cryptography.
#crypto
includefile /etc/ntp/crypto/pw
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys
# Specify the key identifiers which are trusted.
#trustedkey 4 8 42
# Specify the key identifier to use with the ntpdc utility.
#requestkey 8
# Specify the key identifier to use with the ntpq utility.
#controlkey 8
# Enable writing of statistics records.
7、啟動ntp服務
systemctl start ntpd
或
service ntpd start
8、再次檢視伺服器狀態是否配置成功
[[email protected] mysql-libs]# systemctl status ntpd
● ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2019-08-16 18:33:01 CST; 13s ago
Process: 8792 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 8794 (ntpd)
Tasks: 1
CGroup: /system.slice/ntpd.service
└─8794 /usr/sbin/ntpd -u ntp:ntp -g
Aug 16 18:33:01 hadoop101.com ntpd[8794]: Listen normally on 3 ...
Aug 16 18:33:01 hadoop101.com ntpd[8794]: Listen normally on 4 ...
Aug 16 18:33:01 hadoop101.com ntpd[8794]: Listen normally on 5 ...
Aug 16 18:33:01 hadoop101.com ntpd[8794]: Listen normally on 6 ...
Aug 16 18:33:01 hadoop101.com ntpd[8794]: Listening on routing ...
Aug 16 18:33:01 hadoop101.com ntpd[8794]: 0.0.0.0 c016 06 restart
Aug 16 18:33:01 hadoop101.com ntpd[8794]: 0.0.0.0 c012 02 freq_...
Aug 16 18:33:01 hadoop101.com ntpd[8794]: 0.0.0.0 c011 01 freq_...
Aug 16 18:33:01 hadoop101.com systemd[1]: Started Network Time ...
Aug 16 18:33:02 hadoop101.com ntpd[8794]: 0.0.0.0 c514 04 freq_...
Hint: Some lines were ellipsized, use -l to show in full.
或
[[email protected] ~]# service ntpd status
9、檢視是否同步
[[email protected] ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*LOCAL(0) .LOCL. 5 l 20 64 7 0.000 0.000 0.000
10、設定開機啟動
[[email protected] ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
11、設定防火牆,開啟udp123埠(如果防火牆關了就不用設定了)
[[email protected] ~]# firewall-cmd --permanent --add-port=123/udp
success
[[email protected] ~]# firewall-cmd --reload
success
12、檢視防火牆已開啟埠
iptables -L -n
二、客戶端配置(其他節點)
前5步與伺服器一致
6、修改配置檔案,將剛剛搭建好的NTP伺服器作為客戶端上游時間伺服器
vim /etc/ntp.conf
#註釋掉其他上游時間伺服器
#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
#配置上游時間伺服器為本地的ntpd Server伺服器
server hadoop101.com
fudge hadoop101.com stratum 0
#配置允許上游時間伺服器主動修改本機的時間
restrict hadoop101.com nomodify notrap noquery
配置後:
[[email protected] ~]# vi /etc/ntp.conf
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
#restrict default nomodify notrap nopeer noquery
# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
#restrict 127.0.0.1
#restrict ::1
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
restrict hadoop101.com nomodify notrap noquery
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#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 hadoop101.com
fudge hadoop101.com stratum 0
#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 autokey # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 autokey # manycast client
# Enable public key cryptography.
#crypto
includefile /etc/ntp/crypto/pw
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys
# Specify the key identifiers which are trusted.
#trustedkey 4 8 42
# Specify the key identifier to use with the ntpdc utility.
#requestkey 8
# Specify the key identifier to use with the ntpq utility.
7、與本地ntpd Server同步一下
[[email protected] ~]# ntpdate -u hadoop101.com
15 Aug 11:33:35 ntpdate[8768]: adjust time server 192.168.1.101 offset 0.004621 sec
8、啟動ntp服務
systemctl start ntpd
或
service ntpd start
9、設定開機啟動
[[email protected] ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
10、檢視狀態
[[email protected] ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
hadoop101.com .LOCL. 1 u 13 64 1 8.148 -2.581 0.000