CentOS7搭建NTP服务器及客户端同步时间

在配置时钟同步服务器时第一次同步时间时,使用ntpdate命令;后续通过ntpd服务与服务器同步时间。

一、搭建NTP服务器

1、查看服务器、客户端操作系统版本

[root@web ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)

2、查看服务器是否安装ntp,系统默认安装ntpdate;

[root@web ~]# rpm -qa | grep ntp
fontpackages-filesystem-1.44-8.el7.noarch
ntpdate-4.2.6p5-28.el7.centos.x86_64
python-ntplib-0.3.2-1.el7.noarch

3、安装ntp ntpdate,其中ntpdate默认安装,可以只安装ntp;

yum install ntp ntpdate -y

4、查看是否已安装完成,与第2步对比

[root@web ~]# rpm -qa | grep ntp
fontpackages-filesystem-1.44-8.el7.noarch
ntpdate-4.2.6p5-28.el7.centos.x86_64
ntp-4.2.6p5-28.el7.centos.x86_64
python-ntplib-0.3.2-1.el7.noarch

5、查看ntp服务器状态,两条命令效果一样

[root@web ~]# systemctl status ntpd
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

[root@web ~]# service ntpd status
Redirecting to /bin/systemctl status ntpd.service
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

6、修改配置文件,使该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 127.127.1.0 iburst

7、启动ntp服务

systemctl start ntpd

service ntpd start

8、再次查看服务器状态

[root@web ~]# systemctl status ntpd
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2018-08-21 14:29:12 CST; 8s ago
  Process: 6588 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 6589 (ntpd)
    Tasks: 1
   CGroup: /system.slice/ntpd.service
           └─6589 /usr/sbin/ntpd -u ntp:ntp -g

Aug 21 14:29:12 web ntpd[6589]: Listen normally on 2 lo 127.0.0.1 UDP 123
Aug 21 14:29:12 web ntpd[6589]: Listen normally on 3 eno1 192.168.0.163 UDP 123
Aug 21 14:29:12 web ntpd[6589]: Listen normally on 4 virbr0 192.168.122.1 UDP 123
Aug 21 14:29:12 web ntpd[6589]: Listen normally on 5 lo ::1 UDP 123
Aug 21 14:29:12 web ntpd[6589]: Listen normally on 6 eno1 fe80::6e92:bfff:fe6f:daea UDP 123
Aug 21 14:29:12 web ntpd[6589]: Listening on routing socket on fd #23 for interface updates
Aug 21 14:29:12 web ntpd[6589]: 0.0.0.0 c016 06 restart
Aug 21 14:29:12 web ntpd[6589]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM
Aug 21 14:29:12 web ntpd[6589]: 0.0.0.0 c011 01 freq_not_set
Aug 21 14:29:13 web ntpd[6589]: 0.0.0.0 c514 04 freq_mode

或 

[root@web ~]# service ntpd status
Redirecting to /bin/systemctl status ntpd.service
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2018-08-21 14:29:12 CST; 56s ago
  Process: 6588 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 6589 (ntpd)
    Tasks: 1
   CGroup: /system.slice/ntpd.service
           └─6589 /usr/sbin/ntpd -u ntp:ntp -g

Aug 21 14:29:12 web ntpd[6589]: Listen normally on 2 lo 127.0.0.1 UDP 123
Aug 21 14:29:12 web ntpd[6589]: Listen normally on 3 eno1 192.168.0.163 UDP 123
Aug 21 14:29:12 web ntpd[6589]: Listen normally on 4 virbr0 192.168.122.1 UDP 123
Aug 21 14:29:12 web ntpd[6589]: Listen normally on 5 lo ::1 UDP 123
Aug 21 14:29:12 web ntpd[6589]: Listen normally on 6 eno1 fe80::6e92:bfff:fe6f:daea UDP 123
Aug 21 14:29:12 web ntpd[6589]: Listening on routing socket on fd #23 for interface updates
Aug 21 14:29:12 web ntpd[6589]: 0.0.0.0 c016 06 restart
Aug 21 14:29:12 web ntpd[6589]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM
Aug 21 14:29:12 web ntpd[6589]: 0.0.0.0 c011 01 freq_not_set
Aug 21 14:29:13 web ntpd[6589]: 0.0.0.0 c514 04 freq_mode

9、查看是否同步

[root@web ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*LOCAL(0)        .LOCL.           5 l   20   64    7    0.000    0.000   0.000

10、设置开机启动

[root@web ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd
service.

11、设置防火墙,打开udp123端口

[root@web ~]# firewall-cmd --permanent --add-port=123/udp
success
[root@web ~]# firewall-cmd --reload
success

12、查看防火墙已打开端口

iptables -L -n

二、客户端配置

前5步与服务器一致

6、修改配置文件,将刚刚搭建好的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
#配置上游时间服务器为本地的ntpd Server服务器
server 192.168.0.163
#配置允许上游时间服务器主动修改本机的时间
restrict 192.168.0.163 nomodify notrap noquery

7、与本地ntpd Server同步一下

ntpdate -u 192.168.0.163

8、启动ntp服务

systemctl start ntpd

service ntpd start

9、设置开机启动

[root@db3 ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

10、查看状态

[root@db3 ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*192.168.0.163   LOCAL(0)        11 u   21   64  377    0.068   -0.824   0.367

 

  • 13
    点赞
  • 115
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
以下是在CentOS 7上搭建NTP时间服务器的步骤: 1.安装NTP软件包 ```shell yum install ntp -y ``` 2.配置NTP服务器 编辑`/etc/ntp.conf`文件,添加如下内容: ```shell # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). 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 # Allow LAN traffic. restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap ``` 其中,`server`行指定了NTP服务器使用的公共时间服务器,`restrict`行指定了允许访问NTP服务器的IP地址范围。 3.启动NTP服务 ```shell systemctl start ntpd ``` 4.设置NTP服务开机自启动 ```shell systemctl enable ntpd ``` 5.检查NTP服务状态 ```shell systemctl status ntpd ``` 如果NTP服务正在运行,则输出类似以下内容: ```shell ● ntpd.service - Network Time Service Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2021-08-10 14:20:20 CST; 1h 5min ago Main PID: 1234 (ntpd) CGroup: /system.slice/ntpd.service └─1234 /usr/sbin/ntpd -u ntp:ntp -g ``` 6.检查NTP服务器是否正常工作 ```shell ntpq -p ``` 如果NTP服务器正常工作,则输出类似以下内容: ```shell remote refid st t when poll reach delay offset jitter ============================================================================== *0.centos.pool. .POOL. 16 p - 64 0 0.000 0.000 0.000 +1.centos.pool. .POOL. 16 p - 64 0 0.000 0.000 0.000 +2.centos.pool. .POOL. 16 p - 64 0 0.000 0.000 0.000 +3.centos.pool. .POOL. 16 p - 64 0 0.000 0.000 0.000 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值