在红帽企业版 Linux 7 上
不幸的是 ntptimeset 在 Red Hat Enterprise Linux 中不可用,所以我们必须尝试其他方法
现在的日期和时间
# date Thu Dec 7 10:25:03 IST 2015
NTPD服务状态
# systemctl status ntpd ● ntpd.service - Network Time Service Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled) Active: active (running) since Wed 2015-12-06 17:43:02 IST; 16h ago Process: 5957 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS) Main PID: 5958 (ntpd) CGroup: /system.slice/ntpd.service └─5958 /usr/sbin/ntpd -u ntp:ntp -g Dec 06 17:43:02 Ban17-pgw01-a systemd[1]: Starting Network Time Service... Dec 06 17:43:02 Ban17-pgw01-a ntpd[5957]: ntpd 4.2.6p5@1.2349-o Wed Mar 1 09:00:51 UTC 2015 (1) Dec 06 17:43:02 Ban17-pgw01-a systemd[1]: Started Network Time Service. Dec 06 17:43:02 Ban17-pgw01-a ntpd[5958]: proto: precision = 0.038 usec Dec 06 17:43:02 Ban17-pgw01-a ntpd[5958]: 0.0.0.0 c01d 0d kern kernel time sync enabled
现在让我们更改日期和时间
# date --set "4 Dec 2015 10:00:00" Mon Dec 4 10:00:00 IST 2015
首先停止ntpd服务
# systemctl stop ntpd
接下来使用以下命令从 NTP 服务器启动强制同步
# ntpdate 192.168.10.100 7 Dec 10:27:36 ntpdate[6701]: step time server 192.168.10.100 offset 260769.003401 sec
现在再次启动NTP服务
# systemctl start ntpd
最后验证本地主机日期和时间是否正确更新
# date Thu Dec 7 10:27:48 IST 2015
重要说明:如果我们不想停止 ntp 服务器进行同步,请使用“-u”和 ntpdate
-u Direct ntpdate to use an unprivileged port for outgoing packets. This is most useful when behind a firewall that blocks incoming traffic to privileged ports, and you want to synchronize with hosts beyond the firewall. Note that the -d option always uses unprivi‐ leged ports. 指示 ntpdate 将非特权端口用于传出数据包。 当在防火墙后面阻止特权端口的传入流量,并且您希望与防火墙之外的主机同步时,这非常有用。 请注意,-d选项始终使用非特权端口。
没有使用 '-u '时并且 NTPD 服务正在运行时
# ntpdate 0.in.pool.ntp.org 10 Nov 17:34:48 ntpdate[2574]: the NTP socket is in use, exiting
使用'-u' , ntpd 在后台运行
# ntpdate -u 0.in.pool.ntp.org 10 Nov 17:35:22 ntpdate[2575]: step time server 13.126.37.14 offset 1.335054 sec
默认情况下,如果 NTP 服务正在运行,它将根据轮询间隔自动从 NTP 服务器同步客户端的日期和时间,但是如果我们想立即进行强制同步并且不能等待 NTP 客户端同步它,该怎么办?
在 SuSE Enterprise Linux 上
我的服务器上的当前日期和时间是
# date Fri Dec 8 02:51:04 IST 2015
ntp服务状态
# /etc/init.d/ntp status remote refid st t when poll reach delay offset jitter ============================================================================== 192.168.10.100 10.171.8.4 3 u 1 64 1 0.173 -593996 0.000 10.40.130.1 .INIT. 16 u - 64 0 0.000 0.000 0.000 Checking for network time protocol daemon (NTPD): running
ntp 客户端正在运行,让我们在强制同步之前更改服务器的日期和时间
# date --set "4 Dec 2015 10:00:00" Mon Dec 4 10:00:00 IST 2015
新的日期和时间
# date Mon Dec 4 10:00:02 IST 2015
是时候进行强制同步了
# /etc/init.d/ntp ntptimeset Time synchronized with 192.168.10.100
可以看到,它立即向 NTP 服务器发送请求,并从 NTP 服务器更新本地主机日期和时间详细信息
# date Thu Dec 7 10:23:35 IST 2015
日期:2020-06-02 22:16:59 来源:oir作者:oir