安裝NTP服務(wù)可以說很容易调煎,幾個要點搞定就成五慈。下面是我裝NTP服務(wù)器的記錄。
一淘讥、首先當然是慣例更新
sudo apt update
sudo apt upgrade
axing@84:~$sudo apt install ntp
Reading state information... Done
The following packages were automatically installed and are no longer required:
libcurses-perl libcurses-ui-perl libpkcs11-helper1 libterm-readkey-perl
Use 'sudo apt autoremove' to remove them.
Suggested packages:
ntp-doc
The following NEW packages will be installed:
ntp
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 640 kB of archives.
After this operation, 1,952 kB of additional disk space will be used.
Get:1 http://gb.archive.ubuntu.com/ubuntu bionic-updates/universe amd64 ntp amd64 1:4.2.8p10+dfsg-5ubuntu7.1 [640 kB]
Fetched 640 kB in 0s (13.1 MB/s)
Selecting previously unselected package ntp.
然后修改下配置文件圃伶。
這里我們建議去找最近的ntp公共服務(wù)器,訪問站點 https://www.ntppool.org/
查找你的區(qū)域蒲列,比如我需要找 Europe/United Kingdom窒朋,可以發(fā)現(xiàn)有這么幾個,記下來要在配置文件里改的蝗岖。
server 0.uk.pool.ntp.org
server 1.uk.pool.ntp.org
server 2.uk.pool.ntp.org
server 3.uk.pool.ntp.org
這里是中國常用的NTP服務(wù)器炼邀,以供參考:
210.72.145.44 (國家授時中心服務(wù)器IP地址)
202.112.10.36 # 1.cn.pool.ntp.org
59.124.196.83 # 0.asia.pool.ntp.org
s2m.time.edu.cn 北京大學
s2c.time.edu.cn 北京郵電大學
二、修改配置文件 /etc/ntp.conf
# Specify one or more NTP servers.
# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information.
#這里常用的選項是prefer - 優(yōu)先主機剪侮, iburst -當服務(wù)器不可用時將發(fā)包檢測
server 0.uk.pool.ntp.org iburst
server 1.uk.pool.ntp.org iburst
server 2.uk.pool.ntp.org iburst
server 3.uk.pool.ntp.org iburst
#pool 0.ubuntu.pool.ntp.org iburst
#pool 1.ubuntu.pool.ntp.org iburst
#pool 2.ubuntu.pool.ntp.org iburst
#pool 3.ubuntu.pool.ntp.org iburst
# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
#訪問網(wǎng)段限制
restrict xx.xxx.xx.0 mask 255.255.255.0 notrust
三拭宁、重啟服務(wù)洛退,查看服務(wù)狀態(tài)
axing@84:~$ sudo service ntp restart
axing@84:~$ sudo service ntp status
● ntp.service - Network Time Service
Loaded: loaded (/lib/systemd/system/ntp.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2019-01-16 16:02:17 GMT; 7s ago
Docs: man:ntpd(8)
Process: 24693 ExecStart=/usr/lib/ntp/ntp-systemd-wrapper (code=exited, status=0/SUCCESS)
Main PID: 24711 (ntpd)
Tasks: 2 (limit: 1110)
CGroup: /system.slice/ntp.service
└─24711 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 113:118
Jan 16 16:02:17 84 ntpd[24711]: Listen normally on 2 lo 127.0.0.1:123
Jan 16 16:02:17 84 ntpd[24711]: Listen normally on 3 ens3 xx.xxx.xxx.xx:123
Jan 16 16:02:17 84 ntpd[24711]: Listen normally on 4 lo [::1]:123
Jan 16 16:02:17 84 ntpd[24711]: Listen normally on 5 ens3 [fe80::5054:ff:febd:720d%2]:123
Jan 16 16:02:17 84 ntpd[24711]: Listening on routing socket on fd #22 for interface updates
Jan 16 16:02:18 84 ntpd[24711]: Soliciting pool server 91.189.89.199
Jan 16 16:02:19 84 ntpd[24711]: Soliciting pool server 91.189.94.4
Jan 16 16:02:20 84 ntpd[24711]: Soliciting pool server 91.189.89.198
Jan 16 16:02:21 84 ntpd[24711]: Soliciting pool server 91.189.91.157
Jan 16 16:02:22 84 ntpd[24711]: Soliciting pool server 2001:67c:1560:8003::c8
四、開放防火墻端口杰标,我們看到ntp使用udp 123端口
axing@84:~$ sudo ufw allow 123/udp
Rule added
Rule added (v6)
現(xiàn)在兵怯,服務(wù)器端就配置完了。 查看與上層ntp服務(wù)器的狀態(tài)
root@84:~# watch ntpq -p
Every 2.0s: ntpq -p C84: Wed Jan 16 16:24:16 2019
remote refid st t when poll reach delay offset jitter
==============================================================================
ntp.ubuntu.com .POOL. 16 p - 64 0 0.000 0.000 0.000
+lond-web-1.spee 87.242.168.84 2 u 60 64 377 1.501 -0.110 0.215
+bode.spiderspac 85.199.214.99 2 u 65 64 377 1.342 -0.112 0.096
*87.242.168.84 ( .UPPS. 1 u 58 64 377 11.249 -0.027 0.079
+ntp1.wirehive.n 195.66.241.2 2 u - 64 377 5.466 -0.073 0.245
-alphyn.canonica 132.246.11.231 2 u 10 64 377 75.283 -3.859 0.057
-pugot.canonical 17.253.52.253 2 u 11 64 377 1.762 0.029 0.150
-chilipepper.can 145.238.203.14 2 u 6 64 377 1.293 0.061 0.057
-golem.canonical 17.253.52.125 2 u 16 64 377 1.288 0.029 0.551
其中這些狀態(tài)的意思是:
remote: 連接的遠程NTP服務(wù)器腔剂;
refid: 提供時間同步的服務(wù)器IP媒区;
st: 遠程服務(wù)器的層級別(stratum)。由于NTP是層型結(jié)構(gòu)掸犬,有頂端的服務(wù)器袜漩、多層的Relay服務(wù)器、再到客戶端湾碎。所以服務(wù)器級別從高到低可以設(shè)定為1-16宙攻。為了減緩負荷和網(wǎng)絡(luò)堵塞,原則上應(yīng)該避免直接連接到級別為1的服務(wù)器介褥;
when: 幾秒鐘前曾經(jīng)做過時間同步更新的動作座掘;
poll: 本地主機和遠程服務(wù)器多少時間進行一次同步(單位:s);
reach: 已經(jīng)向上層NTP服務(wù)器要求更新的次數(shù)柔滔;
delay: 網(wǎng)絡(luò)傳輸過程當中延遲的時間(單位:10^(-6)s溢陪,微秒);
offset: 時間補償?shù)慕Y(jié)果(單位與:10^(-3)s睛廊,毫秒)形真;
jitter: Linux系統(tǒng)時間與BIOS硬件時間的差值(單位:10^(-6)s,微秒)超全。其絕對值越小没酣,主機和對時服務(wù)器的時間就越接近;
*: 遠端的服務(wù)器已經(jīng)被確認為主NTP Server卵迂,系統(tǒng)時間將由這臺機器所提供裕便;
+: 作為輔助的NTP Server,與帶有*號的服務(wù)器一起為我們提供同步服務(wù). 當```*```號服務(wù)器不可用時见咒,它就可以接管偿衰;
-: 遠程服務(wù)器被clustering algorithm認為是不合格的NTP Server;
x: 遠程服務(wù)器不可用改览;
五下翎、linux客戶端配置ntp
5.1先安裝ntpdate,測試NTP工具
axing@THN:~$ sudo apt install ntpdate
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed
ntpdate
0 to upgrade, 1 to newly install, 0 to remove and 0 not to upgrade.
Need to get 51.8 kB of archives.
After this operation, 183 kB of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu bionic-updates/universe amd64 ntpdate amd64 1:4.2.8p10+dfsg-5ubuntu7.1 [51.8 kB]
Fetched 51.8 kB in 0s (162 kB/s)
Selecting previously unselected package ntpdate.
(Reading database ... 92007 files and directories currently installed.)
Preparing to unpack .../ntpdate_1%3a4.2.8p10+dfsg-5ubuntu7.1_amd64.deb ...
Unpacking ntpdate (1:4.2.8p10+dfsg-5ubuntu7.1) ...
Setting up ntpdate (1:4.2.8p10+dfsg-5ubuntu7.1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
測試NTP服務(wù)器是否好用(如有提示“ntpdate[XXX]: the ntp socket is in use, exiting”宝当, 可加參數(shù) -u )
axing@THN:~$ sudo ntpdate xx.xxx.xxx.xx
16 Jan 16:48:51 ntpdate[22307]: adjust time server xx.xxx.xxx.xx offset -0.001725 sec
5.2 很好视事,那么我們先停止ubuntu自帶的時間同步服務(wù)
axing@THN:~$ sudo timedatectl set-ntp off
5.3 安裝ntp服務(wù)并添加主NTP服務(wù)器,可以修改/etc/ntp.cfg庆揩,或者直接使用下面語句添加:
axing@THN:~$ sudo bash -c "echo server xx.xxx.xxx.xx prefer iburst >> /etc/ntp.conf"
#重啟NTP服務(wù)
axing@THN:~$ sudo service ntp restart
看一下同步狀態(tài):
axing@THN:~$ watch ntpq -p
Every 2.0s: ntpq -p THN: Wed Jan 16 17:32:29 2019
remote refid st t when poll reach delay offset jitter
==============================================================================
ntp.ubuntu.com .POOL. 16 p - 64 0 0.000 0.000 0.000
*xx.xxx.xxx.xx xx.xxx.xx.4 3 u 57 64 77 0.825 -0.951 0.706
+ns1.luns.net.uk 158.43.192.66 2 u 58 64 77 8.456 -0.887 0.751
-37.220.20.12 (h 82.69.97.89 2 u 53 64 77 3.795 -3.091 0.580
+time.rdg.uk.as4 87.242.168.84 2 u 56 64 77 2.171 -1.905 0.719
-armcd.co.uk 130.88.212.143 3 u 55 64 77 1.756 -0.599 0.706
-pugot.canonical 140.203.204.77 2 u 60 64 77 1.737 -0.923 0.492
-alphyn.canonica 132.246.11.231 2 u 1 64 177 75.296 -4.481 0.427
-golem.canonical 17.253.52.125 2 u 65 64 77 1.313 -1.000 0.693
-chilipepper.can 193.79.237.14 2 u 3 64 177 1.477 -0.380 0.335
可以看到俐东,已經(jīng)和主服務(wù)器同步成功了跌穗。