CentOS/Redhat Linux:安装OpenNTPD同步本地时钟

时间:2020-01-09 10:42:54  来源:igfitidea点击:

OpenNTPD是Unix系统的守护进程,它实现网络时间协议以将计算机系统的本地时钟与远程NTP服务器同步。
如何在CentOS/RHEL/Fedora Linux而非Linux操作系统提供的默认NTPD客户端/服务器下安装OpenNTPD?
与CentOS/RHEL/Fedora Linux附带的传统NTPD相比,OpenNTPD提供了简单性和安全性。
该软件提供了将本地时钟同步到远程NTP服务器的功能,并且可以充当NTP服务器本身,从而重新分配本地时钟。

OpenNTPD是OpenBSD项目的一部分。
在本教程中,您将学习如何在CentOS/RHEL/Fedora Linux上安装OpenNTPD。

编译并安装OpenNTPD

通过访问此页面下载可移植的OpenNTPD版本,执行:

# cd /tmp
# wget http://ftp3.usa.openbsd.org/pub/OpenBSD/OpenNTPD/openntpd-6.0p1.tar.gz

焦油焦油球:

# tar -zxvf openntpd-6.0p1.tar.gz
# cd openntpd-6.0p1

您必须在CentOS/RHEL 7服务器上安装GNU编译器。
了解如何在CentOS/RHEL 7上安装编译器以编译OpenNTPD。
要使用默认选项编译OpenNTPD,请执行:

# ./configure
# make
# make install

输出示例:

Making install in include
make[1]: Entering directory `/tmp/openntpd-6.0p1/include'
make[2]: Entering directory `/tmp/openntpd-6.0p1/include'
make[2]: Nothing to be done for `install-exec-am'.
make[2]: Nothing to be done for `install-data-am'.
....
..
...
make[2]: Entering directory `/tmp/openntpd-6.0p1/src'
/usr/bin/mkdir -p '/usr/local/sbin'
  /bin/sh ../libtool   --mode=install /usr/bin/install -c ntpd '/usr/local/sbin'
libtool: install: /usr/bin/install -c ntpd /usr/local/sbin/ntpd
make  install-exec-hook
make[3]: Entering directory `/tmp/openntpd-6.0p1/src'
 
WARNING: the privilege separation path is specified as /var/empty, 
          but this directory contains files!
 
Please ensure that /var/empty is empty. If you installed a previous 
OpenNTPD version and created a user with a home directory at /var/empty/ntpd, 
please adjust that user to use /var/empty instead and delete /var/empty/ntpd.
 
make[3]: Leaving directory `/tmp/openntpd-6.0p1/src'
/usr/bin/mkdir -p '/usr/local/share/man/man5'
/usr/bin/install -c -m 644 ntpd.conf.5 '/usr/local/share/man/man5'
/usr/bin/mkdir -p '/usr/local/share/man/man8'
/usr/bin/install -c -m 644 ntpctl.8 ntpd.8 '/usr/local/share/man/man8'
make[2]: Leaving directory `/tmp/openntpd-6.0p1/src'
make[1]: Leaving directory `/tmp/openntpd-6.0p1/src'
make[1]: Entering directory `/tmp/openntpd-6.0p1'
make[2]: Entering directory `/tmp/openntpd-6.0p1'
make  install-exec-hook
make[3]: Entering directory `/tmp/openntpd-6.0p1'
 
/usr/local/etc/ntpd.conf already exists, install will not overwrite
make[3]: Leaving directory `/tmp/openntpd-6.0p1'
make[2]: Nothing to be done for `install-data-am'.
make[2]: Leaving directory `/tmp/openntpd-6.0p1'
make[1]: Leaving directory `/tmp/openntpd-6.0p1'

配置OpenNTPD

执行以下命令来创建_ntp用户和组,以在自己的 Jail中运行OpenNTPD:

# groupadd _ntp
# useradd -g _ntp -s /sbin/nologin -d /var/empty/openntpd -c 'OpenNTP daemon' _ntp
# mkdir -p /var/empty/openntpd
# chown 0 /var/empty/openntpd
# chgrp 0 /var/empty/openntpd
# chmod 0755 /var/empty/openntpd

安装基于systemd的启动脚本

编辑/usr/lib/systemd/system/openntpd.service:

# vi /usr/lib/systemd/system/openntpd.service

添加以下行

[Unit]
Description=OpenNTP Daemon
After=network.target
Conflicts=systemd-timesyncd.service

[Service]
Type=forking
ExecStart=/usr/local/sbin/ntpd -s

[Install]
WantedBy=multi-user.target

保存并关闭文件。

ntpd.conf配置

编辑/usr/local/etc/ntpd.conf,执行:

# vi /usr/local/etc/ntpd.conf

让它在本地主机和主服务器IP上监听:

listen on 127.0.0.1
listen on 10.210.206.58
listen on ::1

同步到一个名为ntp.example.com或者ntp.isp.example.com的服务器:

server ntp.isp.example.com
 
# Or Use a random selection of 8 public stratum 2 servers
servers pool.ntp.org

关闭并保存文件。

启动OpenNTPD

执行以下命令:

$ sudo systemctl start openntpd.service

要停止openntpd,请执行:

$ sudo systemctl stop openntpd.service

要重新启动openntpd,请执行:

$ sudo systemctl restart openntpd.service

验证OpenNTPD是否正常工作

执行以下命令:

$ sudo systemctl status openntpd.service

输出示例:

? openntpd.service - OpenNTP Daemon
   Loaded: loaded (/usr/lib/systemd/system/openntpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2015-11-22 10:20:08 UTC; 3s ago
  Process: 15508 ExecStart=/usr/local/sbin/ntpd -s (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/openntpd.service
           ??15509 ntpd: ntp engine
           ??15510 ntpd: dns engine
           ??15511 /usr/local/sbin/ntpd -s

Nov 22 10:20:07 cenots-db systemd[1]: Starting OpenNTP Daemon...
Nov 22 10:20:07 cenots-db ntpd[15509]: listening on 127.0.0.1
Nov 22 10:20:07 cenots-db ntpd[15509]: listening on 10.210.206.58
Nov 22 10:20:07 cenots-db ntpd[15509]: listening on ::1
Nov 22 10:20:07 cenots-db ntpd[15509]: ntp engine ready
Nov 22 10:20:08 cenots-db systemd[1]: Started OpenNTP Daemon.

确保它在重新启动时启动:

$ sudo systemctl enable openntpd

执行以下命令以验证OpenNTPD(ntpd)是否正在运行:

$ netstat -tulpn
$ netstat -tulpn | grep :123

或者

$ sudo ss -ltun

输出示例:

udp        0      0 10.210.206.58:123               0.0.0.0:*                               31182/ntpd          
udp        0      0 127.0.0.1:123               0.0.0.0:*                               31182/ntpd

NTP是否同步?

执行以下命令:

$ timedatectl status

输出示例:

Local time: Tue 2015-11-22 10:25:46 UTC
  Universal time: Tue 2015-11-22 10:25:46 UTC
        RTC time: n/a
       Time zone: UTC (UTC, +0000)
     NTP enabled: n/a
NTP synchronized: yes
RTC in local TZ: no
      DST active: n/a