计算机内部的时钟是在BIOS上记载的,如果BIOS电池没电或者其他原因导致BIOS数据被清除可能会导致时间不正确。如果想快速准确的获取时间,那就需要ntp服务器来协助同步了。
关于时区的概念可以自己搜索查询。标准时间是格林威治时间(Greenwich Mean Time, GMT),地球被划分为24个时区,不同时区的时间就用GMT+或者-来计算当地时间。按照经纬度来分北京时间是GMT+8。所以如果在服务器上遇到时间不对了就去调整时区吧。
1、NTP Server端环境以及所需软件
服务器环境:Centos 6.8
所需软件:ntp(ntp时间服务器的主要软件)、tzdata(Time Zone data,提供 各时区的显示格式)
安装软件:yum install -y ntp
设置开机启动:chkconfig ntpd on
2、跟ntp软件相关的一些配置文件:
ntp服务器相关的配置文件: /etc/ntp.conf:ntp服务器的配置文件 /usr/share/zoneinfo:tzdata提供,为各时区的时间格式对应档 /etc/sysconfig/clock:设定时区,以及设置是否使用UTC时钟的配置文件 /etc/localtime:本地时间的配置文件。会给上边clock定义的ZONE复制一份为localtime,显示的时间就已配置的为准 修改时间命令: /bin/date:系统时间修改以及显示(软件时间) /sbin/hwclock:BIOS时钟修改和显示(硬件时间) /usr/sbin/ntpdate:客户端时间校正。如果仅需要ntp client功能,只需要这个指令即可
3、ntp服务器的配置
上边已经安装过ntp服务,下面先检测下需要用到的上层ntp服务器。
在http://www.pool.ntp.org/zone/cn可以查看到中国地区的ntp服务器,可以检测下本机和上层ntp服务器是否可以正常连接。
如果显示下面这些信息说明可以正常连接,可以给页面上显示的几个时间服务器都测试一下:
# ntpdate -q 0.cn.pool.ntp.org server 173.255.246.13, stratum 2, offset 0.035972, delay 0.27255 server 61.216.153.107, stratum 0, offset 0.000000, delay 0.00000 server 188.166.95.178, stratum 2, offset -0.014046, delay 0.35356 server 193.228.143.24, stratum 2, offset -0.015733, delay 0.41383 6 Mar 15:28:11 ntpdate[6684]: adjust time server 188.166.95.178 offset -0.014046 sec
先贴出来一个配置好的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). 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 <--拒绝IPv4用户 restrict -6 default kod nomodify notrap nopeer noquery <--拒绝IPv6用户 # 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 restrict 192.168.10.0 mask 255.255.255.0 nomodify <--放行指定网段使用ntp服务器 # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). server 0.cn.pool.ntp.org <---设定此时间服务器来源 server 1.cn.pool.ntp.org server 2.cn.pool.ntp.org server 3.cn.pool.ntp.org #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 # logfile /var/log/ntp.log <---日志记录文档
部分指令说明:
restrict做权限控制: restrict [IP地址] mask [netmask IP] [parameter] parameter主要参数如下(如果此处留空则是没有限制): ignore: 拒绝所有类型的 NTP 联机; nomodify: 客户端不能使用 ntpc 与 ntpq 这两支程序来修改服务器的时间参数, 但客户端仍可透过这部主机来进行网络校时的; noquery: 客户端不能够使用 ntpq, ntpc 等指令来查询时间服务器,等于不提供 NTP 的网络校时啰; notrap: 不提供 trap 这个远程事件登录 (remote event logging) 的功能。 notrust: 拒绝没有认证的客户端 server设定上层NTP服务器: server [IP or hostname or 域名] [prefer] prefer即是设置多个优先使用的 driftfile记录时间差异: ntp server计算时间是根据BIOS芯片的震荡周期计算的, 此周期和上层的Time Server可能会不一致, 所以ntp会自动计算误差并记录到指定的文档中 driftfile后面的文档必须用完整路径和文件名 该档案需要设定成 ntpd 这个 daemon 可以写入的权限 该档案所记录的数值单位为:百万分之一秒 (ppm) keys认证: 除了使用 restrict来进行客户端认证还可以使用keys来进行,可以参考ntp-keygen命令来进行设置
4、同步更新hwclock
在/etc/sysconf/ntpd中添加SYNC_HWCLOCK=yes可以同步更新硬件时间
5、启动ntpd服务
# service ntpd start 正在启动 ntpd: [确定] # netstat -tunlp | grep ntpd udp 0 0 192.168.20.1:123 0.0.0.0:* 6734/ntpd udp 0 0 42.236.74.157:123 0.0.0.0:* 6734/ntpd udp 0 0 127.0.0.1:123 0.0.0.0:* 6734/ntpd udp 0 0 0.0.0.0:123 0.0.0.0:* 6734/ntpd udp 0 0 :::123 :::* 6734/ntpd ntp服务走的是udp的123端口,所以需要在防火墙上开放123端口否则客户端如果通过此ntp服务器来进行时间校对
6、查看和上层Time Server的连接状态
# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== 61-216-153-106. .INIT. 16 u - 64 0 0.000 0.000 0.000 *dns1.synet.edu. 202.118.1.48 2 u 2 64 3 59.275 36.595 21.171 +ntp5.flashdance 193.11.166.20 2 u 64 64 2 452.333 15.584 0.000 +163.172.177.158 5.103.128.88 2 u 4 64 3 308.840 28.360 30.942 ntpq -p可以列出本机和上层NTP服务器的状态,上边几个字段意义为: remote:上层ntp主机IP或者主机名 *:remote前有[*]的代表正在使用的上层ntp +:remote前有[+]的代表已经连接上,作为备用的上层ntp服务器用 refid:上层ntp主机的地址 st:是 stratum 阶层 when:几秒钟前曾经做过时间同步化更新的动作; poll:下一次更新在几秒钟之后; reach:已经向上层 NTP 服务器要求更新的次数 delay:网络传输过程当中延迟的时间,单位为 10^(-3) 秒 offset:时间补偿的结果,单位与 10^(-3) 秒 jitter:Linux 系统时间与 BIOS 硬件时间的差异时间,单位为 10^(-3) 秒
7、确认已经进行同步
# ntpstat synchronised to NTP server (202.112.29.82) at stratum 3 time correct to within 80 ms polling server every 64 s 用于检测是否与上层ntp服务器连接。上述信息可以看到已经校正时间和多长时间会校正一次
8、客户端同步测试:
# ntpdate 192.168.100.100 6 Mar 15:50:39 ntpdate[42125]: adjust time server 192.168.100.100 offset -0.023898 sec 添加任务计划: # crontab -e 0 2 * * * /usr/sbin/ntpdate 192.168.100.100 每天凌晨2点同步时间