一. 為什么需要NTP
· 為了讓服務(wù)器更安全
· 為了讓時間同步更準確
二. 時間同步軟件
NTP(Network Time Protocol,網(wǎng)絡(luò)時間協(xié)議)是用來使網(wǎng)絡(luò)中的各個計算機時間同步的一種協(xié)議。它的用途是把計算機的時鐘同步到世界協(xié)調(diào)時UTC,其精度在局域網(wǎng)內(nèi)可達0.1ms,在互聯(lián)網(wǎng)上絕大多數(shù)的地方其精度可以達到1-50ms。
NTP服務(wù)器就是利用NTP協(xié)議提供時間同步服務(wù)的
· NTP(CentOS6)
· Chrony (CentOS7)
二. 部署NTP服務(wù)器
1. 安裝NTP服務(wù)
[root@node01 ~]# yum -y install ntp
2. 修改NTP配置文件
[root@node01 ~]# mv /etc/ntp.conf /etc/ntp.conf.ori
[root@node01 ~]# cat >/etc/ntp.conf << EOF
> driftfile /var/lib/ntp/drift
>
> restrict default nomodify
> restrict 172.16.1.0/24 192.168.10.0/24
>
> restrict ::1
>
> server ntp.aliyun.com iburst
> server ntp.aliyun.com iburst
> server ntp.aliyun.com iburst
> server ntp.aliyun.com iburst
> EOF
注:其中nomodify(表示客戶端可以同步) notrap nopeer noquery(表示安全,開啟后不能使用ntpstat ntpq -p這兩個命令)
3. 關(guān)閉chronyd
[root@node01 ~]# systemctl stop chronyd
[root@node01 ~]# systemctl disable chronyd
4. 啟動NTP服務(wù)
[root@node01 ~]# systemctl start ntpd.service
[root@node01 ~]# systemctl status ntpd.service
5. 測試NTP服務(wù)
[root@node01 ~]# ntpq -p #查看NTP服務(wù)器和哪些上游服務(wù)器同步時間
remote refid st t when poll reach delay offset jitter
==============================================================================
*203.107.6.88 10.137.38.86 2 u 4 64 3 32.120 38.238 1.300
st:即stratum階層,值越小表示ntp serve的精準度越高
when:幾秒前曾做過時間同步更新的操作
Poll表示,每隔多少毫秒與ntp server同步一次
reach:已經(jīng)向上層NTP服務(wù)器要求更新的次數(shù)
delay:網(wǎng)絡(luò)傳輸過程鐘延遲的時間
offset:時間補償?shù)慕Y(jié)果
jitter:Linux系統(tǒng)時間與BIOS硬件時間的差異時間
==============================================================================
[root@node01 ~]# ntpstat # 顯示本機上一次和服務(wù)器同步時間的情況
synchronised to NTP server (203.107.6.88) at stratum 3
time correct to within 1005 ms
polling server every 64 s
四. 部署Chronyc服務(wù)器
1. 查看是否安裝chrony服務(wù)
[root@node01 ~]# rpm -qa chrony
chrony-3.4-1.el7.x86_64
2. 修改chronyc配置文件
[root@node01 ~]# vim /etc/chrony.conf
3. 運行程序
[root@node01 ~]# systemctl start chronyd
[root@node01 ~]# systemctl status chronyd
4. 測試
[root@node01 ~]# systemctl stop ntpd
[root@node01 ~]# chronyc sources -v
210 Number of sources = 2
.-- Source mode '^' = server, '=' = peer, '#' = local clock.
/ .- Source state '*' = current synced, ' ' = combined , '-' = not combined,
| / '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
|| .- xxxx [ yyyy ] /- zzzz
|| Reachability register (octal) -. | xxxx = adjusted offset,
|| Log2(Polling interval) --. | | yyyy = measured offset,
|| \ | | zzzz = estimated error.
|| | | \
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^ 120.25.115.20 2 6 167 26 2845us[ 2845us] /- 21ms
^* 203.107.6.88 2 6 77 28 -2116us[-5914us] /- 29ms
客戶端
[root@web01 ~]# ntpdate 10.0.0.61
21 Jun 18:47:08 ntpdate[6529]: step time server 10.0.0.61 offset 63.273886 sec
6. 幾個命令
chronyc sources -v - 查看時間同步源
chronyc sourcestats -v - 查看時間同步源狀態(tài)
imedatectl set-ntp yes - 啟用NTP時間同步
cronyc tracking - 顯示系統(tǒng)時間信息
chronyc accheck - 檢查NTP訪問是否對特定主機可用
chronyc activity - 該命令會顯示有多少NTP源在線/離線
chronyc add server - 手動添加一臺新的NTP服務(wù)器。
chronyc clients - 在客戶端報告已訪問到服務(wù)器
chronyc delete - 手動移除NTP服務(wù)器或?qū)Φ确?wù)器
chronyc settime - 手動設(shè)置守護進程時間
timedatectl set-local-rtc 1 設(shè)置硬件時間,硬件時間默認為UTC