Linux NTP服务器  (时间服务器) 

 

Date –s 22:12:30   # 设置时间

 

闰秒  71075960

在这个时候,要关闭ntp服务

1.1. 软件安装

所需软件:默认已安装

yum install ntp -y

ntpdate-4.2.6p5-1.el6.centos.i686

fontpackages-filesystem-1.41-1.1.el6.noarch

ntp-4.2.6p5-1.el6.centos.i686

 

1.2. 配置NTP

 

配置文件:

 

vi /etc/ntp.conf

NTP策略

restrict(限制) default kod nomodify(限改) notrap(限级查询) nopeer noquery

restrict -6 default kod nomodify notrapnopeer noquery

# 默认的一些权限  -6IPV6

# 限级查询指的是本机所连接的服务器的上级服务器,不能向上级服务器直接查询

restrict 127.0.0.1      # IPV4

restrict -6 ::1         # IPV6

restrict 192.168.1.0 mask 255.255.255.0nomodify notrap  #指定1.0网段的查询权限

restrict [你的IP] mask [netmask_IP] [parameter]

其中 parameter 的参数主要有底下这些:

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

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

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

  • notrap
        不提供 trap 这个远程事件登录 (remote event logging) 的功能。

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

 

# 如何parameter没有指定任何参数,代表没有任何限制

# 设置当前网段对此网段内的NTP服务器的权限

 

NTP的同步服务器

server [IP or hostname] [prefer]   # 设置上层NTP服务器

server 0.centos.pool.ntp.org iburst       server服务器

server 1.centos.pool.ntp.org iburst

server 2.centos.pool.ntp.org iburst

server 3.centos.pool.ntp.org iburst

server 127.127.1.0                    #主板芯片时钟   必须设置

fudge 127.127.1.1 stratum 10     # 降低本机同步的优先级

driftfile /home/ntp          # driftfile [可以被 ntpd 写入的目录与档案] 记录时钟芯片与上级服务器时间的误差

                         # 档案需要设定成 ntpd 这个 daemon 可以写入的权限。

 

 

设定样板:

·  [root@linux  ~]# vi /etc/ntp.conf

# 在保留大部分的默认值的情况下,我们作了几个小部分的修改喔:

# 1. 先处理权限方面的问题:

restrict default nomodify notrap noquery

restrict 220.130.158.71  <==底下这三行在开放主机进入的权限

restrict 220.130.158.51

restrict 220.130.158.52

restrict 127.0.0.1   mask 255.0.0.0  <==内部与 LAN 的权限

restrict 192.168.1.0 mask 255.255.255.0  nomodify   # 设置允许网段

 

# 2. 设定主机来源!

server 220.130.158.71 prefer  <==以这部主机为最优先

server 220.130.158.51

server 220.130.158.51

 

# 3.原本内定的一个内部时间数据,不需要更动他

server   127.127.1.0     # local clock

fudge    127.127.1.0 stratum 10

 

# 4. 就是那个时间差异分析的咚咚,保留默认值即可。

driftfile /var/lib/ntp/drift

broadcastdelay  0.008

 

# 5.暂时不会使用的 keys 相关认证功能。

keys            /etc/ntp/keys

然后准备一下修订一下 /etc/sysconfig/ntpd 吧!

[root@linux ~]# vi /etc/sysconfig/ntpd

OPTIONS="-u ntp:ntp -p  /var/run/ntpd.pid"

SYNC_HWCLOCK=yes

# 将他改成 yes 吧!这样 BIOS 的时间也会跟着改变的!

 

 

1.3. 端口和服务

 

端口 123

 

Service ntpd restart

 

 

1.4. 防火墙配置

 

1.5. 设置时区

 

vi /etc/sysconfig/clock

 

# The time zone of the system is defined bythe contents of /etc/localtime.

# This file is only for evaluation by system-config-date,do not rely on its

# contents elsewhere.

ZONE="Asia/Shanghai"

 

 

/usr/share/zoneinfo/   所有时区文件

 

/etc/localtime  

 

 

1.6. 客户端同步

 

 

[root@localhost ~]#  ntpstat 查看同步状态

 

 

#客户端要和NTP server进行时钟同步。在客户端执行

[root@localhost ~]#  ntpdate server IP     

 

# 手动让server和时钟芯片同步   当客户端执行ntpdate时,必须服务器端有更新同步才能更新

[root@localhost ~]#  hwclock –w      

 

1.7. 计划任务

Crontab:

47 21 * * * /usr/sbin/ntpdate 192.168.1.200>>/usr/local/logs/crontab/ntpdate.log

 

 

1.8. 同步记录

[root@localhost ~]#  ntpq –p

[root@localhost ~]# ntpq -p

    remote    refid(上一级)  st  t  when poll reach   delay  offset时差  jitter

==============================================================================

 202.118.1.130    .INIT.         16 u    -   64   0    0.000    0.000  0.001

 news.neu.edu.cn  .INIT.        16  u    -  64    0    0.000   0.000   0.000

 dns1.synet.edu.  202.118.1.46     2 u    1   64   1   54.697  2453.85  0.001

[root@localhost ~]#

# st级别

 

 

1.9. 步骤精华  ★★★

 

1.10.NTP管理

[root@localhostmodules]# ntpq -p

offset 时差  

1.11. 常见错误

  • 提示1

[root@node2 ~]# ntpdate 10.17.1.60

11 Aug 11:05:28 ntpdate[7326]: no serversuitable for synchronization found

 

#restrict(限制) default kod nomodify(限改) notrap(限级查询) nopeer noquery

#restrict -6 default kod nomodify notrapnopeer noquery

解答:以上二项至少有一项未注释

 

  • 提示2