NTP/chrony—时间服务器
NTP:Network Time Protocol
服务端:10.0.0.120
客户端10.0.0.121
- 协议端口:123
- 主配置文件:/etc/ntp.conf
服务端配置
1、selinux跟firewalld 关掉
2、IP配置为静态,配置主机名,双机正常通信
3、配置国内的yum源,比如阿里云
4、安装基本命令, vim netstat unzip wget
1.安装软件
# yum install ntp -y
2.配置
# vim /etc/ntp.conf
server 127.127.1.0 #添加上这行,将本机作为本地时间服务器
3.启动服务
# systemctl restart ntpd #启动服务
# systemctl enable ntpd #开机启动
4.查看服务运行情况
# ntpq -p
remote refid st t when poll reach delay offset jitter
==========================================================
*LOCAL(0) .LOCL. 5 l 35 64 7 0.000 0.000 0.000
客户端配置
1、selinux跟firewalld 关掉
2、IP配置为静态,配置主机名,双机正常通信
3、配置国内的yum源,比如阿里云
4、安装基本命令, vim netstat unzip wget
客户端:
1.安装软件
[root@smx02 ~]# yum install ntpdate -y
2.同步时间
[root@smx02 ~]# ntpdate 10.0.0.120
15 Jan 19:32:40 ntpdate[7946]: adjust time server 10.0.0.120 offset 0.008265 sec
要实现自动同步,可结合计划任务来实现
[root@smx02 ~]# crontab -l
* 12 * * * ntpdate 10.0.0.120 # 每天12点同步时间
chrony
软件名:chrony #服务器和客户端都安装
服务名:chronyd
配置文件: /etc/chrony.conf
服务端配置
[root@smx ~]# vim /etc/chrony.conf # 安装软件
# Allow NTP client access from local network.
allow 10.0.0.121 #允许哪些客户端同步时间
[root@smx ~]# systemctl restart chronyd # 重启服务
[root@smx ~]# systemctl enable chronyd # 开机启动服务
[root@smx ~]# chronyc sources # 查看服务器资源状态
210 Number of sources = 4
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^- 139.199.215.251 2 6 71 47 -12ms[ -12ms] +/- 49ms
^* dns2.synet.edu.cn 1 6 33 51 +284us[ -144us] +/- 33ms
^- tick.ntp.infomaniak.ch 1 6 207 44 -9423us[-9423us] +/- 117ms
^- ntp8.flashdance.cx 2 6 17 53 -11ms[ -12ms] +/- 129ms
客户端配置
[root@smx02 ~]# vim /etc/chrony.conf
server 10.0.0.120 # 添加一行配重
[root@smx02 ~]# systemctl restart chronyd
[root@smx02 ~]# systemctl enable chronyd
[root@smx02 ~]# chronyc sources -v
210 Number of sources = 5
.-- 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
===============================================================================
^? 10.0.0.120 2 6 1 10 +8166us[+7290us] +/- 38ms
^* 111.230.189.174 2 6 17 2 -380us[-2524us] +/- 44ms
^- makaki2.miuku.net 3 6 17 3 +54ms[ +52ms] +/- 134ms
^? tick.ntp.infomaniak.ch 1 6 10 8 -667us[-1543us] +/- 118ms
^? stratum2-1.ntp.mow01.ru.> 2 6 3 3 -5622us[-7767us] +/- 65ms
查看时间同步状态
[root@smx02 ~]# timedatectl status
Local time: Mon 2024-01-15 19:42:20 CST
Universal time: Mon 2024-01-15 11:42:20 UTC
RTC time: Mon 2024-01-15 11:42:20
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
开启网络时间同步
[root@smx02 ~] # timedatectl set-ntp true