CentOS 6.5 学习笔记 搭建NTP服务器

NTP 服务器用于时间同步,局域网内可搭建一台 NTP 服务器,客户端连接 NTP 服务器获取时间校准服务,

从而使所有客户端的时间保持一致.

 

连接方式: 上游 ntp 服务器(外网) --- ntp 服务器(外网/内网) --- 客户端(内网)

 

本地 ntp 服务器从上游 ntp 服务器获得时间校准服务,然后向本地内网的客户端提供时间校准服务.

如果上游 ntp 服务不可用(断网,故障等),则本地 ntp 服务器以本地时间为准,向客户端提供时间校准服务.

国内推荐使用阿里云作为上游 ntp 服务器,国外可使用 pool.ntp.org

 

1 服务器配置

>>> 1.1 安装 ntp 服务 

[root@ ~]#: yum install ntp ntpdate -y
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
base                      | 3.7 kB 00:00

...

Complete!  # 安装完成

 

>>> 1.2 修改配置文件 

[root@ ~]#: cp /etc/ntp.conf /etc/ntp.conf.bak20190903  # 备份

[root@ ~]#: vi /etc/ntp.conf  # 清空原有,输入以下:

restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1

restrict 192.168.10.0 mask 255.255.255.0 nomodify notrap  # 允许客户端从此处获取时间

 

server ntp1.aliyun.com  # 使用上游 ntp 服务器 ntp1.aliyun.com 阿里云

server time1.aliyun.com 

server pool.ntp.org    

 

restrict ntp1.aliyun.com nomodify notrap noquery # 允许上游服务器修改本机时间

restrict time1.aliyun.com nomodify notrap noquery 

restrict pool.ntp.org nomodify notrap noquery

 

server 127.127.1.0             # local clock, 本地时钟,

fudge 127.127.1.0 stratum 10   # 若外部时间服务器不可用,则用本地时间为客户端提供服务

includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys

# 保存退出

 

>>> 1.3 添加 iptables 规则 

允许 ntp 服务器监听 udp 端口 123

[root@ ~]#: cp /etc/sysconfig/iptables /etc/sysconfig/iptables.bak20190913  # 备份

[root@ ~]#: vi /etc/sysconfig/iptables  # 添加规则

1 # Firewall configuration written by system-config-firewall
2 # Manual customization of this file is not recommended.
...
12 -A FORWARD -j REJECT --reject-with icmp-host-prohibited

13 -A INPUT -m state --state NEW -m udp -p udp --dport 123 -j ACCEPT  # 添加于此
14 COMMIT

# 保存退出

配置参数简介: 

ignore: 关闭所有 ntp 联机服务 

nomodify: 客户端可通过服务器同步时间,但不能修改服务器的时间 

notrap: 拒绝特殊的 ntpdq 捕获消息 

noquery: 拒绝 btodq/ntpdc 查询 

server: 添加上游 ntp 服务器 

 

>>> 1.4 重启 iptables 

[root@ ~]#: /etc/init.d/iptables restart
iptables:将链设置为政策 ACCEPT:filter [确定]
iptables:清除防火墙规则: [确定]
iptables:正在卸载模块: [确定]
iptables:应用防火墙规则: [确定]

 

>>> 1.5 开启 ntp 服务 

[root@ ~]#: /etc/init.d/ntpd start
正在启动 ntpd: [确定]

 

>>> 1.6 查看 ntp 服务器

[root@ ~]#: ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*LOCAL(0) .LOCL. 10 l 17 64 1 0.000 0.000 0.000                # 本地 ntp 服务器(自身)
sv1.ggsrv.de 205.46.178.169 2 u 17 64 1 387.291 0.780 0.000    # 上层 ntp 服务器

 

>>> 1.7 设置 ntpd 服务开机启动

[root@ ~]#: chkconfig --add ntpd # 添加

[root@ ~]#: chkconfig ntpd on  # 开启

[root@ ~]#: chkconfig --list | grep "ntp"  # 查看
ntpd 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭

 

 

2 客户端配置

>>> 2.1 客户端安装 ntp 服务  

[root@ ~]#: yum install ntp ntpdate -y
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
base                      | 3.7 kB 00:00

...

Complete!  # 安装完成

 

>>> 2.2 修改配置文件  

