时间同步
- 设置时区和时间 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29- [root@[ALL] ~]# [ -f /etc/localtime ] && cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 
 [root@[ALL] ~]# [ -f /etc/sysconfig/clock ] && echo 'ZONE="Asia/Shanghai"' | tee /etc/sysconfig/clock
 [root@[ALL] ~]# [ -f /etc/timezone ] && echo 'Asia/Shanghai' | tee /etc/timezone
 [root@[ALL] ~]# [ -f /etc/sysconfig/ntpd ] && echo 'SYNC_HWCLOCK=yes' | tee -a /etc/sysconfig/ntpd
 [root@[ALL] ~]# ntpdate cn.pool.ntp.org
 [root@[ALL] ~]# cp -f /etc/{ntp.conf,ntp.conf.bak}
 [root@[ALL] ~]# cat > /etc/ntp.conf <<EOF
 > driftfile /var/lib/ntp/drift
 > restrict default nomodify notrap nopeer noquery
 > restrict 127.0.0.1
 > restrict ::1
 > server cn.pool.ntp.org prefer
 > server 0.centos.pool.ntp.org iburst
 > server 1.centos.pool.ntp.org iburst
 > server 2.centos.pool.ntp.org iburst
 > server 3.centos.pool.ntp.org iburst
 > includefile /etc/ntp/crypto/pw
 > keys /etc/ntp/keys
 > disable monitor
 > EOF
 [root@[ALL] ~]# cp -f /etc/ntp/{step-tickers,step-tickers.bak}
 [root@[ALL] ~]# cat > /etc/ntp/step-tickers <<EOF
 > cn.pool.ntp.org
 > 0.centos.pool.ntp.org
 > 1.centos.pool.ntp.org
 > 2.centos.pool.ntp.org
 > 3.centos.pool.ntp.org
 > EOF
 [root@[ALL] ~]# systemctl start ntpd && systemctl enable ntpd- 1 
 2
 3
 4- //不在推荐使用 
 [root@[ALL] ~]# systemctl stop ntpd
 [root@[ALL] ~]# systemctl disable ntpd
 [root@[ALL] ~]# echo '1 1 * * * /usr/sbin/ntpdate -s cn.pool.ntp.org' | tee /var/spool/cron/root
- ntpd 和 ntpdate 简介 - 1 
 2
 3
 4
 5- 在Windwos中,系统时间的设置很简单,界面操作,通俗易懂.而且设置后,重启,关机都没关系.系统时间会自动保存在Bios的时钟里面,启动计算机的时候,系统会自动在Bios里面取硬件时间,以保证时间的不间断. 
 在Linux下,默认情况下,系统时间和硬件时间,并不会自动同步.在Linux运行过程中,系统时间和硬件时间以异步的方式运行,互不干扰.硬件时间的运行,是靠Bios电池来维持,而系统时间,是用CPU tick来维持的
 在系统开机的时候,会自动从Bios中取得硬件时间,设置为系统时间.
- Linux系统时间的设置 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11- //查看时间 
 [root@localhost ~]# date
 2008年 12月 12日 星期五 14:44:12 CST
 //修改时间
 [root@localhost ~]# date --set "1/1/09 00:01" ==>月/日/年时:分:秒
 2009年 01月 01日 星期四 00:01:00 CST
 //date 有几种时间格式可接受,这样也可以设置时间:
 [root@localhost ~]# date 012501012009.30 ==>月日时分年.秒
 2009年 01月 25日 星期日 01:01:30 CST
- Linux硬件时间的设置 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10- 硬件时间的设置,可以用hwclock或者clock命令.其中,clock和hwclock用法相近,只用一个就行,只不过clock命令除了支持x86硬件体系外,还支持Alpha硬件体系 
 //查看硬件时间 可以是用 hwclock ,hwclock --show 或者 hwclock -r
 [root@localhost ~]# hwclock --show
 2008年12月12日 星期五 06时52分07秒 -0.376932 seconds
 //设置硬件时间
 [root@localhost ~]# hwclock --set --date="1/25/09 00:00" <== 月/日/年时:分:秒
 [root@localhost ~]# hwclock
 2009年01月25日 星期日 00时00分06秒 -0.870868 seconds
- Linux系统时间和硬件时间的同步 - 1 
 2
 3
 4
 5
 6
 7
 8
 9- 同步系统时间和硬件时间,可以使用hwclock命令: 
 //以系统时间为基准,修改硬件时间
 [root@localhost ~]# hwclock --systohc ==> sys(系统时间)to(写到)hc(Hard Clock)
 [root@localhost ~]# hwclock -w
 //以硬件时间为基准,修改系统时间
 [root@localhost ~]# hwclock --hctosys
 [root@localhost ~]# hwclock -s
