如何在linux命令行查询时间服务器
如何在linux命令行查询时间服务器详细介绍
在 Linux 命令行查询时间服务器的方法有很多种,下面为你介绍几种常用的方式。
1. 使用
timedatectl 命令(推荐)
这个命令可以查看系统当前的时间设置,其中就包含了 NTP(网络时间协议)服务器的信息。
bash
timedatectl
要是你只需要查看 NTP 服务器地址,可使用下面的命令:
bash
timedatectl show NTP Servers
2. 查看配置文件
NTP 服务器的配置信息一般存储在 /etc/ntp.conf 或者 /etc/systemd/timesyncd.conf 文件中。
bash
/etc/chrony.conf ^server
/etc/systemd/timesyncd.conf ^NTP
3. 使用
ntpq 命令(需要安装 ntp 包)
如果你已经安装了 NTP 服务,就可以用这个命令来查询当前同步的时间服务器。
bash
ntpq
ntpq lpeers
4. 使用
chronyc 命令(针对 chrony 服务)
CentOS 7 及后续版本,还有 Ubuntu 16.04 及后续版本,默认使用 chrony 作为 NTP 客户端。
bash
chronyc sources
chronyc tracking
5. 检查正在运行的服务
通过查看正在运行的服务,能确定系统使用的是哪种 NTP 客户端。
bash
systemctl status chronyd
systemctl status ntpd
systemctl status systemd-timesyncd
示例输出
下面是 timedatectl 和 chronyc sources 的示例输出:
bash
Local time: Mon -05-06 :30:45 CST
Universal time: Mon -05-06 07:30:45 UTC
RTC time: Mon -05-06 07:30:45
Time zone: Asia/Shanghai CST, +0800
System clock synchronized:
NTP service: active
NTP servers: .pool.ntp.org .pool.ntp.org .pool.ntp.org .pool.ntp.org
Number of sources
MS Name/IP address Stratum Poll Reach LastRx Last sample
^* time.cloudflare.com -42us -42us +/- 135ms
^+ ntp.aliyun.com -20us -20us +/- 96ms
^- ntp1.aliyun.com -40us -40us +/- 96ms
^? :3200:1::1 - +0ns +0ns +/- 0ns
99123456789101112138008 Local time: Mon :30:45 CST Universal time: Mon :30:45 UTC RTC time: Mon :30:45 Time zone: Asia/Shanghai (CST, +)System clock synchronized: NTP : active NTP servers: .pool.ntp.org .pool.ntp.org .pool.ntp.org .pool.ntp.org
Number of sources = MS Name/IP address Stratum Poll Reach LastRx Last sample===============================================================================^* time.cloudflare.com [ ] +/- 135ms^+ ntp.aliyun.com [ ] +/- 96ms^- ntp1.aliyun.com [ ] +/- 96ms^? :3200:1::1 +0ns[ +0ns] +/- 0ns
根据你的系统配置,选择合适的方法来查询时间服务器。