NTP
概述
-
NTP是网络时间协议(Network Time Protocol),它是用来同步网络中各个计算机的时间的协议。
-
时间同步的原理:
- 标准时间:由原子钟报时的国际标准时间UTC(Universal Time Coordinated,世界协调时)
- NTP授时网站:http://www.ntp.org.cn/pool
时间同步设定
语法:
ntpdate NTP服务器的IP地址或域名(通过NTP授时网站获取,建议使用域名,因为ip有多个且不稳定)
查看系统时间
[root@server1 ~]# date
2021年 08月 17日 星期二 21:40:41 CST
连接NTP服务器同步时间
[root@server1 ~]# yum install -y ntp
[root@server1 ~]# ntpdate cn.ntp.org.cn
17 Aug 21:45:08 ntpdate[12593]: step time server 120.197.116.202 offset 2.239298 sec
自动同步
[root@server1 ~]# systemctl start ntpd
[root@server1 ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
ntp服务器搭建
通过外网进行时间同步,需要消耗一定的时间,企业中往往不可取,一般都是自建ntp服务器
设置环境
[root@server1 ~]# hostnamectl set-hostname ntp
[root@server1 ~]# su
[root@ntp ~]# systemctl stop firewall
[root@ntp ~]# systemctl disable firewalld
[root@ntp ~]# setenforce 0
[root@ntp ~]# sed -i 's/enforced/disabled/' /etc/selinux/config
[root@ntp ~]# yum clean all
[root@ntp ~]# yum makecache
安装
[root@ntp ~]# yum install -y ntp
[root@ntp ~]# vim /etc/ntp.conf
restrict 127.0.0.1
restrict ::1
restrict 192.168.226.0 mask 255.255.255.0 #添加192.168.226.0网段
同步时间,保证服务器时间准确
[root@ntp ~]# ntpdate cn.ntp.org.cn
启动服务,定期时间同步
[root@ntp ~]# systemctl start ntpd
[root@ntp ~]# systemctl enable ntpd
-------------------------------------------------------------------
客户端手动时间同步
[root@client ~]# ntpdate 192.168.226.10
30 Aug 00:51:21 ntpdate[11789]: step time server 192.168.226.10 offset 709941.283099 sec
定时同步
[root@client ~]# crontab -e
* * * * * /usr/sbin/ntpdate 192.168.226.10 &>/dev/null