- 不同机器之间的时间同步 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15- 为了避免主机时间因为长期运作下所导致的时间偏差,进行时间同步(synchronize)的工作是非常必要的. 
 Linux系统下,一般使用ntp服务器来同步不同机器的时间.一台机器,可以同时是ntp服务器和ntp客户机.在网络中,推荐使用像DNS服务器一样分层的时间服务器来同步时间.
 同步时间,可以使用ntpdate命令,也可以使用ntpd服务
 //使用ntpdate比较简单,格式如下:
 [root@linux ~]# ntpdate [-nv] [NTP IP/hostname]
 [root@linux ~]# ntpdate 192.168.0.1
 [root@linux ~]# ntpdate time.ntp.org
 但这样的同步,只是强制性的将系统时间设置为ntp服务器时间.如果cpu tick有问题,只是治标不治本.所以,一般配合cron命令,来进行定期同步设置.比如,在crontab中添加:
 0 12 * * * * /usr/sbin/ntpdate time.ntp.org
 //使用ntpd服务,要好于ntpdate加cron的组合
 因为,ntpdate同步时间,会造成时间的跳跃,对一些依赖时间的程序和服务会造成影响.比如: sleep,timer等.而且,ntpd服务可以在修正时间的同时,修正cpu tick.理想的做法为,在开机的时候,使用ntpdate强制同步时间,在其他时候使用ntpd服务来同步时间.
 要注意的是,ntpd有一个自我保护设置: 如果本机与上源时间相差太大,ntpd不运行.所以新设置的时间服务器一定要先 ntpdate从上源取得时间初值,然后启动ntpd服务.ntpd服务运行后,先是每64秒与上源服务器同步一次,根据每次同步时测得的误差值经复杂计算逐步调整自己的时间,随着误差减小,逐步增加同步的间隔.每次跳动,都会重复这个调整的过程.
- ntpd服务的设置 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60- //ntpd服务的相关设置文件 
 * /etc/ntp.conf
 这个是NTP daemon的主要设文件,也是 NTP 唯一的设定文件
 * /usr/share/zoneinfo/
 在这个目录下的文件其实是规定了各主要时区的时间设定文件(例如:北京地区的时区设定文件在/usr/share/zoneinfo/Asia/Beijing).
 这个目录里面的文件与下面要谈的两个文件(clock与localtime)是有关系的
 * /etc/sysconfig/clock
 这个文件其实也不包含在NTP的daemon当中,因为这个是linux的主要时区设定文件.
 每次开机后,Linux会自动的读取这个文件来设定自己系统所默认要显示的时间
 * /etc/localtime
 这个文件就是"本地端的时间配置文件".
 刚刚那个clock文件里面规定了使用的时间设置文件(ZONE)为"/usr/share/zoneinfo/Asia/Beijing",所以说,这就是本地端的时间了.此时,Linux系统就会将Beijing那个文件另存为一份/etc/localtime文件,所以未来我们的时间显示就会以Beijing那个时间设定文件为准
 * /etc/timezone
 系统时区文件
 下面重点说说"/etc/ntp.conf"文件的设置: 在NTP Server的设定上面,其实最好不要对Internet无限制的开放,尽量仅提供您自己内部的Client端联机进行网络校时就好.此外,NTP Server也是需要网络上面较为准确的主机来自行更新自己的时间,所以在我们的NTP Server上面也要找一部最靠近自己的Time Server来进行自我校正.事实上,NTP这个服务也是C/S的一种模式
 [root@linux ~]# vi /etc/ntp.conf
 # 1.关于权限设定部分
 # 权限设定主要以"restrict"这个参数来设定,主要语法为:
 # restrict {IP_addr} mask {net_mast} parameter
 # * IP 可以是软件地址,也可以是"default","default"类似"0.0.0.0"
 # * paramter则有:
 # * ignore: 关闭所有的NTP联机服务
 # * nomodify: 表示Client端不能更改Server端的时间参数.不过,Client端仍然可以透过Server端来进行网络校时
 # * notrust: 该Client除非通过认证,否则该Client来源将被视为不信任网域
 # * noquery: 不提供Client端的时间查询
 # * notrap: 不提供trap这个远程事件登入
 # * Kod: kod技术可以阻止"Kiss of Death"包对服务器的破坏
 # * Nopeer: 不与其他同一层的NTP服务器进行时间同步
 # 如果paramter完全没有设定,那就表示该IP(或网域)没有任何限制
 restrict default nomodify notrap noquery #关闭所有的NTP要求封包
 restrict 127.0.0.1 #这是允许本地查询
 restrict 192.168.0.1 mask 255.255.255.0 nomodify
 # 在192.168.0.1/24网段内的服务器就可以通过这台NTPServer进行时间同步了
 # 2.上层主机的设定
 # 要设定上层主机主要以server这个参数来设定,语法为:
 # server [IP|HOST Name] [prefer]
 # * Server后面接的就是我们上层"Time Server",如果Server参数后面加上"perfer"的话,那表示我们的NTP主机主要以该部主机来作为时间校正的对应.另外,为了解决更新时间封包的传送延迟动作,所以可以使用"driftfile"来规定我们的主机在与Time Server"沟通时所花费的时间,可以记录在"driftfile"后面接的文件内.
 # * prefer 表示优先级最高
 # * burst 当一个运程NTP服务器可用时,向它发送一系列的并发包进行检测
 # * iburst 当一个运程NTP服务器不可用时,向它发送一系列的并发包进行检测
 # 默认情况小15分钟后才会与上层NTP服务器进行时间校对
 # 例如:我们的NTP server与cn.pool.ntp.org联机时所花费的时间会记录在"/etc/ntp/drift"文件内
 server 0.pool.ntp.org
 server 1.pool.ntp.org
 server 2.pool.ntp.org
 server cn.pool.ntp.org prefer
 # 3.其他设置值,以系统默认值即可
 server 127.127.1.0 # local clock
 fudge 127.127.1.0 stratum 10
 driftfile /var/lib/ntp/drift
 broadcastdelay 0.008
 keys /etc/ntp/keys
 综上,restrict用来设置访问权限,server用来设置上层时间服务器,driftfile用来设置保存漂移时间的文件
