Linux的时间设置与同步(NTP)

         在oracle10g做rac的时候,尽量要保持2个节点的时间一样,liunx的NTP服务是可以满足这个需求的。今天我就用一个非系统工程师的理解来说明下如何配置NTP同步。原理我就不说了,我也是一知半解的,就是懂点配置。

      说道配置文件,NTP的配置文件就在/etc/ntp.conf

      (如果看客您只想看怎么配置 搜索crontab 在本页面,即进入正文)

[root@xuexi1 ~]# cat /etc/ntp.conf 
# 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 key 42         # broadcast server
#broadcastclient                        # broadcast client
#broadcast 224.0.1.1 key 42             # multicast server
#multicastclient 224.0.1.1              # multicast client
#manycastserver 239.255.254.254         # manycast server
#manycastclient 239.255.254.254 key 42  # 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

# Drift file.  Put this in a directory which the daemon can write to.
# No symbolic links allowed, either, since the daemon updates the file
# by creating a temporary in the same directory and then rename()'ing
# it to the file.
driftfile /var/lib/ntp/drift

# 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

      这个文件常用的没有几个属性,我们把注释的部分都去掉看看

[root@xuexi1 ~]# cat /etc/ntp.conf |grep -v ^#
#权限部分限制
restrict default kod nomodify notrap nopeer noquery    <==拒絕 IPv4 的用戶
restrict -6 default kod nomodify notrap nopeer noquery <==拒絕 IPv6 的用戶

restrict 127.0.0.1  <==底下兩個是預設值,放行本機來源
restrict -6 ::1

#設定服务器地址
server 0.rhel.pool.ntp.org
server 1.rhel.pool.ntp.org
server 2.rhel.pool.ntp.org

server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10

#时间差异设置和KEY 不需要改动一般情况下
driftfile /var/lib/ntp/drift
keys /etc/ntp/keys
    具体参数怎么使用大家可以参考台湾鸟哥的网站:http://linux.vbird.org/linux_server/0440ntp.php

  常用的查看ntp的命令如下:

[root@xuexi1 ~]# /etc/init.d/ntpd start  #启动ntp服务
Starting ntpd: [  OK  ]
[root@xuexi1 ~]# 
[root@xuexi1 ~]# chkconfig ntpd on  #开机启动
[root@xuexi1 ~]# 
[root@xuexi1 ~]# netstat -tlunp | grep ntp  #查看端口占用
udp        0      0 10.10.27.11:123             0.0.0.0:*                               4996/ntpd           
udp        0      0 127.0.0.1:123               0.0.0.0:*                               4996/ntpd           
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               4996/ntpd           
udp        0      0 ::1:123                     :::*                                    4996/ntpd           
udp        0      0 fe80::20c:29ff:fe90:123     :::*                                    4996/ntpd           
udp        0      0 :::123                      :::*                                    4996/ntpd           
[root@xuexi1 ~]# 
[root@xuexi1 ~]# 
[root@xuexi1 ~]# 
[root@xuexi1 ~]# 
[root@xuexi1 ~]# ntpstat #查看同步情况
unsynchronised
  time server re-starting
   polling server every 64 s
