一、离线环境安装ntpd服务端
#在可连外网的服务器下载ntp相关的软件包
yum install --downloadonly --downloaddir=/home/ntp ntp
#离线安装
rpm -ivh autogen-libopts-5.18-5.el7.x86_64.rpm
rpm -ivh ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm
rpm -ivh ntp-4.2.6p5-29.el7.centos.2.x86_64.rpm
#修改配置
#允许所有机器与之同步
restrict 127.0.0.1
restrict ::1
#当远程服务器不可访问时,与本机同步。需添加配置,否则远程连接会以下的报错
server 127.127.1.0
fudge 127.127.1.0 stratum 8
#启动
systemctl start ntpd
systemctl enable ntpd
systemctl status ntpd
#修改时区
[root@caorui ~]# date
Mon Jan 16 05:56:46 UTC 2023
[root@caorui ~]# timedatectl set-timezone Asia/Shanghai
[root@caorui ~]# date
Mon Jan 16 13:59:25 CST 2023
#模拟修改时间
[root@caorui ~]# date -s '2 day ago' #修改时间为两天前
Sat Jan 14 14:04:06 CST 2023
[root@caorui ~]# date
Sat Jan 14 14:04:09 CST 2023
二、安装客户端并进行时间同步
1、客户端进行时间同步(centos7):
#安装ntpdate
[root@xuchen-test-2 ~]#rpm -ivh /tmp/ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm
[root@xuchen-test-2 ~]#date
Mon Jan 16 14:30:17 CST 2023
[root@xuchen-test-2 ~]#ntpdate 10.14.32.182
14 Jan 14:30:25 ntpdate[2623]: step time server 10.14.32.182 offset -172801.921393 sec
[root@xuchen-test-2 ~]#date
Sat Jan 14 14:30:27 CST 2023
2、客户端进行时间同步(ubuntu):
#安装ntpdate和ntp
nuctech@nuctech:~$ sudo apt install ntpdate
nuctech@nuctech:~$ sudo apt install ntp
nuctech@nuctech:~$ date
Sat Jan 16 15:18:24 CST 2023
nuctech@nuctech:~$ sudo ntpdate 10.14.32.182
14 Jan 15:18:26 ntpdate[6642]: step time server 10.14.32.182 offset -172799.949730 sec
nuctech@nuctech:~$ date
Sat Jan 14 15:18:30 CST 2023
三、报错记录
#报错1:
[root@xuchen-test-1 ntp]#ntpdate 10.14.32.182
16 Jan 14:12:54 ntpdate[13865]: no server suitable for synchronization found #报错,服务端必须已经进行了自身同步
[root@xuchen-test-1 ntp]#ntpdate 10.14.32.182
14 Jan 14:14:01 ntpdate[16585]: step time server 10.14.32.182 offset -172799.971843 sec
#报错2:
nuctech@nuctech:~$ sudo ntpdate 10.14.32.182
16 Jan 15:16:09 ntpdate[6488]: the NTP socket is in use, exiting
#客户端的ntpd服务在使用,关闭即可
nuctech@nuctech:~$ ps -ef | grep ntpd
ntp 5722 1 0 15:11 ? 00:00:00 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 117:122
nuctech 6592 4998 0 15:17 pts/1 00:00:00 grep --color=auto ntpd
nuctech@nuctech:~$ sudo kill -9 5722