- ntp服务的启动与观察 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46- 在启动NTP服务前,先对提供服务的这台主机手动的校正一次时间.()因为启动服务器,端口会被服务端占用,就不能手动同步时间了) 
 [root@linux ~] # ntpdate cn.pool.ntp.org
 然后,启动ntpd服务:
 [root@linux ~] # /etc/init.d/ntpd start
 查看端口:
 [root@linux ~] # netstat -ln|grep 123
 udp 0 0 192.168.228.153:123 0.0.0.0:*
 udp 0 0 127.0.0.1:123 0.0.0.0:*
 udp 0 0 0.0.0.0:123 0.0.0.0:*
 udp 0 0 :::123 :::*
 确认我们的NTP服务器已经更新了自己的时间:
 [root@linux ~] # ntpstat
 synchronized to NTP server(127.127.1.0) at stratum 11
 time correct to within 950ms
 polling server every 64 s
 常见错误:
 25 Apr 15:30:17 ntpdate[11520]: no server suitable for synchronization found
 其实,这不是一个错误.而是由于每次重启NTP服务器之后大约要3-5分钟客户端才能与server建立正常的通讯连接.当此时用客户端连接服务端就会报这样的信息.一般等待几分钟就可以了.
 [root@linux ~] # ntptrace –n 127.0.0.1
 127.0.0.1:stratum 11, offset 0.000000,synch distance 0.950951
 222.73.214.125:stratum 2,offset –0.000787,synch distance 0.108575
 209.81.9.7:stratum 1,offset 0.000028,synch distance 0.00436,refid ‘GPS’
 #这个指令可以列出目前NTP服务器(第一层)与上层NTP服务器(第二层)彼此之间的关系
 指令"ntpq -p"可以列出目前我们的NTP与相关的上层NTP的状态,以上的几个字段的意义如下:
 * remote
 即NTP主机的IP或主机名称
 注意最左边的符号,如果由"+"则代表目前正在作用中的上层NTP,如果是"*"则表示也有连上线,不过是作为次要联机的NTP主机
 * refid 参考的上一层NTP主机的地址
 * st 即stratum阶层
 * when 几秒前曾做过时间同步更新的操作
 * poll 下次更新在几秒之后
 * reach 已经向上层NTP服务器要求更新的次数
 * delay 网络传输过程钟延迟的时间
 * offset 时间补偿的结果
 * jitter Linux系统时间与BIOS硬件时间的差异时间
 最后提及一点,ntp服务,默认只会同步系统时间!!!
 如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件
 在/etc/sysconfig/ntpd文件中,添加"SYNC_HWCLOCK=yes".这样,就可以让硬件时间与系统时间一起同步
