CentOS7之NTP服务器简单配置
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS7之NTP服务器简单配置相关的知识,希望对你有一定的参考价值。
时间服务器
NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议。
NTP服务器就是利用NTP协议提供时间同步服务的。
ntp软件(支持ntp协议) CentOS6自带
CentOS7需要安装的
chrony软件(支持ntp协议) CentOS7自带
环境准备
[[email protected] ~]# getenforce
Disabled
[[email protected] ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
[[email protected] ~]# hostname -I
10.0.0.62 172.16.1.62
第一种方法:NTP
服务端部署
-
下载软件
[[email protected] ~]# yum install -y ntp
[[email protected] ~]# rpm -qa ntp
ntp-4.2.6p5-25.el7.centos.2.x86_64 - 配置ntp文件
备份
[[email protected] ~]# cp /etc/ntp.conf{,.ori}
修改
[[email protected] ~]# vim /etc/ntp.conf
8 #restrict default nomodify notrap nopeer noquery
9 restrict default nomodify
21 #server 0.centos.pool.ntp.org iburst
22 #server 1.centos.pool.ntp.org iburst
23 #server 2.centos.pool.ntp.org iburst
24 #server 3.centos.pool.ntp.org iburst
25 server ntp1.aliyun.com
- 与备份文件对比
[[email protected] ~]# diff /etc/ntp.conf{,.ori}
8,9c8,9
< #restrict default nomodify notrap nopeer noquery
< restrict default nomodify
restrict default nomodify notrap nopeer noquery
21,25c21,25
< #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 ntp1.aliyun.comserver 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
- 启动服务
[[email protected] ~]# systemctl start ntpd
- 检查ntp是否能同步
[[email protected] ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
time5.aliyun.co 10.137.38.86 2 u 36 64 1 52.686 -1371.9 0.000
[[email protected] ~]# ntpstat
synchronised to NTP server (182.92.12.11) at stratum 3
time correct to within 982 ms
polling server every 64 s
客户端部署
-
下载软件
[[email protected] ~]# yum -y install ntpdate -
测试是否同步
[[email protected] ~]# ntpdate 172.16.1.62
10 Jan 09:07:47 ntpdate[1504]: adjust time server 172.16.1.62 offset -0.001513 sec - 定时任务
[[email protected] ~]# crontab -l
#时间同步
/5 * /usr/sbin/ntpdate 172.16.1.62 &>/dev/null
第二种方法:chrony
服务端部署
-
修改chrony配置文件
[[email protected] ~]# vim /etc/chrony.conf
1 # Use public servers from the pool.ntp.org project.
2 # Please consider joining the pool
(http://www.pool.ntp.org/join.html).
3 #server 0.centos.pool.ntp.org iburst
4 #server 1.centos.pool.ntp.org iburst
5 #server 2.centos.pool.ntp.org iburst
6 #server 3.centos.pool.ntp.org iburst
7 server ntp1.aliyun.com26 # Allow NTP client access from local network.
27 #allow 192.168.0.0/16
28 allow 172.16.1.0/24 - 开启服务
[[email protected] ~]# systemctl start chronyd
客户端部署 - 修改配置文件
[[email protected] ~]# vim /etc/chrony.conf
1 # Use public servers from the pool.ntp.org project.
2 # Please consider joining the pool (http://www.pool.ntp.org/join.html).
3 #server 0.centos.pool.ntp.org iburst
4 #server 1.centos.pool.ntp.org iburst
5 #server 2.centos.pool.ntp.org iburst
6 #server 3.centos.pool.ntp.org iburst
7 server 172.16.1.62
-
开启服务
[[email protected] ~]# systemctl start chronyd -
修改时间测试
[[email protected] ~]# date -s 1111
Wed Jan 10 11:11:00 CST 2018 - 查看日志
[[email protected] ~]# tailf /var/log/messages
Jan 10 09:30:26 Cobbler chronyd[1593]: Selected source 172.16.1.62
Jan 10 09:30:26 Cobbler chronyd[1593]: System clock wrong by -6348.794966 seconds, adjustment started
Jan 10 09:30:26 Cob[[email protected] ~]# date -s 1111
Wed Jan 10 11:11:00 CST 2018
5.查看时间
[[email protected] ~]# date
Wed Jan 10 11:15:47 CST 2018
[[email protected] ~]# date
Wed Jan 10 09:30:37 CST 2018
以上是关于CentOS7之NTP服务器简单配置的主要内容,如果未能解决你的问题,请参考以下文章