下面內容引自於 https://blog.csdn.net/hellboy0621/article/details/81903091
ntp分為服務端和客戶端
一、服務端配置
下面執行命令目錄無要求,有要求會提示
1、查看服務器是否安裝ntp,系統默認安裝ntpdate;
[root@k8s-01 /]# rpm -qa | grep ntp fontpackages-filesystem-1.44-8.el7.noarch python-ntplib-0.3.2-1.el7.noarch ntpdate-4.2.6p5-25.el7.centos.x86_64
2、安裝ntp ntpdate,其中ntpdate默認安裝,可以只安裝ntp;
[root@k8s-01 /]# yum install ntp ntpdate -y
3、查看是否已安裝完成,與第2步對比
[root@k8s-01 /]# rpm -qa | grep ntp ntpdate-4.2.6p5-28.el7.centos.x86_64 fontpackages-filesystem-1.44-8.el7.noarch ntp-4.2.6p5-28.el7.centos.x86_64 python-ntplib-0.3.2-1.el7.noarch
4、查看ntp服務器狀態,兩條命令效果一樣
[root@k8s-01 /]# systemctl status ntpd
● ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
5、修改配置文件,使該NTP服務器在不聯網的情況下,使用本服務器的時間作為同步時間
[root@k8s-01 /]# 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
在下面再添加一行,不能是127.0.0.1,切記
# 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 iburst
6、啟動ntp服務並再次查看狀態
[root@k8s-01 /]# systemctl start ntpd [root@k8s-01 /]# systemctl status ntpd ● ntpd.service - Network Time Service Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled) Active: active (running) since Thu 2019-07-18 15:07:51 CST; 6s ago Process: 44578 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS) Main PID: 44579 (ntpd) Memory: 820.0K CGroup: /system.slice/ntpd.service └─44579 /usr/sbin/ntpd -u ntp:ntp -g Jul 18 15:07:51 k8s-01 ntpd[44579]: Listen normally on 2 lo 127.0.0.1 UDP 123 Jul 18 15:07:51 k8s-01 ntpd[44579]: Listen normally on 5 docker0 172.17.0.1 UDP 123 Jul 18 15:07:51 k8s-01 ntpd[44579]: Listen normally on 6 lo ::1 UDP 123 Jul 18 15:07:51 k8s-01 ntpd[44579]: Listen normally on 7 eth0 fe80::e5d0:2b73:64b:3000 UDP 123 Jul 18 15:07:51 k8s-01 ntpd[44579]: Listening on routing socket on fd #24 for interface updates Jul 18 15:07:51 k8s-01 ntpd[44579]: 0.0.0.0 c016 06 restart Jul 18 15:07:51 k8s-01 ntpd[44579]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM Jul 18 15:07:51 k8s-01 ntpd[44579]: 0.0.0.0 c011 01 freq_not_set
7、查看是否同步
[root@k8s-01 /]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
localhost .INIT. 16 l - 512 0 0.000 0.000 0.000
8、設置開機啟動
[root@k8s-01 /]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
9、防火牆設置參考開頭的引用鏈接
二、客戶端配置
前4步與服務端配置一致
5、修改配置文件,將剛剛搭建好的NTP服務器作為客戶端上游時間服務器
[root@k8s-02 /]# 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 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 192.168.0.11 iburst restrict 192.168.0.11 nomodify notrap noquery
192.168.0.11 為服務端的ip,根據實際情況替換
a:注釋掉其他上游時間服務器
b:配置上游時間服務器為本地的ntpd Server服務器
c:配置允許上游時間服務器主動修改本機的時間
6、與本地ntpd Server同步一下
[root@k8s-02 /]# ntpdate -u 192.168.0.11
18 Jul 16:06:07 ntpdate[44317]: adjust time server 192.168.0.11 offset -0.000050 sec
7、啟動ntp服務
[root@k8s-02 /]# systemctl start ntpd
8、設置開機啟動
[root@k8s-02 /]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
9、查看狀態
[root@k8s-02 /]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*192.168.0.11 LOCAL(0) 6 u 6 64 377 0.195 -0.002 0.021
10、防火牆設置參考開頭的引用鏈接
結束