- 快速部署 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29- // 私有ntp服务器,同步公网的ntp服务器 
 * 编辑配置
 /etc/ntp.conf:
 # 定义服务器
 server pool.ntp.org
 restrict default nomodify notrap noquery
 #从192.168.0.1-192.168.0.254的服务器都可以使用我们的NTP服务器来同步时间
 restrict 192.168.0.0 mask 255.255.255.0 notrust nomodify notrap
 server 127.127.1.0
 fudge 127.127.1.0 stratum 8 当NTP服务不可用时,使用NTP服务器当前时间同步给客户机
 #注释掉
 #restrict default ignore
 * 启动:
 /etc/init.d/ntpd start
 chkconfig ntpd on
 默认情况下,我们配置的NTP服务器不会去时间源那里同步时间,所以必须修改"/etc/ntp/step-tickers"文件,加入我们的时间源,这样每次通过"/etc/init.d/ntpd"来启动服务的时候就会自动更新时间了
 //配置LINUX客户端
 在linux客户端上执行"ntpdate ntp_server_ip"就可以根据时间服务器统一局域网的时间了,将上面的命令放到cron里每天早上3点定期执行,crontab –e 然后输入:
 0 3 * * * /usr/sbin/ntpdate 192.168.0.179
 //防火墙
 iptables -I INPUT -p udp -m udp --sport 123 -j ACCEPT
- 安装 - 1 - yum -y install ntp ntpdate 
- 配置参数 - 1 
 2
 3
 4
 5
 6
 7- ignore :关闭所有的 NTP 联机服务 
 nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时
 notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网
 noquery :不提供客户端的时间查询:用户端不能使用ntpq,ntpc等命令来查询ntp服务器
 notrap :不提供trap远端登陆:拒绝为匹配的主机提供模式 6 控制消息陷阱服务。陷阱服务是 ntpdq 控制消息协议的子系统,用于远程事件日志记录程序。
 nopeer :用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟
 kod : 访问违规时发送 KoD 包。
- 通用配置文件 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12- /etc/ntp.conf: 
 driftfile /var/lib/ntp/drift
 restrict default nomodify notrap nopeer noquery
 restrict 127.0.0.1
 restrict ::1
 server 0.centos.pool.ntp.org iburst
 server 1.centos.pool.ntp.org iburst
 server 2.centos.pool.ntp.org iburst
 server 3.centos.pool.ntp.org iburst
 includefile /etc/ntp/crypto/pw
 keys /etc/ntp/keys
 disable monitor
- 限制网段访问 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12- /etc/ntp.conf: 
 driftfile /var/lib/ntp/drift
 restrict default nomodify notrap
 restrict 127.0.0.1
 restrict ::1
 server 0.centos.pool.ntp.org iburst
 server 1.centos.pool.ntp.org iburst
 server 2.centos.pool.ntp.org iburst
 server 3.centos.pool.ntp.org iburst
 includefile /etc/ntp/crypto/pw
 keys /etc/ntp/keys
 disable monitor
- 启动ntp服务 - 1 
 2
 3
 4
 5
 6
 7
 8- systemctl start ntpd 
 systemctl enable ntpd
 iptables -A INPUT -p UDP -i eno16777736 -s 192.168.1.0/24 --dport 123 -j ACCEPT
 setsebool -P ntp_disable_trans
 vi /etc/sysconfig/ntpd #允许BIOS与系统时间同步,添加下面一行。
 SYNC_HWCLOCK=yes
- 检测ntp服务是否运行 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18- //检测ntp服务是否运行 
 netstat -tlunp | grep ntp
 //检测有无和上层ntp联通
 ntpstat
 //检测ntp和上层ntp的联通状态
 ntpq -p
 remote - 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先
 refid - 参考上一层ntp主机地址
 st - stratum阶层
 when - 多少秒前曾经同步过时间
 poll - 下次更新在多少秒后
 reach - 已经向上层ntp服务器要求更新的次数
 delay - 网络延迟
 offset - 时间补偿
 jitter - 系统时间与bios时间差
- 客户端配置时间同步 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12- //与ntp服务端同步 
 ntpdate 192.168.1.101
 //写入到bios
 hclock -w
 //写入到计划任务
 /etc/crontab:
 01 0 * * * root /usr/sbin/ntpdate 192.168.1.101 2>&1 >/dev/null
 //重启crond计划任务
 /etc/init.d/crond restart