linux7配置ntp服务器,centos7 搭建ntp服务器

一、安装ntp服务

首先检查服务器上是否已安装ntp服务:

[root@centos7 /]rpm -qa | grep ntp

ntp-4.2.6p5-22.el7.centos.2.x86_64

ntpdate-4.2.6p5-22.el7.centos.2.x86_64

如果有上述内容输出,测说明ntp服务已安装;否则需要安装ntp服务

[root@centos7 /]yum install -y ntpd

安装有yum、rpm、源码安装等多种方式,本次安装使用yum安装。如果服务器能联网,建议采用yum安装。

二、配置ntp

采用yum或rpm安装情况下,ntp的配置文件路径为/etc/ntp.conf

1、首先找到互联网上提供时间时间同步的ntp server

http://www.pool.ntp.org是NTP的官方网站,在这上面我们可以找到离我们城市最近的NTP Server. NTP建议我们为了保障时间的准确性,最少找两个个NTP Server。

比如在中国可以选择下面几台服务器

server 1.cn.pool.ntp.org

server 0.asia.pool.ntp.org

server 2.asia.pool.ntp.org

2、在配置之前最好先手动和上面的服务器同步一次时间,使服务器时间尽量接近标准时间

[root@centos7 /]ntpdate 1.cn.pool.ntp.org

假如你的时间差的很离谱的话第一次会看到调整的幅度比较大,所以保险起见可以运行两次。 那么为什么在打开NTP服务之前先要手动运行同步呢?1. 因为根据NTP的设置,如果你的系统时间比正确时间要快的话那么NTP是不会帮你调整的,所以要么你把时间设置回去,要么先做一个手动同步

2. 当你的时间设置和NTP服务器的时间相差很大的时候,NTP会花上较长一段时间进行调整.所以手动同步可以减少这段时间

3、配置/etc/ntp.conf(红色部分为修改部分)

# For more information about this file, see the man pages

# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

driftfile /var/lib/ntp/drift

# Permit time synchronization with our time source, but do not

# permit the source to query or modify the service on this system.

restrict default nomodify notrap nopeer noquery

# Permit all access over the loopback interface.  This could

# be tightened as well, but to do so would effect some of

# the administrative functions.

restrict 127.0.0.1

restrict ::1

# Hosts on local network are less restricted.

#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

restrict 192.168.1.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 1.cn.pool.ntp.org  #向该服务器同步时间,下同

server 0.asia.pool.ntp.org

server 2.asia.pool.ntp.org

#broadcast 192.168.1.255 autokey# broadcast server

#broadcastclient# broadcast client

#broadcast 224.0.1.1 autokey# multicast server

#multicastclient 224.0.1.1# multicast client

#manycastserver 239.255.254.254# manycast server

#manycastclient 239.255.254.254 autokey # manycast client

####restrict参数####

#kod 使用kod技术防范“kiss of death”***

#ignore 拒绝任何NTP连接

#nomodify 用户端不能使用ntpc,ntpq修改时间服务器参数,但是可以进行网络校时

#noquery 用户端不能使用ntpc,ntpq查询时间服务器参数,不可以进行网络校时

#notrap 不提供远程日志功能

#notrust 拒绝没有认证的客户端

restrict 1.cn.pool.ntp.org nomodify notrap noquery

restrict 0.asia.pool.ntp.org nomodify notrap noquery

restrict 2.asia.pool.ntp.org nomodify notrap noquery

server 127.0.0.1

fudge 127.127.1.0 stratum 5

#设置本地时间级别是5,如果上级时间服务器均失效,对外发布本地时间,时间层级是4。

#这行是时间服务器的层次。设为0则为顶级,如果要向别的NTP服务器更新时间,请不要把它设为0

# Enable public key cryptography.

#crypto

includefile /etc/ntp/crypto/pw

# Key file containing the keys and key identifiers used when operating

# with symmetric key cryptography.

keys /etc/ntp/keys

# Specify the key identifiers which are trusted.

#trustedkey 4 8 42

# Specify the key identifier to use with the ntpdc utility.

#requestkey 8

# Specify the key identifier to use with the ntpq utility.

#controlkey 8

# Enable writing of statistics records.

#statistics clockstats cryptostats loopstats peerstats