[root@xuexi1 ~]# 
[root@xuexi1 ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*LOCAL(0)        .LOCL.          10 l   24   64   37    0.000    0.000   0.001

     

   说了这么多原理,还是给大家一个例子吧,下面是一个服务端的配置,

[root@xuexi1 etc]# cat ntp.conf
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery

restrict 127.0.0.1 
restrict -6 ::1
restrict 10.10.27.13 #可以访问的主机IP,也可以设置整个网段,看最上面的全文件内容 

server 0.rhel.pool.ntp.org
server 1.rhel.pool.ntp.org
server 2.rhel.pool.ntp.org


server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10

driftfile /var/lib/ntp/drift

keys /etc/ntp/keys
Broadcastdelay 0.008


[root@xuexi1 etc]# 

    客户端的配置文件

[root@localhost ~]# cat /etc/ntp.conf |grep -v ^#
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery

restrict 127.0.0.1 
restrict -6 ::1

server 10.10.27.11 prefer


driftfile /var/lib/ntp/drift
keys /etc/ntp/keys

Broadcastdelay 0.008 #从字面意思来看 是 广播延时时间。不明白干吗用的,原文件没有说明有这个参数
[root@localhost ~]# 
   

      这样配置以后,大家就可以启动服务了,在两个节点服务器(由于ntpd的server/client之间的时间误差不允许超过1000秒,因此你得先手动进行时间同步,然后再设定与启动服务。

[root@xuexi1 ~]# /etc/init.d/ntpd start  #启动ntp服务
Starting ntpd: [  OK  ]
[root@xuexi1 ~]# 
[root@xuexi1 ~]# chkconfig ntpd on  #开机启动
[root@xuexi1 ~]# 
[root@xuexi1 ~]# netstat -tlunp | grep ntp  #查看端口占用
          在我修改服务端的时间后,我发现客户端是不能马上同步的,在我看了很多的帖子以后,我才发现,聪明的大师就配置了一个ntp服务端,其他节点同步时间的时候是用ntpdate的,这个命令可以马上同步时间。可以把这个命令加在客户端的crontab 

     下面给大家也演示下crontab的方式,这个很简单,我推荐这个方法

     服务器端:直接添加下面的最后一行 对了10.10.27.11是服务端IP

[root@xuexi1 ~]# cat /etc/ntp.conf |grep -v ^# 
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery

restrict 127.0.0.1 
restrict -6 ::1


server 0.rhel.pool.ntp.org
server 1.rhel.pool.ntp.org
server 2.rhel.pool.ntp.org


server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10

driftfile /var/lib/ntp/drift

keys /etc/ntp/keys



restrict 10.10.27.0 mask 255.255.255.0 nomodify   #直接编辑文件 添加这行 没事闲着不要加 notrap
[root@xuexi1 ~]# 

      服务端配置好了以后,记得启动

[root@xuexi1 ~]# /etc/init.d/ntpd start  #启动ntp服务
Starting ntpd: [  OK  ]
[root@xuexi1 ~]# 

    客户端就有点麻烦了

[root@localhost ~]# ll ntpdate_to_11.sh   #新建一个sh文件
-rwxrwxrwx 1 root root 47 Jun 12 11:47 ntpdate_to_11.sh
[root@localhost ~]# 
[root@localhost ~]# cat ntpdate_to_11.sh  #文件内容如下
while :; do ntpdate 10.10.27.11; sleep 1; done
[root@localhost ~]# 
[root@localhost ~]# pwd
/root
[root@localhost ~]# 
[root@localhost ~]# crontab -e  
在VI界面输入“* * * * * /usr/sbin/ntpdate 10.10.27.11 >> /root/ntpdate_to_rac1.log 2>&1” #记得保存啊
[root@localhost ~]# crontab -l  #查看下是否添加成功
* * * * * /usr/sbin/ntpdate 10.10.27.11 >> /root/ntpdate_to_rac1.log 2>&1
      所有同步的过程记录在日志ntpdate_to_rac1.log里面了。

      一开始的时候会报

ntpdate_to_11.sh     ntpdate_to_rac1.log  
[root@localhost ~]# cat ntpdate_to_rac1.log 
12 Jun 11:51:05 ntpdate[4520]: no server suitable for synchronization found
12 Jun 11:52:05 ntpdate[4532]: no server suitable for synchronization found
12 Jun 11:53:01 ntpdate[4547]: no server suitable for synchronization found
12 Jun 11:54:01 ntpdate[4559]: no server suitable for synchronization found
12 Jun 11:55:02 ntpdate[4570]: no server suitable for synchronization found
12 Jun 11:56:01 ntpdate[4583]: no server suitable for synchronization found
12 Jun 11:57:01 ntpdate[4595]: no server suitable for synchronization found
12 Jun 11:58:01 ntpdate[4607]: no server suitable for synchronization found
12 Jun 11:59:01 ntpdate[4620]: no server suitable for synchronization found
12 Jun 12:00:01 ntpdate[4631]: no server suitable for synchronization found
12 Jun 12:01:01 ntpdate[4642]: no server suitable for synchronization found
12 Jun 12:02:01 ntpdate[4654]: adjust time server 10.10.27.11 offset 0.077524 sec
12 Jun 12:03:01 ntpdate[4662]: adjust time server 10.10.27.11 offset 0.017492 sec
12 Jun 12:04:01 ntpdate[4670]: adjust time server 10.10.27.11 offset -0.008772 sec
12 Jun 12:05:01 ntpdate[4679]: adjust time server 10.10.27.11 offset 0.004367 sec
12 Jun 12:06:01 ntpdate[4688]: adjust time server 10.10.27.11 offset -0.002139 sec
          上面错误是服务器端的ntp还没有启动呢,这个过程,大概在15分以内。大家注意下就好了。上面的同步周期是1分钟,大家可以根据自己的喜好来修改。



  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值