时间服务器是一种计算机网络仪器,它从参考时钟获取实际时间,再利用计算机网络把时间信息传递给用户。虽然还有一些比较少用或过时的协议仍然在使用,但现时最重要及广泛使用,作为时间信息发送和同步化的协议是网络时间协议(NTP)。服务器所参考的时间信息可以是从另一时间服务器、连线的原子钟或无线电时钟所提供。
环境准备:CentOS7 时间服务器:192.168.56.129 客户端:192.168.56.13 原理:客户端向时间服务器进行查询以实现时间同步,依赖于ntp服务
部署时间服务器
[root@node1 ~]# yum install ntp* -y
[root@node1 ~]# vim /etc/ntp.conf
driftfile /var/lib/ntp/drift #记录晶体晶片的时钟频率
restrict default nomodify notrap nopeer noquery #设置权限,不允许其他服务器进行修改,trap,peer,查询
restrict 127.0.0.1 #也是限制权限,ip后面没写,就是拥有所有权限
restrict 192.168.56.0 mask 255.255.255.0 nomodify notrap
#当有主机来查询,是56网段匹配时,可以使用nomodify,notrap等权限,noquery证明可以查询,如果不是56网段,就使用default的限制
server 127.127.1.0 iburst #作为时间服务器向谁同步,127.127.1.0本地回环地址(主板芯片上的地址),iburst加速服务
[root@node1 ~]# systemctl start ntpd
[root@node1 ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
[root@node1 ~]# date
Tue Sep 4 07:20:43 CST 2018
客户端配置
[root@node3 ~]# date -s "20:00:00" #设置成错误时间
Tue Sep 4 20:00:00 CST 2018
[root@node3 ~]# systemctl is-active ntpd #不用开启ntpd服务
unknown
[root@node3 ~]# ntpdate 192.168.56.129
4 Sep 07:23:30 ntpdate[29705]: step time server 192.168.56.129 offset -45467.042262 sec
[root@node3 ~]# date
Tue Sep 4 07:23:34 CST 2018
自动同步
[root@node3 ~]# crontab -l
#sync the time
#*/5 * * * * /usr/sbin/ntpdate 192.168.56.129 &> /dev/null
修改时区方法
[root@rhel7 ~]# timedatectl
Local time: Mon 2018-08-27 11:42:50 HKT
Universal time: Mon 2018-08-27 03:42:50 UTC
RTC time: Tue 2018-09-04 06:53:47
Timezone: Asia/Hong_Kong (HKT, +0800)
NTP enabled: n/a
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
[root@rhel7 ~]# timedatectl set-timezone Asia/Shanghai #方法一
[root@rhel7 ~]# ls -l /etc/localtime
lrwxrwxrwx. 1 root root 35 Aug 27 11:43 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai
[root@rhel7 ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #方法二