NTP服务搭建
1.192.168.121.112 (搭建NTP服务)
2.192.168.121.113 (验证-同步NTP服务器的时间)
3.192.168.121.114 (验证-同步NTP服务器的时间)
NTP协议全称网络时间协议(Network Time Procotol),用于在国际互联网上传递统一、标准的时间。
NTP是一个跨越广域网和局域网的复杂同步时间协议,它通常可以获得毫秒级的精度。
NTP协议是OSI参考模型的高层协议,符合UDP传输协议格式,专用端口:123
- ntp安装(在192.。168.121.112服务器上操作)
# 1.查看ntp是否安装
[root@localhost ~]# rpm -qa | grep ntp
fontpackages-filesystem-1.44-8.el7.noarch
ntpdate-4.2.6p5-29.el7.centos.2.x86_64
# 2.安装ntp
[root@localhost ~]# rpm -qa | grep ntp
fontpackages-filesystem-1.44-8.el7.noarch
ntpdate-4.2.6p5-29.el7.centos.2.x86_64
ntp-4.2.6p5-29.el7.centos.2.x86_64
# 3.启动ntp服务
[root@localhost ~]# systemctl start ntpd
# 4.查询网络中的NTP服务器,同时显示客户端和每个服务器的连接状态
[root@localhost ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
tick.ntp.infoma .GPS. 1 u 1 64 1 211.440 -8.970 0.125
stratum2-1.ntp. .INIT. 16 u - 64 0 0.000 0.000 0.000
ntp5.flashdance .INIT. 16 u - 64 0 0.000 0.000 0.000
makaki.miuku.ne .INIT. 16 u - 64 0 0.000 0.000 0.000
- NTP默认使用UDP协议的123端口,开放123端口
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-port=123/udp
[root@localhost ~]# firewall-cmd --state
running
# 确认加入规则
[root@localhost ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client ssh
ports: 5432/tcp 123/udp 8080/tcp 22/tcp
protocols:
- 修改ntp.conf文件
[root@localhost ~]# vi /etc/ntp.conf
#1 把下边这行注释掉
# restrict default nomodify notrap nopeer noquery
#2 注释掉原有的4行server,增加下边的两行,127.127.1.0代表把本机作为时间服务器
server 127.127.1.0
fudge 127.127.1.0 stratum 10
- 重启ntpd
[root@localhost ~]# systemctl restart ntpd
- 在其他服务器上执行
# 手动同步
[root@localhost ~]# ntpdate 192.168.121.112
12 Jan 10:15:16 ntpdate[1766]: adjust time server 192.168.121.112 offset -0.170168 sec
# 查看上层服务器状态
[root@localhost ~]# ntpdate -q 192.168.121.112
server 192.168.121.112, stratum 11, offset -0.097738, delay 0.02705
12 Jan 10:18:26 ntpdate[1768]: adjust time server 192.168.121.112 offset -0.097738 sec
# 监控变化,每两秒刷新一次
[root@localhost ~]# watch ntpdate 192.168.121.112
Every 2.0s: ntpdate 192.168.121.112 Thu Jan 12 10:22:25 2023
12 Jan 10:22:38 ntpdate[1784]: adjust time server 192.168.121.112 offset -0.072616 sec
# crontab定时任务进行时间校准
[root@localhost ~]# vim /etc/crontab
#每30秒同步一次
*/30 * * * * root /usr/sbin/ntpdate 192.168.121.112 > /dev/null 2>&1
功能 | 命令 |
---|
查询网络中的NTP服务器 | ntpq -p |
查看上层服务器状态 | ntpdate -q 192.168.121.112 |
手动同步时间 | ntpdate 192.168.121.112 |
NTP服务器监控 | watch ntpdate 192.168.121.112 |
功能 | 命令 |
---|
查看时间 | date |
查看硬件时间 | hwclok -r |
查看系统所在时区 | date -R |
查看所有时区 | ls /usr/share/zoneinfo 或 timedatectl list-timezones |
查看其他时区的当前时间 | zdump Asia/Shanghai |
修改时区 | timedatectl set-timezone Asia/Shanghai |
保存时间修改 | clock -w |