系统环境CentOS7
搭建NTP服务器
1.查看服务器是否安装ntp
rpm -qa | grep ntp
2.安装ntp
yum install -y ntp
3.修改ntp配置文件
vim /etc/ntp.conf
把配置文件的下面四行注释/删除 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
在配置文件里添加下面四行 server 0.cn.pool.ntp.org iburst server 1.cn.pool.ntp.org iburst server 2.cn.pool.ntp.org iburst server 3.cn.pool.ntp.org iburst
4.启动ntp服务,并设置开机自启动
systemctl start ntpd systemctl enable ntpd
5.查询ntp是否同步
ntpq -p
6.开启防火墙ntp默认端口udp123
firewall-cmd --permanent --zone=public --add-port=123/udp firewall-cmd --reload
NTP客户端配置
安装过程跟上文一致
1.修改ntp配置文件,将上面的NTP服务器作为客户端同步NTP时间服务器
vim /etc/ntp.conf #配置允许NTP Server时间服务器主动修改本机的时间 restrict 192.168.0.15 nomodify notrap noquery #注释掉其他时间服务器 #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 #配置时间服务器为本地搭建的NTP Server服务器 server 服务端IP地址
2.与NTP server服务器同步时间
ntpdate -u 服务端IP地址
3.查看NTP同步状态
ntpq -p
4.设置定时任务
crontab -e 或者 vim /etc/crontab 0 12 * * * * /usr/sbin/ntpdate 服务端IP 每天的12点整,同步一次时间
备注
1.删除原本已安装的ntp
yum -y remove ntpXXXX
2.ntpd和ntpdate的区别
在Windows中,系统时间会自动保存在BIOS的时钟里面,启动计算机的时候,系统会自动在BIOS里取硬件的时间,以保证时间的不间断。
但在Linux下,默认的情况下系统时间和硬件时间并不会自动同步。在Linux运行过程中,系统时间和硬件时间以异步的方式运行,互不干扰。硬件时间运行,是靠BIOS电池来维持,而系统时间,是用CPU tick来维持。