linux 内网NTP 时间同步服务搭建

CentOS7 搭建内网NTP服务器

安装NTP服务

1
[root@localhost ~]# yum install -y ntp

修改配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost ~]# vim /etc/ntp.conf

# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.101.0 mask 255.255.255.0 nomodify notrap # 添加本行

# 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 # 注释本行
server 127.127.1.0 # 添加本行
fudge 127.127.1.0 stratum 10 # 添加本行

启动NTP

1
2
3
4
[root@localhost ~]# systemctl start ntpd
[root@localhost ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
[root@localhost ~]#

查看服务器状态:

1
2
3
4
5
[root@localhost ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*LOCAL(0) .LOCL. 10 l 1 64 1 0.000 0.000 0.000
[root@localhost ~]#

在局域网另找一台机器,

1 安装NTP
2 ntpdate 192.168.101.199 # 是NTP Server 192.168.101.199
3 使用 date 命令验证与NTP服务器时间是否统一

定时同步时间:

1
2
3
4
[root@localhost ~]# vim /etc/cron.hourly/ntp.sh
#!/bin/bash
/usr/sbin/ntpdate 192.168.101.199
[root@localhost ~]# chmod +x /etc/cron.hourly/ntp.sh