NTP时间同步

网络时间同步

在很多场景下,我们需要保证服务器时间的准确性。这时,如何去做同步就是我们需要考虑的了。
在可以访问外网的环境下,我们直接去同步外部的ntp服务器的时间就ok了。
全球已知的ntp服务器列表可在www.ntp.org.cn中查看。
下来我们开始同步:

  1. 首先安装ntpdate包
[root@localhost ~]# rpm -qa | grep ntp
fontpackages-filesystem-1.44-8.el7.noarch
python-ntplib-0.3.2-1.el7.noarch
[root@localhost ~]# yum install ntpdate -y
......
Installed:
  ntpdate.x86_64 0:4.2.6p5-28.el7.centos

Complete!
  1. 修改系统时间
[root@localhost ~]# date
Thu Jan  3 11:54:34 CST 2019
[root@localhost ~]# date -s 20181124;date -s 10:10:10
Sat Nov 24 00:00:00 CST 2018
Sat Nov 24 10:10:10 CST 2018
[root@localhost ~]# date
Sat Nov 24 10:10:12 CST 2018

  1. 这里我们选择ntp.api.bz作为我们的ntp服务器,同步时间

ntp.api.bz 是一组NTP服务器集群,目前有6台服务器,位于上海电信

[root@localhost ~]# ntpdate ntp.api.bz
 3 Jan 11:55:40 ntpdate[18861]: step time server 47.98.146.45 offset 3462299.644905 sec
[root@localhost ~]# date
Thu Jan  3 11:55:55 CST 2019

可以看到时间已经同步成功了,如果时间还是不对,那就是时区设置的不对。

查看当前时区:
[root@localhost ~]# date -R
Wed, 02 Jan 2019 23:49:10 -0700
如果不是+0800,则修改时区:
[root@localhost ~]# cp /usr/share/zoneinfo/Canada/Mountain /etc/localtime
重新查看:
[root@localhost ~]# date -R
Thu, 03 Jan 2019 14:51:18 +0800
最后重新同步时间即可。
  1. 写入硬件时间
#查看硬件时间
[root@localhost ~]# hwclock -r
Thu 03 Jan 2019 02:55:42 PM CST  -0.114605 seconds
#写入硬件时间
[root@localhost ~]# hwclock -w

如果没有将当前时间写入硬件时间,那么重新启动服务器后,系统时间会根据之前错误的硬件时间生成错误的系统时间。

自定义ntp服务器时间同步

自定义ntp服务器适用于无网集群环境,在不能访问外网的情况下,保证集群内服务器时间一致。

  1. 在集群中选定一台服务器作为ntp server服务器。
#安装ntp服务
[root@ntp-server ~]# yum install ntp.x86_64                                                                  
......
Installed:
  ntp.x86_64 0:4.2.6p5-28.el7.centos

Dependency Installed:
  ntpdate.x86_64 0:4.2.6p5-28.el7.centos

Complete!
#编辑ntp配置文件
[root@ntp-server ~]# vim /etc/ntp.conf
  8 #restrict default nomodify notrap nopeer noquery			#default默认为所有主机
  9 restrict 192.168.137.0 mask 255.255.255.0 nomodify notrap	#设置成自己集群所在的网段,允许该网段其它服务器同步时间
 22 #server 0.centos.pool.ntp.org iburst						#注释掉默认的时钟源
 23 #server 1.centos.pool.ntp.org iburst
 24 #server 2.centos.pool.ntp.org iburst
 25 #server 3.centos.pool.ntp.org iburst
 26 server 127.127.1.0														#设置本机为时钟源
:wq
#启动ntp服务并开机自启
[root@ntp-server ~]# systemctl start ntpd.service
[root@ntp-server ~]# systemctl enable ntpd.service
  1. ntp客户端服务器配置
#安装ntpdate
[root@ntp-client ~]# yum install ntpdate -y
[root@ntp-client ~]# date
Sun Oct 16 00:00:02 CST 2016
[root@ntp-client ~]# ntpdate 192.168.137.4					#指定ntp-server
 3 Jan 15:32:48 ntpdate[21049]: step time server 192.168.137.4 offset 69953545.264845 sec
[root@ntp-client ~]# date
Thu Jan  3 15:32:52 CST 2019
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值