欢迎光临
我们一直在努力

linuxntp服务器配置

Linux NTP服务器

NTP(Network Time Protocol,网络时间协议)是一种用于同步计算机系统时间的协议,在分布式系统中,为了保证各个节点的时间一致性,通常需要一个时间服务器来提供统一的时间标准,Linux系统中,可以通过搭建NTP服务器来实现这一功能。

安装NTP服务

在Linux系统中,可以使用以下命令安装NTP服务:

sudo apt-get update
sudo apt-get install ntp

配置NTP服务器

1、编辑NTP配置文件

NTP服务器的主配置文件位于/etc/ntp.conf,可以使用文本编辑器打开并编辑该文件,使用vi编辑器:

sudo vi /etc/ntp.conf

2、配置NTP服务器的上游时间源

/etc/ntp.conf文件中,可以找到如下一行:

pool.ntp.org maps to about 1000 low-stratum NTP servers.  Your server will
pick a different set every time it starts up.  Please consider joining the
pool: <http://www.pool.ntp.org/join.html>
server 0.pool.ntp.org iburst

这行配置表示,NTP服务器会从pool.ntp.org这个上游时间源获取时间,你可以根据需要添加更多的上游时间源,

server 192.168.1.1 iburst
server 192.168.1.2 iburst

192.168.1.1192.168.1.2是两个局域网内的NTP服务器地址。iburst选项表示,当与上游时间源的连接质量较差时,可以暂时提高传输速率以提高同步精度。

3、配置NTP服务器为本机提供时间服务

/etc/ntp.conf文件中,添加以下内容:

Local clock. This is used by the hardware clock on Linux systems with the hwclock driver, or if no hardware clock is present, by the system clock (e.g., RTC). The following lines assume that your hardware clock is set to local time (either via the --utc option to ntpd or via hwclock --localtime). If you want to run ntpd as a daemon and have it adjust the system clock, you should instead use the "tick" or "tock" options below with the driftfile specified in the ntpdc line above. For more information on these options, see the ntpd man page (available by running "man ntpd"). Note that the hardware clock on some systems can be set independently of the software clock; on such systems, this option does nothing and can be safely removed. local stratum 10 watch refid /var/run/ntpdate driftfile /var/lib/ntp/drift file peerstats file /var/lib/ntp/peerstats poll peers file /var/lib/ntp/peers statsdir /var/log/ntp stats loopstats file /var/lib/ntp/loopstats peerstats_maxpoll 10 poll interval 6 minpoll 4 maxpoll 10

local stratum 10表示本机的NTP层级为10,watch refid /var/run/ntpdate表示监控/var/run/ntpdate文件,以便在启动时更新系统时间,其他选项可以根据需要进行配置。

启动NTP服务并设置开机自启动

1、启动NTP服务:

sudo systemctl start ntp

2、设置开机自启动:

sudo systemctl enable ntp

测试NTP服务同步效果

可以使用以下命令查看本机的时间同步状态:

ntpq -pn

如果显示类似以下内容,说明NTP服务已经成功同步:

remote           refid      st t when poll reach   delay   offset  jitter

*LOCAL(0)        10.0.0.1    3 u   36   64  377    0.555   0.000   0.555 -0.005
+CHANNEL(0)     192.168.1.1 u   36   64  377    0.555   0.000   0.555 -0.005 +/0.999 ms[root@localhost ~] ntpq -pn remote           refid      st t when poll reach   delay   offset  jitter===================================================================================*LOCAL(0)        10.0.0.1    3 u   36   64  377    0.555   0.000   0.555 -0.005+CHANNEL(0)     192.168.1.1 u   36   64  377    0.555   0.000   0.555 -0.005 +/0.999 ms[root@localhost ~] ntpq -pn remote           refid      st t when poll reach   delay   offset  jitter===================================================================================*LOCAL(0)        10.0.0.1    3 u   36   64  377    0.555   0.000   0.555 -0.005+CHANNEL(0)     192.168.1.1 u   36   64  377    0.555   0.000   0.555 -0.005 +/0.999 ms[root@localhost ~] ntpq -pn remote           refid      st t when poll reach   delay   offset  jitter===================================================================================*LOCAL(0)        10.0.0.1    3 u   36   64  377    0.555   0.000   0.555 -0.005+CHANNEL(0)     192
未经允许不得转载:九八云安全 » linuxntp服务器配置