什么是NTP
NTP是用来使计算机时间同步化的一种协议,全称是Network Time Protocol。它可以在大规模的设备范围内同步矫正时间到几ms级别的精度,在网络稳定的局域网内,精度甚至可以达到微秒级别。 ntp端口使用的是 123
手动下载安装包地址
http://mirror.centos.org/centos/7/os/x86_64/Packages/ntp-4.2.6p5-29.el7.centos.2.x86_64.rpm
http://mirror.centos.org/centos/7/os/x86_64/Packages/ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm
一、ntp服务器时间同步
1.获取阿里云服务器时间同步到服务器
关闭防火墙
systemctl stop firewalld
setenforce 0
手动随便设定个时间:
date -s '2010-10-10 10:10:10'
一般Linux安装的时候默认已安装NTP服务,安装前检查是否已安装
[root@localhost ~]# rpm -qa|grep ntp
ntp-4.2.6p5-29.el7.centos.2.x86_64
ntpdate-4.2.6p5-29.el7.centos.2.x86_64
如果没有安装就执行下面的命令进行安装:
yum -y install ntp
修改配置文件:
vim /etc/ntp.conf
#注销默认Server
#server 0.rhel.pool.ntp.org iburst
#server 1.rhel.pool.ntp.org iburst
#server 2.rhel.pool.ntp.org iburst
#server 3.rhel.pool.ntp.org iburst
#添加:
server ntp1.aliyun.com prefer #阿里云的服务器时间
启动ntp
systemctl start ntpd
等待5到10分钟,执行 ntpq -p就可以看到 服务器已经与阿里云服务器的时间同步了
查看服务器ntp服务状态:
[root@centos6 ~]# ntpstat
[root@localhost ~]# ntpstat
unsynchronised
time server re-starting
polling server every 8 s
表示同步上级时间服务器成功。
立即查看时间没有同步,不用担心,等待5到10分钟后再次查看
再次查看时间date:
date
timedatectl
systemctl enable ntpd.service
2.同步服务器
修改主服务器配置文件:
server ntp1.aliyun.com prefer
下面添加3行:
server 127.127.1.0
# local clock#本地的时间服务器,本机,当上面那个不管事的时候,客户端就同步本地时间
fudge 127.127.1.0 stratum 15
restrict 192.168.131.0 mask 255.255.255.0 nomodify notrap
修改同步服务器时间和配置文件:
手动修改时间:
date -s '2010-10-10 10:10:10'
vim /etc/ntp.conf
#注销默认Server
#server 0.rhel.pool.ntp.org iburst
#server 1.rhel.pool.ntp.org iburst
#server 2.rhel.pool.ntp.org iburst
#server 3.rhel.pool.ntp.org iburst
添加:
server 192.168.131.87 prefer #主服务器的ip
保存退出
启动ntp
systemctl start ntpd
[root@localhost ~]# ntpdate 192.168.131.87
10 Oct 10:14:15 ntpdate[5961]: the NTP socket is in use, exiting
这时候会提示报错,socket被占用,无法通信。
这时查看ntp状态为,提示同步不成功。
[root@localhost ~]# ntpstat
unsynchronised
time server re-starting
polling server every 8 s
不用担心,等待5到10分钟后,在执行ntpstat。
[root@localhost ~]# ntpstat
unsynchronised
polling server every 64 s
表示同步上级时间服务器成功。
查看时间
date
timedatectl
设置开机重启
systemctl enable ntpd.service