通过命令行获取远程时间,使用 TZ 将其设置为本地时钟

通过命令行获取远程时间,使用 TZ 将其设置为本地时钟

我正在试验时钟。在本地盒子上更改了它。之后,Google 和其他 SSL 网站停止工作,所以我无法获得准确的当地时间(我不戴手表)。幸运的是,手机在另一个房间,所以我拿到了它,然后设置了时间。

但是自动化怎么办?当我所在的数据中心不允许使用电话时怎么办?有没有命令行方式来查询非 SSL 远程位置以获取 UTC 时间?

Cherry 是一款可以获取当地时区并设置当地时间的工具,但我也能做到,只会偏离几秒钟……但可以通过反复调整

date -s "xxx"呼叫直到准确为止。

我在看如何使用 curl 命令获取日期和时间?,但是当我使用时ntpq -c 'rv 0 clock' 139.59.15.185,我得到以下输出:

139.59.15.185: timed out, nothing received

***Request timed out

和命令:

ntpq -c 'rv 0 clock' ntp.ubuntu.com

输出:

ntp.ubuntu.com: timed out, nothing received

***Request timed out

以下是 的内容sudo nano /etc/ntp.conf

# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift

# Leap seconds definition provided by tzdata leapfile /usr/share/zoneinfo/leap-seconds.list

# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats filegen loopstats file
loopstats type day enable filegen peerstats file peerstats type day
enable filegen clockstats file clockstats type day enable

# Specify one or more NTP servers.

# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information. pool 0.ubuntu.pool.ntp.org iburst pool 1.ubuntu.pool.ntp.org iburst pool 2.ubuntu.pool.ntp.org iburst pool 3.ubuntu.pool.ntp.org iburst

# Use Ubuntu's ntp s...

答案1

ntpq命令用于查询 NTP 服务器的状态和配置,而不是查询时间。服务器可能不允许从随机客户端查询其状态,因此您会遇到超时。要从远程服务器查询时间,请使用ntpdate命令:

ntpdate -q ntp.ubuntu.com

将显示本地系统和ntp.ubuntu.com服务器之间的时间差,例如:

15 Jul 18:50:09 ntpdate[25028]: adjust time server 91.189.89.198 offset -0.139723 sec

要将本地时间设置为服务器公布的时间ntp.ubuntu.com,请使用:

sudo ntpdate ntp.ubuntu.com

(需要“sudo”是因为设置系统时间需要 root 权限)

相关内容