[root@ ~]#: cp /etc/ntp.conf /etc/ntp.conf.bak20190903  # 备份

[root@ ~]#: vi /etc/ntp.conf  # 清空原有,输入以下:

driftfile /var/lib/ntp/drift

restrict default kod nomodify notrap nopeer noquery

restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys

server 192.168.10.61  # NTP 服务器IP
restrict 192.168.10.61 nomodify notrap noquery
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10

# 保存退出

 

>>> 2.3 手动同步时间 

[root@ ~]#: ntpdate -d 192.168.10.61  # ntp 服务器IP
...
192.168.10.61: Server dropped: no data  # 报错 no data 
server 192.168.10.61, port 123
...

3 Sep 13:11:12 ntpdate[1594]: no server suitable for synchronization found  # 报错 no server

 

排错1: 在 ntp 服务器上关闭 iptables  

[root@ ~]#: /etc/init.d/iptables stop  # 此行在服务器上操作
iptables:将链设置为政策 ACCEPT:filter [确定]
iptables:清除防火墙规则: [确定]
iptables:正在卸载模块: [确定]

再次手动同步时间

[root@ ~]#: ntpdate -d 192.168.10.61  # 此行在客户端操作
...
Looking for host 192.168.10.61 and service ntp
...
server 192.168.10.61, port 123
...

3 Sep 13:18:06 ntpdate[1606]: adjust time server 192.168.10.61 offset -0.008476 sec

可以正常获取服务器时间.

 

排错2:关闭 iptables 不安全,修改服务器 iptables 配置文件

 

[root@ ~]#: vi /etc/sysconfig/iptables  # 此行在服务器上操作

# Firewall configuration written by system-config-firewall

# Manual customization of this file is not recommended.

*filter

:INPUT ACCEPT [0:0]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [0:0]

#-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

#-A INPUT -p icmp -j ACCEPT

#-A INPUT -i lo -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT # 保留 ssh

#-A INPUT -j REJECT --reject-with icmp-host-prohibited

#-A FORWARD -j REJECT --reject-with icmp-host-prohibited

-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT # 保留 ssh

-A INPUT -p udp -m udp --dport 123 -j ACCEPT # 添加此规则

 

COMMIT

# 保存退出

客户端可以正常获取服务器时间.

按照其它教程的说明,添加的规则是 -A INPUT -m state --state NEW -m udp -p udp --dport 123 -j ACCEPT 

但是添加后无法正常使用,原因不明.

 

注意,服务器开启 NTP 服务后,客户端需要等待几分钟之后,才可以从服务器获取时间,否则报错.

 

>>> 2.4 客户端自动校时

> 2.4.1 新建 log 文件,记录 ntpdate 运行情况

[root@ logs]#: mkdir -p /etc/logs/  # 新建目录

[root@ logs]#: touch /etc/logs/ntpdate.log   # 新建日志文件

 

> 2.4.2 使用 contab 计划任务,设置客户端自动校时. 

[root@ logs]#: vi /etc/crontab  # 修改配置
# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed 

  *  */2  *  *  *  /usr/sbin/ntpdate 192.168.10.61 >> /etc/logs/ntpdate.log 2>&1  # 添加此行 

  # 每两个小时执行一次命令 /usr/sbin/ntpdate 192.168.10.61 >> /etc/logs/ntpdate.log 2>&1

  # 保存退出.

 

ddns-update-style interim;ignore client-updates;
subnet 192.168.1.0 netmask 255.255.255.224 {
# --- default gateway        option routers                  192.168.1.254;        option subnet-mask              255.255.255.224;
        option nis-domain               "domain.org";        option domain-name              "manage.com";        option domain-name-servers      192.168.1.32;
        option time-offset              -28800; # Eastern Standard Time#       option ntp-servers              192.168.1.1;#       option netbios-name-servers     192.168.1.1;# --- Selects point-to-point node (default is hybrid). Don't change this unless# -- you understand Netbios very well#       option netbios-node-type 2;
        range dynamic-bootp 192.168.1.1 192.168.1.30;        default-lease-time 21600;        max-lease-time 43200;
        # we want the nameserver to appear at a fixed address#       host ns {#               next-server marvin.redhat.com;#               hardware ethernet 12:34:56:78:AB:CD;#               fixed-address 207.175.42.254;#       }}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值