NTP时间同步服务器客户端、服务端的安装以及配置文件的配置

     NTP(Network Time Protocol)服务器是用于局域网服务器时间同步使用的,可以保证局域网所有的服务器与时间服务器的时间保持一致,应用于对时间实时性要求高的、必须统一时间事件。

                                 服务端安装


一、安装ntp服务      

yum install ntp ntpdate -y

二、修改ntp.conf配置文件

cp /etc/ntp.conf /etc/ntp.conf.bak   #对ntp.conf文件进行备份

     修改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.
#开启内部递归网络接口lo 即允许本机地址一切的操作
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


service 192.168.75.132 
 # 这句也是手动增加的,指明局域网中作为NTP服务器的IP

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# 互联网上的三个ntp服务器。server用来指定要同步的时间服务器的ip地址
# 如果局域网环境下,注释掉下面默认的网络服务器地址,改成自己的局域网时间服务器IP地址
# 其中后面perfer表示优先,常用的有iburst,burst)
server 0.rhel.pool.ntp.org    
server 1.rhel.pool.ntp.org
server 2.rhel.pool.ntp.org 

#sebroadcast 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指定的服务器无法使用,就使用本机时钟作为ntp服务时间来源
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    
# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats

     对配置文件中的restrict进行说明: 

下面详细对restrict进行介绍,这是一个权限控制的语句。语法如下:

    ask [netmask_IP] [parameter]

如果后面不加任何参数 则表示允许对面ip或ip段的任何操作。

如:restrict 127.0.0.1

常用的参数有:

ignore: 拒绝所有类型的 NTP 联机;

nomodify: 客户端不能使用 ntpc 与 ntpq 这两支程序来修改服务器的时间参数, 但客户端仍可透过这部主机来进行网络校时的;

noquery: 客户端不能够使用 ntpq, ntpc 等指令来查询时间服务器,等于不提供 NTP 的网络校时;

notrap: 不提供 trap 这个远程事件登录的功能。

notrust: 拒绝没有认证的客户端。

三、启动NTP服务

service ntpd start

     为了使服务可以在系统引导的时候自动启动,执行 

service ntpd start chkconfig ntpd on

                                 客户端配置


一、客户端安装ntp服务

yum install ntp ntpdate -y

二、配置ntp.conf文件 。主要是添加时间服务器的IP地址

配置文件未做特别指出则与服务器端配置一致

tinker panic 0 
(这行意思是设置客户端与服务器端时间差距,默认最大为1000,为0为不限制。)

server 192.168.245.128 prefer  minpoll 3 maxpoll 3  
(minpoll表示客户端向服务器端同步的最少时间,单位为2的次幂最小为3,秒)

server 192.168.245.130 iburst  minpoll 3 maxpoll 3
(maxpoll表示客户端向服务器端同步的最长时间,单位为2的次幂最大为10,秒)

三、手动同步时间

ntpdate 服务器IP或域名

                                 检查连接是否成功


一、ntpq -p命令参数:此命令可以查看客户端和服务器端连接情况

remote:NTP 主机的 IP 或主机名,左边的符号含义:
    如果有『 * 』代表目前正在作用当中的上层 NTP
    如果是『 + 』代表也有连上线,而且可作为下一个提供时间更新的候选者。
    如果是『 - 』 表示为不合格的ntp服务器
refid:上一层 NTP 主机的地址
st:远程服务器的层级别,0-16,0为最高层,
when:几秒钟前曾经做过时间同步更新的动作;
poll:下一次更新在几秒钟之后;
reach:已经向上层 NTP 服务器要求更新的次数
delay:网络传输过程当中延迟的时间,单位为 10^(-6) 秒
offset:时间补偿的结果,单位与 10^(-3) 秒
jitter:Linux 系统时间与 BIOS 硬件时间的差异时间, 单位为 10^(-6) 秒。

二、date修改系统时间,然后重启ntpd服务

date命令是显示或设置系统时间与日期。
语法
    date(选项)(参数)
选项
    -d<字符串>:显示字符串所指的日期与时间。字符串前后必须加上双引号;
    -s<字符串>:根据字符串来设置日期与时间。字符串前后必须加上双引号;
    -u:显示GMT;
    --help:在线帮助;
    --version:显示版本信息。
参数
    <+时间日期格式>:指定显示时使用的日期时间格式。
日期格式字符串列表
    %H 小时,24小时制(00~23)
    %I 小时,12小时制(01~12)
    %k 小时,24小时制(0~23)
    %l 小时,12小时制(1~12)
    %M 分钟(00~59)
    %p 显示出AM或PM
    %r 显示时间,12小时制(hh:mm:ss %p)
    %s 从1970年1月1日00:00:00到目前经历的秒数
    %S 显示秒(00~59)
    %T 显示时间,24小时制(hh:mm:ss)
    %X 显示时间的格式(%H:%M:%S)
    %Z 显示时区,日期域(CST)
    %a 星期的简称(Sun~Sat)
    %A 星期的全称(Sunday~Saturday)
    %h,%b 月的简称(Jan~Dec)
    %B 月的全称(January~December)
    %c 日期和时间(Tue Nov 20 14:12:58 2012)
    %d 一个月的第几天(01~31)
    %x,%D 日期(mm/dd/yy)
    %j 一年的第几天(001~366)
    %m 月份(01~12)
    %w 一个星期的第几天(0代表星期天)
    %W 一年的第几个星期(00~53,星期一为第一天)
    %y 年的最后两个数字(1999则是99)
实例
    格式化输出:
        date +"%Y-%m-%d"
        2009-12-07

                               连接失败解决办法


一、有防火墙阻断了与server之间的通讯,即123端口是否正常开放;

    检查ntp server的防火墙。可能是server的防火墙屏蔽了upd 123端口。可以用如下命令来关掉iptables服务后再尝试从ntp客户端的同步,如果成功,证明是防火墙的问题,需要更改iptables的设置。

service iptables stop

    在vi /etc/sysconfig/iptables添加如下规则:

-A INPUT -m state --state NEW -m udp -p udp --dport 123 -j ACCEPT 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章