如何在Debian 9 Linux上设置NTP服务器和客户端

安装NTP服务器

# apt install ntp

安装后,确认NTP服务器已启动并运行:

# systemctl status ntp
● ntp.service - LSB: Start NTP daemon
   Loaded: loaded (/etc/init.d/ntp; generated; vendor preset: enabled)
   Active: active (running) since Fri 2015-06-16 10:19:45 AEST; 3s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1259 ExecStop=/etc/init.d/ntp stop (code=exited, status=0/SUCCESS)
  Process: 1308 ExecStart=/etc/init.d/ntp start (code=exited, status=0/SUCCESS)
    Tasks: 2 (limit: 4915)
   CGroup: /system.slice/ntp.service
           └─1318 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 108:112

配置NTP服务器

NTP服务器的默认配置文件是/etc/ntp.conf

默认的NTP服务器配置依赖于debian.pool.ntp.org服务器组:

pool 0.debian.pool.ntp.org iburst
pool 1.debian.pool.ntp.org iburst
pool 2.debian.pool.ntp.org iburst
pool 3.debian.pool.ntp.org iburst

根据位置,我们可以将上述NTP服务器组进行编辑到较近位置的NTP服务器。
或者,使用国家/地区特定的pool.ntp.org子组。

例如,将NTP服务器子组设置为美国。
编辑“/etc/ntp.conf”:

pool 0.us.pool.ntp.org iburst
pool 1.us.pool.ntp.org iburst
pool 2.us.pool.ntp.org iburst
pool 3.us.pool.ntp.org iburst

还可以对NTP服务器的访问权限限制为特定的LAN客户端。

例如,通过在“/etc/ntp.conf”中添加下面行,服务器将限制NTP仅使用掩码10.0.0.0网络,使用掩码`255.0.0.0。

restrict 10.0.0.0 mask 255.0.0.0 nomodify notrap

在我们对“/etc/ntp.conf”的更改后, 重新启动NTP服务器:

# systemctl restart ntp

设置开机自启动

# systemctl enable ntp
ntp.service is not a native service, redirecting to systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable ntp

客户端配置

首先需要安装ntpdate包:

# apt install ntpdate

安装后,尝试查询我们的NTP服务器。

假设我们的NTP服务器是通过 ntp.onitroad.com解析的:

# ntpdate ntp.onitroad.com
6 Jun 10:59:05 ntpdate[11190]: adjust time server 10.1.1.125 offset -0.005883 sec

然后编辑/etc/default/ntpdate文件,让客户端永久使用新的NTP服务器。

日期:2020-07-07 20:56:36 来源:oir作者:oir