一、安装ntp服务
yum install -y ntp
二、设置时区
1、查看系统提供的时区信息
[root@localhost ~]# ls -F /usr/share/zoneinfo/
Africa/ Brazil/ Egypt GB-Eire HST Japan Navajo posixrules Turkey zone.tab
America/ Canada/ Eire GMT Iceland Kwajalein NZ PRC UCT Zulu
Antarctica/ CET EST GMT0 Indian/ Libya NZ-CHAT PST8PDT Universal
Arctic/ Chile/ EST5EDT GMT-0 Iran MET Pacific/ right/ US/
Asia/ CST6CDT Etc/ GMT+0 iso3166.tab Mexico/ Poland ROC UTC
Atlantic/ Cuba Europe/ Greenwich Israel MST Portugal ROK WET
Australia/ EET GB Hongkong Jamaica MST7MDT posix/ Singapore W-SU
在这里面可以找到自己所在城市的time zone文件,例如北京时间就是这个文件
[root@localhost ~]# ls -F /usr/share/zoneinfo/Asia/Shanghai
/usr/share/zoneinfo/Asia/Shanghai
查看每个time zone当前的时间可以使用zdump命令
[root@localhost ~]# zdump Japan
Japan Thu Feb 16 12:01:34 2017 JST
若要修改系统时区可以使用如下方法
[root@localhost ~]# rm -rf /etc/localtime
[root@localhost ~]# ln -sf /usr/share/zoneinfo/posix/Asia/Shanghai /etc/localtime
[root@localhost ~]# ll /etc/localtime
lrwxrwxrwx. 1 root root 35 Feb 16 2017 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai
[root@localhost ~]# date
Thu Feb 16 11:05:24 CST 2017
三、同步硬件时间和系统时间
首先明确一个概念:
在计算机上有两个时钟,一个是硬件时钟(RTC),一个是系统时钟(System Clock)
硬件时钟(RTC)是指嵌在主板上的特殊的电路,他的存在就是平时我们关机后还可以计算时间的原因。
系统时钟(System Clock)是操作系统的kernel用来计算时间的时钟,它从1970年1月1日00:00:00 UTC时间到目前为止秒数总和的值 在Linux下系统时间在开机的时候会和硬件时间同步(synchronization),之后也就各自独立运行了
既然两个时钟独立运行,时间久了就会产生误差,
[root@localhost ~]# date
Thu Feb 16 11:13:04 CST 2017
[root@localhost ~]# hwclock --show
2017-02-16 11:13:05.679891+8:00
通过hwclock –show 命令我们可以查看机器上的硬件时间(always in local time zone), 我们可以看到它和系统时间还是有一定的误差的, 那么我们就需要把他们同步
如果我们想要把硬件时间设置成系统时间我们可以运行以下命令
[root@localhost ~]# hwclock --hctosys
反之,我们也可以把系统时间设置成硬件时间
[root@localhost ~]# hwclock --systohc
那么如果想设置硬件时间我们可以开机的时候在BIOS里设定.也可以用hwclock命令
[root@localhost ~]# hwclock --set --date="mm/dd/yy hh:mm:ss"
如果想要修改系统时间那么用date命令就最简单了
[root@localhost ~]# date -s "dd/mm/yyyy hh:mm:ss"
四、修改ntp.conf的配置
vim /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).
# 将自己主机的bios芯片震荡频率与上层的Time server频率比较,将误差记录在这个文件里
# 注意: driftfile 后面接的文件需要使用完整的路径文件名,不能是链接文件,并且文件的权限需要设定成 ntpd守护进程可以写入。
# 格式:driftfile 文件名
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 [IP] mask [netmask_IP] [parameter]
# Parameter 的参数主要如下:
# ignore :拒绝所有类型的NTP联机。
# nomodify: 客户端不能使用ntpc 与ntpq 这两个程序来修改服务器的时间参数,但客户端可透过这部主机来进行网络校时;
# noquery:客户端不能够使用ntpc 与ntpq 等指令来查询时间服务器,不提供NTP的网络校时。
# notrap:不提供trap 这个运程事件登入的功能。
# notrust:拒绝没有认证的客户端。
# Kod:kod技术可以阻止“Kiss of Death “包对服务器的破坏。
# Nopeer:不与其他同一层的NTP服务器进行时间同步。
# 如果没有在parameter的地方加上任何参数的话,那么表示这个ip或网络不受任何限制
# 表示默认拒绝所有IP的时间同步
#restrict default nomodify notrap nopeer noquery
# 允许任何ip的客户机都可以进行时间同步
restrict default nomodify
# 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 ::1
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
# 允许192.168.159.0/24网段的客户机都可以进行时间同步
restrict 192.168.159.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 设定上层NTP服务器
# 格式:server [IP or hostname] [prefer]
# perfer:表示优先级最高
# burst :当一个运程NTP服务器可用时,向它发送一系列的并发包进行检测。
# iburst :当一个运程NTP服务器不可用时,向它发送一系列的并发包进行检测。
# 注:默认情况小15分钟后才会与上层NTP服务器进行时间校对。
server 210.72.145.44 prefer
server time1.aliyun.com iburst
server time2.aliyun.com iburst
server time3.aliyun.com iburst
server time4.aliyun.com iburst
# 设置本地服务器为根时间服务器
server 127.127.1.0
# 这行是时间服务器的层次。设为0则为顶级,如果要向别的NTP服务器更新时间,请不要把它设为0
fudge 127.127.1.0 stratum 6
#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
# Disable the monitoring facility to prevent amplification attacks using ntpdc
# monlist command when default restrict does not include the noquery flag. See
# CVE-2013-5211 for more details.
# Note: Monitoring will not be disabled with the limited restriction flag.
# 防御NTP放大攻击和NTP反射
disable monitor
五、启动ntpd服务,并配置开机启动
[root@localhost ~]# systemctl restart ntpd
[root@localhost ~]# systemctl enable ntpd
[root@localhost ~]# systemctl status ntpd
● ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2017-02-16 11:36:29 CST; 12s ago
Main PID: 2819 (ntpd)
Tasks: 1 (limit: 19660)
CGroup: /system.slice/ntpd.service
└─2819 /usr/sbin/ntpd -u ntp:ntp -g
Feb 16 11:36:29 admin.example.local ntpd[2819]: Listen normally on 4 ens34 192.168.139.135 UDP 123
Feb 16 11:36:29 admin.example.local ntpd[2819]: Listen normally on 5 virbr0 192.168.122.1 UDP 123
Feb 16 11:36:29 admin.example.local ntpd[2819]: Listen normally on 6 ens33 fe80::20c:29ff:febf:9bc9 UDP 123
Feb 16 11:36:29 admin.example.local ntpd[2819]: Listen normally on 7 lo ::1 UDP 123
Feb 16 11:36:29 admin.example.local ntpd[2819]: Listen normally on 8 ens34 fe80::15cc:96ee:f841:b516 UDP 123
Feb 16 11:36:29 admin.example.local ntpd[2819]: Listening on routing socket on fd #25 for interface updates
Feb 16 11:36:29 admin.example.local ntpd[2819]: 0.0.0.0 c016 06 restart
Feb 16 11:36:29 admin.example.local ntpd[2819]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM
Feb 16 11:36:29 admin.example.local ntpd[2819]: 0.0.0.0 c011 01 freq_not_set
Feb 16 11:36:31 admin.example.local ntpd[2819]: 0.0.0.0 c614 04 freq_mode
六、查看ntp服务运行状况
# watch ntpq -p
各列的释义:
列值 | 释义 |
---|---|
remote | 它指的就是本地机器所连接的远程NTP服务器 |
refid | 它指的是给远程服务器(e.g. 193.60.199.75)提供时间同步的服务器 |
st | 远程服务器的层级别(stratum). 由于NTP是层型结构,有顶端的服务器,多层的Relay Server再到客户端. 所以服务器从高到低级别可以设定为1-16. 为了减缓负荷和网络堵塞,原则上应该避免直接连接到级别为1的服务器的. |
t | 这个…..我也不知道啥意思^_^ |
when | 我个人把它理解为一个计时器用来告诉我们还有多久本地机器就需要和远程服务器进行一次时间同步 |
poll | 本地机和远程服务器多少时间进行一次同步(单位为秒). 在一开始运行NTP的时候这个poll值会比较小,那样和服务器同步的频率也就增加了,可以尽快调整到正确的时间范围.之后poll值会逐渐增大,同步的频率也就会相应减小 |
reach | 这是一个八进制值,用来测试能否和服务器连接.每成功连接一次它的值就会增加 |
delay | 从本地机发送同步要求到服务器的round trip time |
offset | 这是个最关键的值, 它告诉了我们本地机和服务器之间的时间差别.offset越接近于0,我们就和服务器的时间越接近 |
jitter | 这是一个用来做统计的值. 它统计了在特定个连续的连接数里offset的分布情况. 简单地说这个数值的绝对值越小我们和服务器的时间就越精确 |
在这里会发现两个问题:
1. 我们连接的是0.fedora.pool.ntp.org为什么和remote server不一样?
2. 那个最前面的+和*都是什么意思呢?
第一个问题不难理解,因为NTP提供给我们的是一个cluster server所以每次连接的得到的服务器都有可能是不一样。同样这也告诉我们了在指定NTP Server的时候应该使用hostname而不是IP。
第二个问题和第一个相关,既然有这么多的服务器就是为了在发生问题的时候其他的服务器还可以正常地给我们提供服务.那么如何知道这些服务器的状态呢? 这就是第一个记号会告诉我们的信息
标记 | 释义 |
---|---|
* | 它告诉我们远端的服务器已经被确认为我们的主NTP Server,我们系统的时间将由这台机器所提供 |
+ | 它将作为辅助的NTP Server和带有号的服务器一起为我们提供同步服务. 当号服务器不可用时它就可以接管 |
- | 远程服务器被clustering algorithm认为是不合格的NTP Server |
x | 远程服务器不可用 |