搭建NTP服务器,下端的设备都主动向服务器对时,保持整个系统的时间同步。下端设备采用NTP客户端 ntpd,(注:这个之前已经测试过,NTP服务器在win7系统),现在ubuntu上搭建NTP服务器。
ubuntu版本:
root@jwx-merit:/etc/init.d# lsb_release -a
No LSB modules are available.Distributor ID: Ubuntu
Description: Ubuntu 12.04.5 LTS
Release: 12.04
Codename: precise
1、安装NTP
sudo apt-get install ntp
2、修改配置文件 /etc/ntp.conf
# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help
driftfile /var/lib/ntp/ntp.drift
# Enable this if you want statistics to be logged.
statsdir /var/log/ntpstats/
statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable
# Specify one or more NTP servers.
# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information.
server 0.ubuntu.pool.ntp.org
server 1.ubuntu.pool.ntp.org
server 2.ubuntu.pool.ntp.org
server 3.ubuntu.pool.ntp.org
# Use Ubuntu's ntp server as a fallback.
server ntp.ubuntu.com
# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
# details. The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
# might also be helpful.
#
# Note that "restrict" applies to both servers and clients, so a configuration
# that might be intended to block requests from certain clients could also end
# up blocking replies from your own upstream servers.
# By default, exchange time with everybody, but don't allow configuration.
restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery
# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1
# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
#restrict 192.168.0.0 mask 255.255.255.0 nomodify
restrict 172.16.0.0 mask 255.255.0.0 nomodify #主要是修改这句话,允许同步的网段限制
# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
#broadcast 192.168.123.255
# If you want to listen to time broadcasts on your local subnet, de-comment the
# next lines. Please do this only if you trust everybody on the network!
#disable auth
#broadcastclient
注:(1)、权限设定部分
如: restrict 172.16.0.0 mask 255.255.0.0 nomodify #主要是修改这句话,允许同步的网段限制
权限设定主要以restrict这个参数来设定,
主要的语法为: restrict IP mask netmask_IP parameter
其中IP可以是软体位址,也可以是 default ,default 就类似0.0.0.0
至于 paramter则有:
ignore:关闭所有的NTP 连线服务
nomodify:表示Client 端不能更改 Server 端的时间参数,不过Client端仍然可以透过Server 端來进行网络较时。
notrust:该 Client 除非通过认证,否则该 Client 来源将被视为不信任网域
noquery:不提供 Client 端的时间查询 如果 paramter完全没有设定,那就表示该 IP (或网域) 『没有任何限制!』
(2)、上层主机设定 上层主机选择ntp.ubuntu.com,要设定上层主机主要以server这个参数来设定,语法为:server [IP|FQDN] [prefer] Server 后面接的就是我们上层的Time Server !而如果 Server 参数后面加上perfer 的话,那表示我们的 NTP 主机主 要以该部主机来作为时间较正的对应。另外,为了解决更新时间封包的传送延迟动作,可以使用driftfile 来规定我们的主 机在与Time Server沟通时所花费的时间,可以记录在 driftfile 后面接的档案内
3、ubuntu开启防火墙时,下端设备无法进行NTP对时,因此需要关闭防火墙 或者开放NTP占用的 123端口
(1)关闭防火墙:
1>ufw disable
root@jwx-merit:/etc/init.d# ufw disable
防火墙在系统启动时自动禁用
root@jwx-merit:/etc/init.d# ufw status
状态:不活动
2>永久关闭防火墙:chkconfig iptables off
(2)开放123端口
iptables -t filter -A INPUT -p udp --destination-port 123 -j ACCEPT
4、重启NTP服务
sudo /etc/init.d/ntp restart
5、客户端同步
ntpdate 172.16.30.21 (注:服务器IP为172.16.30.21)
修改硬件时间:hwclock -w