使用ntp服务同步时间

来源:互联网 发布:芈月传 大秦帝国 知乎 编辑:程序博客网 时间:2024/04/20 07:43

今天要在公司的集群上做时间同步,一台机子作为ntp服务器,其余的当做ntp客户端。上网搜了一些资料,总结如下:

ntp服务端配置

  1. 修改ntp配置文件
# vi /etc/ntp.conf  

注意修改中文处注释

# Hosts on local network are less restricted.# 允许内网中其他机器同步时间restrict 192.168.0.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 210.72.145.44 perfer      # 中国国家受时中心server 202.112.10.36             # 1.cn.pool.ntp.orgserver 59.124.196.83             # 0.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# allow update time by the upper server # 允许上层时间服务器主动修改本机时间restrict 210.72.145.44 nomodify notrap noqueryrestrict 202.112.10.36 nomodify notrap noqueryrestrict 59.124.196.83 nomodify notrap noquery

配置文件中有两种关键配置项,一种是server,代表上层时间服务器,本机可以从这些IP上获取时间;一种是restrict,约束了ntp服务的权限。

说下restrict配置。restrict的格式是:

restrict [IP] mask [netmask_IP] [parameter]

parameter主要有以下选项

选项 意义 nomodify 客户端不能修改服务器时间,但是可以从服务器获取时间 notrap 客户端不能使用trap(远端事件登录功能remote event logging) noquery 其他客户端不能从本机获取时间

2. 启动ntp服务

# sudo service ntpd start
  1. 查看服务连接与监听端口
# sudo netstat -tlunp | grep ntp udp        0      0 172.24.209.222:123      0.0.0.0:*                           7554/ntpd       udp        0      0 127.0.0.1:123           0.0.0.0:*                           7554/ntpd       udp        0      0 0.0.0.0:123             0.0.0.0:*                           7554/ntpd       udp6       0      0 fe80::20c:29ff:fe23:123 :::*                                7554/ntpd       udp6       0      0 ::1:123                 :::*                                7554/ntpd       udp6       0      0 :::123                  :::*                                7554/ntpd   

第一行说明ntp服务已经启动,ntp默认监听123端口。


ntp客户端配置

  1. 修改ntp.conf,按照上面的方法配置server为刚才设置的服务器ip,并增加restrict配置(参数nomodify noquery notrap)
server 192.168.0.102restrict 192.168.0.102 nomodify notrap noquery
  1. 启动ntp服务。启动服务后等待几分钟,再检查时间同步是否成功。
# sudo service ntpd start
  1. 手动同步。也可以使用下面命令手动同步时间,下面例子中,192.168.0.102是ntp服务器的地址。
# ntpdate -u 192.168.0.102  

当ntp自动同步有问题时,可以在crontab中加入如下命令(每5分钟同步一次时间):

# crontab -e*/5 * * * * /usr/sbin/ntpdate -u 192.168.0.102
1 0
原创粉丝点击