本文共 9129 字,大约阅读时间需要 30 分钟。
一、ntp同步shell命令:ntpdate time.nist.gov(或填ip:132.163.4.103)
二、配置NTP服务器(默认NTP软件已安装)。
①备份原ntp配置文件
cp/etc/ntp.conf/etc/ntp.conf.bak
②修改配置文件
vi /etc/ntp.conf
我的ntp.conf配置:
logfile /var/log/ntp.logrestrict default nomodifyrestrict 127.0.0.1restrict 192.168.0.0 mask 255.255.255.0 nomodifyserver 128.138.141.172 prefer
若设置本地服务器为根时间服务器,则最后的参数改为:
server 127.127.1.0
fudge 127.127.1.0 stratum 1
三、查看ntpd是否可用。(一般要等5~10分钟后才可用)
watch ntpq -p
四、其他
1. 配置文件中的driftfile是什么? 我们每一个system clock的频率都有小小的误差,这个就是为什么机器运行一段时间后会不精确. NTP会自动来监测我们时钟的误差值并予以调整.但问题是这是一个冗长的过程,所以它会把记录下来的误差先写入driftfile.这样即使你重新开机以后之前的计算结果也就不会丢失了
2. 如何同步硬件时钟? NTP一般只会同步system clock. 但是如果我们也要同步RTC(hwclock)的话那么只需要把下面的选项打开就可以了 代码: # vi /etc/sysconfig/ntpd SYNC_HWCLOCK=yes
参考网站:
以下为转载的
[root@bogon ~]# clear
[root@bogon ~]# lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: Fedora
Description: Fedora release 17 (Beefy Miracle)
Release: 17
Codename: BeefyMiracle
[root@bogon ~]# uname -a
Linux bogon 3.6.10-2.fc17.x86_64 #1 SMP Tue Dec 11 18:07:34 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
[root@bogon ~]# rpm -qa |grep ntp
fontpackages-filesystem-1.44-4.fc17.noarch
ntpdate-4.2.6p5-2.fc17.x86_64
ntp-4.2.6p5-2.fc17.x86_64
[root@bogon ~]# cat /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
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).
server 0.fedora.pool.ntp.org iburst
server 1.fedora.pool.ntp.org iburst
server 2.fedora.pool.ntp.org iburst
server 3.fedora.pool.ntp.org iburst
#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
# 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
[root@bogon Asia]# cat /etc/sysconfig/clock #设定时区的配置文件
ZONE="Asia/Shanghai"
driftfile /var/lib/ntp/drift #侦测BIOS时钟与Linux系统时间的差异写入次文件。
利用restrict 来管理权限控制
Restrict [IP] mask [netmask_IP] [parameter]
Parameter 的参数主要如下:
u ignore :拒绝所有类型的NTP联机。
u nomodify: 客户端不能使用ntpc 与ntpq 这两个程序来修改服务器的时间参数,但客户端可透过这部主机来进行网络校时;
u noquery:客户端不能够使用ntpc 与ntpq 等指令来查询时间服务器,不提供NTP的网络校时。
u notrap:不提供trap 这个运程事件登入的功能。
u notrust:拒绝没有认证的客户端。
u Kod:kod技术可以阻止“Kiss of Death “包对服务器的破坏。
u Nopeer:不与其他同一层的NTP服务器进行时间同步。
利用server 设定上层NTP服务器,格式如下:
server [IP or hostname] [prefer]
perfer:表示优先级最高
burst :当一个运程NTP服务器可用时,向它发送一系列的并发包进行检测。
iburst :当一个运程NTP服务器不可用时,向它发送一系列的并发包进行检测。
注:默认情况小15分钟后才会与上层NTP服务器进行时间校对。.
Keys [key_file]
除了以 restrict 来限制客户端的联机之外,我们也可以透过密钥系统来给客户端认证.
开启ntp服务器:
[root@bogon ~]# cat /etc/sysconfig/ntpd
OPTIONS="-g"
SYNC_HWCLOCK=yes #允许BIOS与系统时间同步。
[root@bogon ~]# systemctl stop ntpd.service #停止NTP服务
[root@bogon ~]# systemctl start ntpd.service #开启NTP服务
[root@bogon ~]# systemctl enable ntpd.service #开机自启动NTP服务
[root@bogon ~]# systemctl status ntpd.service #查看NTP服务状态
ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled)
Active: active (running) since Fri, 04 Jan 2013 18:53:19 +0800; 1min 36s ago
Main PID: 2233 (ntpd)
CGroup: name=systemd:/system/ntpd.service
└ 2233 /usr/sbin/ntpd -u ntp:ntp -g
Jan 04 18:53:19 bogon ntpd[2233]: Listen and drop on 1 v6wildcard :: UDP 123
Jan 04 18:53:19 bogon ntpd[2233]: Listen normally on 2 lo 127.0.0.1 UDP 123
Jan 04 18:53:19 bogon ntpd[2233]: Listen normally on 3 eth0 192.168.1.174 UDP 123
Jan 04 18:53:19 bogon ntpd[2233]: Listen normally on 4 lo ::1 UDP 123
Jan 04 18:53:19 bogon ntpd[2233]: Listen normally on 5 eth0 fe80::20c:29ff:fe89:5909 UDP 123
Jan 04 18:53:19 bogon ntpd[2233]: peers refreshed
Jan 04 18:53:19 bogon ntpd[2233]: Listening on routing socket on fd #22 for interface updates
Jan 04 18:53:20 bogon ntpd[2233]: 0.0.0.0 c016 06 restart
Jan 04 18:53:20 bogon ntpd[2233]: 0.0.0.0 c012 02 freq_set kernel -20.218 PPM
Jan 04 18:53:26 bogon ntpd[2233]: 0.0.0.0 c615 05 clock_sync
[root@bogon ~]# netstat -lpn |grep 123
udp 0 0 192.168.1.174:123 0.0.0.0:* 2269/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 2269/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 2269/ntpd
udp6 0 0 fe80::20c:29ff:fe89:123 :::* 2269/ntpd
udp6 0 0 ::1:123 :::* 2269/ntpd
udp6 0 0 :::123 :::* 2269/ntpd
root@bogon ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
+218.75.4.130 127.67.113.92 2 u 14 64 67 66.252 -24.243 45.587
+ns2.vnet.sk 194.160.23.2 2 u 18 64 77 397.865 -17.536 12.293
*dns2.synet.edu. .PPS. 1 u 13 64 77 66.984 -6.045 46.869
remote :本地主机所连接的上层NTP服务器,最左边的符号如下:
如果有[*] 代表目前正在使用当中的上层NTP服务器。
如果有[+] 代表也有连上上层NTP服务器,可以作为提高时间更新的候选 NTP服务器。
refid :指的是给上层NTP服务器提供时间校对的服务器。
St:上层NTP服务器的级别。
When: 上一次与上层NTP服务器进行时间校对的时间(单位:s)
Poll :下一次本地主机与上层NTP服务器进行时间校对的时间(单位:s)
reach:已经向上层 NTP 服务器要求更新的次数
delay:网络传输过程当中延迟的时间,单位为 10^(-6) 秒
offset:时间补偿的结果,单位与 10^(-3) 秒
jitter:Linux 系统时间与 BIOS 硬件时间的差异时间, 单位为 10^(-6) 秒。
[root@bogon ~]# ntpstat
synchronised to NTP server (202.112.31.197) at stratum 2 #进行时间校对的NTP服务器。
time correct to within 97 ms #本地主机与上层NTP服务器的时间差
polling server every 128 s #下次同步时间。
客户端更新时间:
1. 借助crontab 更新时间
[root@s8-app-db-01 ~]# crontab -l |grep ntpdate
08 * * * * /usr/sbin/ntpdate 192.168.174. && /sbin/hwclock --systohc
2.透过 NTP 去主动的更新时间。
[root@138 ~]# rpm -qa |grep ntp
fontpackages-filesystem-1.41-1.1.el6.noarch
ntpdate-4.2.4p8-2.el6.centos.x86_64
ntp-4.2.4p8-2.el6.centos.x86_64
[root@138 ~]# vim /etc/sysconfig/ntpd
SYNC_HWCLOCK=yes
[root@138 ~]# vim /etc/ntp.conf
restrict 192.168.1.174 #方形服务器来源
server 192.168.1.174 #NTP校对时间服务器
[root@138 ~]# /etc/init.d/ntpd start
正在启动 ntpd: [确定]
[root@138 ~]# chkconfig ntpd on
[root@138 ~]# netstat -plan |grep 123
udp 0 0 192.168.122.1:123 0.0.0.0:* 15590/ntpd
udp 0 0 192.168.212.1:123 0.0.0.0:* 15590/ntpd
udp 0 0 192.168.243.1:123 0.0.0.0:* 15590/ntpd
udp 0 0 192.168.1.168:123 0.0.0.0:* 15590/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 15590/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 15590/ntpd
udp 0 0 fe80::250:56ff:fec0:1:123 :::* 15590/ntpd
udp 0 0 fe80::213:21ff:fec6:375b:123 :::* 15590/ntpd
udp 0 0 ::1:123 :::* 15590/ntpd
udp 0 0 fe80::250:56ff:fec0:8:123 :::* 15590/ntpd
udp 0 0 :::123 :::* 15590/ntpd
实例:
有一台服务器地址为192.168.1.174,需要为192.168.1.0/24这个网络中的主机提供NTP服务,并为210.137.180.180-185这几天计算机提供NTP服务。它使用上级NTP服务器server 0.fedora.pool.ntp.org 、server 1.fedora.pool.ntp.org 、server 2.fedora.pool.ntp.org server 3.fedora.pool.ntp.org
配置文件如下:
driftfile /var/lib/ntp/drift
restrict default ignore
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
restricat 192.168.1.0 mask 255.255.255.0 nomodify notrap
restricat 210.137.180.180
restricat 210.137.180.181
restricat 210.137.180.182
restricat 210.137.180.183
restricat 210.137.180.184
restricat 210.137.180.185
server 0.fedora.pool.ntp.org iburst
server 1.fedora.pool.ntp.org iburst
server 2.fedora.pool.ntp.org iburst
server 3.fedora.pool.ntp.org iburst
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
logfile /var/log/ntp
[root@bogon ~]# systemctl start ntpd.service
[root@bogon ~]# systemctl enable ntpd.service
[root@bogon ~]# cat /etc/sysconfig/ntpd
# Command line options for ntpd
OPTIONS="-g"
[root@bogon ~]# cat /var/log/ntp
6 Jan 15:12:09 ntpd[6558]: 0.0.0.0 c016 06 restart
6 Jan 15:12:09 ntpd[6558]: 0.0.0.0 c012 02 freq_set kernel -16.934 PPM
6 Jan 15:12:15 ntpd[6558]: 0.0.0.0 c61c 0c clock_step +14.819650 s
6 Jan 15:12:29 ntpd[6558]: 0.0.0.0 c614 04 freq_mode
6 Jan 15:12:30 ntpd[6558]: 0.0.0.0 c618 08 no_sys_peer
[root@bogon ~]# ntpq -p -n
remote refid st t when poll reach delay offset jitter
==============================================================================
+218.75.4.130 66.220.9.122 2 u 42 64 3 118.803 17.132 68.630
*202.112.31.197 .PPS. 1 u 35 64 7 63.094 4.830 77.302
+202.112.29.82 202.112.31.197 2 u 34 64 7 67.002 6.711 70.345