【实验Linux版本:Red Hat Enterprise Linux Server release 6.4】
1、 安装ntp
yum -y install ntp
2、 修改ntp.conf的配置
1)vim /etc/ntp.conf
① 配置方法一:只允许100.100.100.0网段的客户机进行时间同步
#在restrict default kod nomodify notrap nopeer noquery(表示默认拒绝所有IP的时间同步)之后增加一行:
restrict 100.100.100.0 mask 255.255.255.0 nomodify notrap
② 配置方法二:允许任何ip的客户机都可以进行时间同步
#将restrict default kod nomodify notrap nopeer noquery修改为如下行:
restrict default nomodify
- 权限的设定主要以restrict这个参数来设定,主要的语法为:
restrict IP地址 mask 子网掩码 参数
其中IP可以是IP地址也可以是default,default是指所有的IP
- 参数主要有以下几个:
ignore :关闭所有的 NTP 联机服务
nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
notrust:客户端除非通过认证,否则该客户端来源将被视为不信任子网
noquery:不提供客户端的时间查询
2)用server这个参数设定上级时间服务器,语法为:
server IP地址或域名 [prefer]
- IP地址或域名就是我们指定的上级时间服务器,如果 Server 参数最后加上 prefer,表示我们的 NTP 服务器主要以该部主机时间进行校准。
[参考/etc/ntp.conf文件]
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
#restrict default kod nomodify notrap nopeer noquery
# 允许任何ip的客户机都可以进行时间同步
restrict default nomodify
restrict -6 default kod nomodify notrap nopeer noquery
# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict -6 ::1
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#iserver 0.rhel.pool.ntp.org
#server 1.rhel.pool.ntp.org
#server 2.rhel.pool.ntp.org
# IP地址或域名就是我们指定的上级时间服务器,如果 Server 参数最后加上 prefer,表示我们的 NTP 服务器主要以该部主机时间进行校准
server 100.100.100.27 prefer
#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 autokey # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 autokey # manycast client
Undisciplined Local Clock. This is a fake driver intended for backup
and when no outside source of synchronized time is available.
server 127.127.1.0 # local clock
#fudge 127.127.1.0 stratum 10
#/etc/init.d/ntpd start #启动时间服务器
#service ntpd status
#service ntpd start
# Enable public key cryptography.
#crypto
includefile /etc/ntp/crypto/pw
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys
# Specify the key identifiers which are trusted.
#trustedkey 4 8 42
# Specify the key identifier to use with the ntpdc utility.
#requestkey 8
# Specify the key identifier to use with the ntpq utility.
#controlkey 8
# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats
-------以下内容可以不做------------
3)、解决NTP服务器校准时间时的传送延迟
使用driftfile参数设置:
driftfile 文件名
在与上级时间服务器联系时所花费的时间,记录在driftfile参数后面的文件内。
注意: driftfile 后面接的文件需要使用完整的路径文件名,不能是链接文件,并且文件的权限需要设定成 ntpd守护进程可以写入。
/usr/share/zoneinfo 这个目录下存放的是各个时区对应的设定文件
/etc/localtime 本地系统时间设定文件
/etc/sysconfig/clock 本机时区设定文件
为了使NTP服务可以在系统引导的时候自动启动,执行:
chkconfig ntpd on
将同步好的时间写到CMOS里边
vim /etc/sysconfig/ntpd
SYNC_HWCLOCK=yes
切记每次修改配置文件后重新启动服务使配置生效,可使用如下命令来检查NTP服务是否启动,启动的话可以得到一个进程ID号;
pgrep ntpd
使用下面的命令检查时间服务器同步的状态
ntpq -p
用ntpstat也可以查看一些同步状态信息,
安装完毕客户端需要过5-10分钟才能从服务器端更新时间
客户端设置:
crontab -e
加入一行
*/15 * * * * /usr/sbin/ntpdate 100.100.100.27(服务器端IP)