#Centos7手动时间同步NTP
使用网络工具来查看端口号,服务端安装NTP服务后,会与chrony的123端口冲突,需要重启
NTP服务解决
环境
Centos7服务端:192.168.10.1
Centos7客户端:192.168.10.2
##服务端配置
安装NTP服务:
yum install ntp -y #安装NTP服务
加入到开机自启并重启
systemctl enable ntpd
systemctl restart ntpd
修改配置文件:
vim /etc/ntp.conf 注释掉以下四行
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
在底下写入
```
#外部时间服务器不可用时,以本地时间作为时间服务
server 127.127.1.0 #local clock
fudge 127.127.1.0 stratum 10
```
3、启动NTP服务器
```
systemctl restart ntpd
systemctl enable ntpd
检查时间服务
[root@1b01 ~]# ntpstat
synchronised to local net at stratum 11
time correct to within 3948 ms
polling server every 64 s
[root@localhost ~]# ntpq -p #查看状态
###### remote refid st t when poll reach delay offset jitter
*LOCAL(0) .LOCL. 10 l 33 64 17 0.000 0.000 0.000
```
4、客户端时间同步测试
```
vim /etc/ntp.conf #编辑配置文件
#注释掉下面四行
#写入服务器IP地址
```
![客户端配置][1]
[1]: https://panfeng-biji.oss-cn-beijing.aliyuncs.com/img/image-20210701171210081.png
#NTP定时计划
```
vim /etc/crontab #编写配置文件
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
*/1 * * * * /usr/sbin/ntpdate 192.168.10.1 >> /1.txt
每分钟 路径 服务器IP #将日志重定向到1.txt
#路径可以使用whereis ntpdate找到
```

同步
```
[root@localhost ~]# ntpq -p
remote refid st t when poll reach delay offset jitter==============================================================================*192.168.10.2 LOCAL(0) 11 u 20 64 1 0.228 0.122 0.025
#出现*号为同步成功
手动同步
[root@localhost 123]# ntpdate 192.168.10.2
2 Jul 01:39:24 ntpdate[3409]: adjust time server 192.168.10.2 offset -0.000079 sec
```
出现套字节问题的解决办法
客户端不能安装ntp服务,如果安装了ntp和ntpdate会占用一个端口号,手动同步时间的时候就会出现
套字节,可以选择关掉ntp服务,然后重新手动同步时间就可以了
```
[root@localhost 123]# ntpdate 192.168.10.2
2 Jul 01:38:33 ntpdate[3383]: the NTP socket is in use, exiting
报错正在使用
yum install net-tools #安装网络工具包
systemctl stop ntpd.service #关闭ntpd.service
[root@localhost 123]# netstat -tunlp #查看进程
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 950/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1113/master
tcp6 0 0 :::22 :::* LISTEN 950/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1113/master
udp 0 0 0.0.0.0:39393 0.0.0.0:* 2611/local
```
效果

```
date -s "01:11" 修改指定时间使其同步
```
##出现套字节处理方法
第一步:过滤ntp进程号
```
ps -ef | grep ntp
```
第二步:杀掉ntp对应的pid
kill -9 1872
最后修改:2021 年 10 月 08 日
© 允许规范转载