NTP配置实践

来源:互联网 发布:log4j数据存进mongodb 编辑:程序博客网 时间:2024/05/01 23:35

前言

NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议。无论是平时使用的私人计算机还是在工作中搭建的服务器集群,时间的统一性和准确性是十分重要的。本文以自己公司的NTP配置实践过程为例,过程本身并不复杂,原理部分请参考扩展阅读。

时间同步真的很重要


更新历史

2016年03月16日 - 更新rhel6.3的NTP配置
2015年06月26日 - 初稿

阅读原文 - http://wsgzao.github.io/post/ntp/

扩展阅读

  • NTP - http://www.ntp.org/
  • NTP 服务器 - http://vbird.dic.ksu.edu.tw/linux_server/0440ntp.php

准备工作

检查NTP是否安装

如果缺少NTP服务,请从官方下载适合自己系统的版本

1
2
3
4
5
#检查rpm包 
rpm -qa | grep ntp

ntp-4.2.4p8-2.el6.x86_64
ntpdate-4.2.4p8-2.el6.x86_64

手动修改时间

选择正确的时区,控制误差在5分钟以内

1
2
3
4
5
6
7
8
9
10
11
12
13
#查看当前时间
date

20081212日 星期五 14:44:12 CST
Fri Jun 26 14:51:37 CST 2015

#手动修改时间(月/日/年时:分:秒)
date -s "1/1/09 00:01"

20090101日 星期四 00:01:00 CST

#手动修改分秒
date -s 21:21

NTP配置

NTP服务端配置

服务端时间来源和策略限制根据实际情况设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#手动同步测试
ntpdate 0.rhel.pool.ntp.org

#编辑ntp配置文件
vi /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 kod nomodify notrap nopeer noquery
#restrict -6 default kod 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 -6 ::1
# Hosts on local network are less restricted.
#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.rhel.pool.ntp.org
server 1.rhel.pool.ntp.org
server 2.rhel.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
# Undisciplined Local Clock. This is a fake driver intended for backup
# and when no outside source of synchronized time is available.
#server 127.127.1.0 # local clock
#fudge 127.127.1.0 stratum 10
# 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

#加入自动启动
chkconfig ntpd on

#启动ntp服务
service ntpd start
service ntpd status
service ntpd stop

NTP客户端配置

如无特殊要求,一般使用crontab计划任务同步时间

1.crontab计划任务方式配置

1
2
crontab -e
0 0 * * * /usr/sbin/sntp -P no -r 172.16.0.3;hwclock -w

2.以服务进程方式实时同步

1
2
3
4
5
6
7
8
9
10
11
#编辑客户端配置文件
vi /etc/ntp.conf
server 198.16.2.135

#启动服务
chkconfig ntpd on
service ntpd restart

#如果相差时间较长,请先手动同步
sntp -P no -r 198.16.2.135
ntpdate –d 198.16.2.135

检查NTP同步状态

NTP协议为UDP端口是123,offset时间偏移量以毫秒为单位,注意防火墙的控制策略

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#查看服务连接和监听
ntp:~ # netstat -tlunp | grep ntp
udp 0 0 198.17.1.224:123 0.0.0.0:* 28916/ntpd
udp 0 0 198.16.2.224:123 0.0.0.0:* 28916/ntpd
udp 0 0 127.0.0.2:123 0.0.0.0:* 28916/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 28916/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 28916/ntpd
udp 0 0 ::1:123 :::* 28916/ntpd
udp 0 0 fe80::250:56ff:feb9:123 :::* 28916/ntpd
udp 0 0 fe80::250:56ff:feb9:123 :::* 28916/ntpd
udp 0 0 :::123 :::* 28916/ntpd

#查看网络中的NTP服务器,同时显示客户端和每个服务器的关系
ntp:~ # ntpq -p

remote refid st t when poll reach delay offset jitter
==============================================================================
*202.120.2.101 202.118.1.81 3 u 5 1024 377 265.748 -2.819 16.138
LOCAL(0) .LOCL. 10 l 34 64 377 0.000 0.000 0.001
0 0
原创粉丝点击