一、ntp简介
二、简单的几个命令
1.查看当前时区
[root@lan-test ~]# timedatectl
Local time: Thu 2019-11-28 15:33:44 CST
Universal time: Thu 2019-11-28 07:33:44 UTC
RTC time: Thu 2019-11-28 07:33:44
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
2.列出所有时区
[root@lan-test ~]# timedatectl list-timezones
3.设置时区
[root@lan-test ~]# timedatectl set-timezone Asia/Shanghai
4.设置时间
[root@lan-test ~]# timedatectl set-time '2019-11-28 15:40:00'
[root@lan-test ~]# date
Thu Nov 28 15:40:02 CST 2019
5.配置文件
/etc/ntp.conf
restrict | [IP地址] | mask | [掩码] | parameter |
---|
parameter的参数如下
parameter | 说明 |
---|---|
ignore | 拒绝所有类型的连接 |
nomodify | 客户端不能使用ntpc与ntpq这两个程序来修改服务器的时间参数,但客户端可通过该主机来进行网络校时 |
noquery | 客户端不能够使用ntpc、ntpq等命令来查询时间服务器,等于不提供NTP的网络校时 |
notrap | 不提供trap这个远程事件登录的功能 |
notrust | 拒绝没有认证的客户端 |
例如:
restrict | 192.168.1.0 | mask | 255.255.255.0 | 留空 |
---|
表示允许192.168.1.0/24网段的主机同步且不受任何限制
server 0.rhel.pool.ntp.org iburst上层服务器的地址,本ntp服务器的时间是同步上层服务器的,iburst表示当初始同步请求时,采用突发方式接连发送8个报文,时间间隔为2秒。
6.查询上层服务器的状态
[root@lan-test ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
tock.ntp.infoma .STEP. 16 u 39 64 0 0.000 0.000 0.000
+ntp7.flashdance 192.36.143.130 2 u 11 64 1 220.382 -4.645 3.127
+a.ams.pobot.net 17.253.34.125 2 u 6 64 1 202.727 -12.523 2.083
*stratum2-1.ntp. 89.175.20.7 2 u 7 64 1 120.931 3.127 0.163
字段 | 说明 |
---|---|
remote | 上层ntp服务器主机名 |
*代表正在使用的上层ntp服务器 | |
+代表连接成功,可提供时间更新的候补服务器 | |
-该NTP服务器被认为不合格 | |
refid | 上层ntp服务器的ip |
st | 上级NTP服务器的层级,0-15 |
t | 连接的方式 |
l local (such as a GPS clock) | |
u unicast (this is the common type) (单播,即NTP client向NTP server发送NTP请求,NTP server回复的模式) | |
m multicast (多播,可跨子网。) | |
b broadcast (广播,不可跨子网。NTP server定时向广播地址发送NTP包,NTP client通过广播地址获取NTP包,同步本地时钟) | |
when | 几秒钟前曾做过时间同步化 |
poll | 下一次更新在几秒钟之后 |
reach | 八进制数,已经向上层服务器要求更新的次数 |
delay | 网络传输过程中的延迟时间 |
offset | 本地和服务器之间的时间差别,越接近0,说明和服务器的时间约接近 |
jitter | 系统时间与硬件时间的差异 |
三、搭建ntp服务器
1.装包
[root@lan-test ~]# yum install ntp -y
2.防火墙放行服务
[root@lan-test ~]# firewall-cmd --add-service=ntp --per
[root@lan-test ~]# firewall-cmd --reload
3.编辑配置文件
[root@lan-test ~]# vim /etc/ntp.conf
restrict 192.168.1.0 mask 255.255.255.0 //限制当前网段的主机可以同步时间
server 127.127.1.0 //上层服务器为自己,则需注释掉上层服务器
fudge 127.127.1.0 stratum 8 //虚拟自己在第八层
4.重启服务
[root@lan-test ~]# systemctl restart ntpd
5.客户端查询上层服务器状态
[root@server ~]# ntpdate -q 192.168.1.206
server 192.168.1.206, stratum 9, offset 0.000031, delay 0.02629
28 Nov 15:50:38 ntpdate[4032]: adjust time server 192.168.1.206 offset 0.000031 sec
6.客户端同步上层服务器的时间
[root@server ~]# ntpdate 192.168.1.206
28 Nov 15:51:04 ntpdate[4033]: adjust time server 192.168.1.206 offset 0.000026 sec
7.设置定时同步时间,每天16:18分同步时间
[root@server ~]# crontab -e
18 16 * * * /usr/sbin/ntpdate 192.168.1.206 > /dev/null 2>&1