如何在Linux/Unix之上綁定ntpd到特定的IP地址
默認的情況下,我們的 ntpd/NTP 服務器會監(jiān)聽所有的端口或者 IP 地址,也就是:0.0.0.0:123。 怎么才可以在一個 Linux 或是 FreeBSD Unix 服務器上,確保只監(jiān)聽特定的 IP 地址,比如 localhost 或者是 192.168.1.1:123 ?
NTP 是網(wǎng)絡時間協(xié)議的首字母簡寫,這是一個用來同步兩臺電腦之間時間的協(xié)議。ntpd 是一個操作系統(tǒng)守護進程,可以設置并且保證系統(tǒng)的時間與互聯(lián)網(wǎng)標準時間服務器同步。
NTP 使用 /etc/directory 之下的 ntp.conf 作為配置文件。
/etc/ntp.conf 之中的端口指令
你可以通過設置端口命令來防止 ntpd 監(jiān)聽 0.0.0.0:123,語法如下:
interface listen IPv4|IPv6|allinterface ignore IPv4|IPv6|allinterface drop IPv4|IPv6|all
上面的配置可以使 ntpd 監(jiān)聽那個地址或者不出來任何請求而直接丟棄。ignore 會防止打開匹配的地址,drop 會導致 ntpd 打開該地址并丟棄所有接收到的包,而不做任何檢查。舉個例子,如果要忽略所有端口之上的監(jiān)聽,加入下面的語句到 /etc/ntp.conf:
interface ignore wildcard
如果只監(jiān)聽 127.0.0.1 和 192.168.1.1 則是這樣:
interface listen 127.0.0.1interface listen 192.168.1.1
這是我 FreeBSD 云服務器上的樣例 /etc/ntp.conf 文件:
$ egrep -v '^#|$^' /etc/ntp.conf
樣例輸出為:
tos minclock 3 maxclock 6pool 0.freebsd.pool.ntp.org iburstrestrict default limited kod nomodify notrap noquery nopeerrestrict -6 default limited kod nomodify notrap noquery nopeerrestrict source limited kod nomodify notrap noqueryrestrict 127.0.0.1restrict -6 ::1leapfile "/var/db/ntpd.leap-seconds.list"interface ignore wildcardinterface listen 172.16.3.1interface listen 10.105.28.1
重啟 ntpd
在 FreeBSD Unix 之上重新加載/重啟 ntpd:
$ sudo /etc/rc.d/ntpd restart
或者 在 Debian 和 Ubuntu Linux 之上使用下面的命令:
$ sudo systemctl restart ntp
或者 在 CentOS/RHEL 7/Fedora Linux 之上使用下面的命令:
$ sudo systemctl restart ntpd
校驗
使用 netstat 和 ss 命令來檢查 ntpd 只綁定到了特定的 IP 地址:
$ netstat -tulpn | grep :123
或是:
$ ss -tulpn | grep :123
樣例輸出:
udp 0 0 10.105.28.1:123 0.0.0.0:* -udp 0 0 172.16.3.1:123 0.0.0.0:* -
在 FreeBSD Unix 服務器上使用 sockstat 命令:
$ sudo sockstat$ sudo sockstat -4$ sudo sockstat -4 | grep :123
樣例輸出:
root ntpd 59914 22 udp4 127.0.0.1:123 *:*root ntpd 59914 24 udp4 127.0.1.1:123 *:*
