# Disable the monitoring facility to prevent amplification attacks using ntpdc

# monlist command when default restrict does not include the noquery flag. See

# CVE-2013-5211 for more details.

# Note: Monitoring will not be disabled with the limited restriction flag.

disable monitor

配置好之后,启动ntp服务

[root@centos7 /]systemctl start ntpd

[root@centos7 /]ntpq -p #查询运行情况

db791bbcdef1ac29d490c391eb3c3e35.png

更多细节

表头remote – 用于同步的远程节点或服务器。“LOCAL”表示本机 (当没有远程服务器可用时会出现)

refid – 远程的服务器进行同步的更高一级服务器

st – 远程节点或服务器的 Stratum(级别,NTP 时间同步是分层的)

t – 类型 (u: unicast(单播) 或 manycast(选播) 客户端, b: broadcast(广播) 或 multicast(多播) 客户端, l: 本地时钟, s: 对称节点(用于备份), A: 选播服务器, B: 广播服务器, M: 多播服务器, 参见“Automatic Server Discovery“)

when – 最后一次同步到现在的时间 (默认单位为秒, “h”表示小时,“d”表示天)

poll – 同步的频率:rfc5905建议在 NTPv4 中这个值的范围在 4 (16秒) 至 17 (36小时) 之间(即2的指数次秒),然而观察发现这个值的实际大小在一个小的多的范围内 :64 (26 )秒 至 1024 (210 )秒

reach – 一个8位的左移移位寄存器值,用来测试能否和服务器连接,每成功连接一次它的值就会增加,以 8 进制显示

delay – 从本地到远程节点或服务器通信的往返时间(毫秒)

offset – 主机与远程节点或服务器时间源的时间偏移量,offset 越接近于0,主机和 NTP 服务器的时间越接近(以方均根表示,单位为毫秒)

jitter – 与远程节点同步的时间源的平均偏差(多个时间样本中的 offset 的偏差,单位是毫秒),这个数值的绝对值越小,主机的时间就越精确

[root@centos7 /]ntpstat #查看是否同步状态,下图可以看出同步成功

bf0c8095bf75e6b0c100648bfaa4d812.png

如需其它服务器从本机同步时间,还需将ntp服务开放

[root@centos7 /]firewall-cmd --zone=public --add-service=ntp --permanent

[root@centos7 /]firewall-cmd --reload

最后设置开机启动

[root@centos7 /]systemctl enable ntpd

参考资料:http://www.centoscn.com/CentOS/config/2015/1105/6385.html

http://blog.csdn.net/iloli/article/details/6431757

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是在 CentOS 7 上搭建 NTP 服务器的步骤: 1. 安装 NTP:运行以下命令安装 NTP: ``` sudo yum install ntp ``` 2. 配置 NTP:编辑 `/etc/ntp.conf` 文件,配置服务器地址和允许访问的客户端地址。例如,以下是一个示例配置: ``` server 0.centos.pool.ntp.org iburst server 1.centos.pool.ntp.org iburst server 2.centos.pool.ntp.org iburst restrict default nomodify notrap nopeer noquery restrict 127.0.0.1 restrict <客户端IP地址> mask <子网掩码> nomodify notrap ``` 其中,`server` 指定 NTP 服务器地址,`restrict` 用于限制访问控制。 3. 启动 NTP:运行以下命令启动 NTP 服务并设置为开机自启: ``` sudo systemctl start ntpd sudo systemctl enable ntpd ``` 4. 验证 NTP:使用 `ntpq` 命令验证 NTP 是否正常工作。例如,运行以下命令: ``` ntpq -p ``` 如果 NTP 正常工作,将显示类似以下内容的输出: ``` remote refid st t when poll reach delay offset jitter ============================================================================== *ntp1.example.c .GPS. 1 u 154 256 377 0.862 -30.200 1.835 +ntp2.example.c .PPS. 1 u 173 256 377 0.991 -30.230 1.309 -ntp3.example.c .GPS. 1 u 130 256 377 1.003 -29.990 1.935 ``` 其中,`*` 表示当前服务器NTP服务器,`+` 表示当前服务器NTP 辅助服务器,`-` 表示当前服务器NTP 被动服务器。 以上就是在 CentOS 7 上搭建 NTP 服务器的步骤。希望能对你有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值