linux下rdate,ntpdate,ntp自動同步日期時間方法

來源:互聯網
上載者:User

通過網路ntp自動同步

如果只需要同步一次,那麼執行如下命令即可

rdate -s time.nist.gov

其中time.nist.gov 是一個時間伺服器,當然你也可以使用另外的時間伺服器。

如果是多台伺服器的時間需要保持完全同步,那麼最好還是給這些伺服器配置統一的自動同步任務,以免因為各種原因,造成它們的時間有所偏差。方法如下

使用 crontab 讓linux自動同步時間

vi /etc/crontab

加上一句:

00 0 1 * * root rdate -s time.nist.gov


time.nist.gov 是一個時間伺服器.

rdate同步配置

centos迷你安裝預設不安裝,先確認已經安裝過,否則先安裝。

其中rdate本身是用來擷取遠程時間伺服器上時間用的,帶上 -s 參數,就可以將擷取到的時間應用到本地系統。

NAME
       rdate - get the time via the network

SYNOPSIS
       rdate [-p] [-s] [-u] [-l] [-t sec] [host...]

DESCRIPTION
       rdate  connects  to  an  RFC www.111cn.net  868 time server over a TCP/IP network, printing the returned time
       and/or setting the system clock.

   OPTIONS
       -p     Print the time returned by the remote machine.

       -s     Set the system time to the returned time.

       -u     Use UDP instead of TCP as the transport.

       -l     Use syslog to output errors (cron.warning) and output (cron.info).

       -t     Set timeout in seconds for every attempt to retreive date.

rdate -s time.nist.gov

ntpdate 本身就是用來同步時間的工具。

樣本 :ntpdate ntp.sjtu.edu.cn

NAME
       ntpdate - set the date and time via NTP

       Disclaimer:  The functionality of this program is now available in the ntpd program. See the -q com-
       mand line option in the ntpd - Network Time Protocol (NTP) daemon page. After a suitable  period  of
       mourning, the ntpdate program is to be retired from this distribution

SYNOPSIS
       ntpdate [ -46bBdqsuv ] [ -a key ] [ -e authdelay ] [ -k keyfile ] [ -o version ] [ -p samples ] [ -t
       timeout ] [ -U user_name ] server [ ... ]

DESCRIPTION

[略,具體參看linux man手冊]

Linux叢集時間同步

打算將11台Linux叢集時間同步一下,作業系統都為Ubuntu 11.04 x86 64位
選定其中一台133.133.10.2作為時間伺服器(ntp server),其他節點與該節點進行時間同步。
 
1、在133.133.10.2(monitor)上安裝ntp server。
 root@monitor:~# apt-get install ntp
 
2、修改/etc/ntp.conf
 
 
# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help
 
driftfile /var/lib/ntp/ntp.drift
 
 
# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/
 
statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable
 
# Specify one or more NTP servers.
 
# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information.
#server 0.ubuntu.pool.ntp.org
#server 1.ubuntu.pool.ntp.org
#server 2.ubuntu.pool.ntp.org
#server 3.ubuntu.pool.ntp.org
 
# Use Ubuntu's ntp server as a fallback.
#server ntp.ubuntu.com
server 127.127.1.0 minpoll 4 maxpoll 5
fudge 127.127.1.0 stratum 2
 
# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
# details.  The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
# might also be helpful.
#
# Note that "restrict" applies to both servers and clients, so a configuration
# that might be intended to block requests from certain clients could also end
# up blocking replies from your own upstream servers.
 
# By default, exchange time with everybody, but don't allow configuration.
#restrict -4 default kod notrap nomodify nopeer noquery
#restrict -6 default kod notrap nomodify nopeer noquery
restrict -4 default kod nomodify noper
restrict -6 default kod nomodify noper
 
# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1
 
# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
#restrict 192.168.123.0 mask 255.255.255.0 notrust
 
 
# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
#broadcast 192.168.123.255
 
# If you want to listen to time broadcasts on your local subnet, de-comment the
# next lines.  Please do this only if you trust everybody on the network!
#disable auth
#broadcastclient

3、運行ntpdate命令

首先重啟一下ntp服務
root@monitor:/etc#/etc/init.d/ntp restart
 
再運行
root@monitor:/etc# ntpdate -q 127.0.0.1
得到
server 127.0.0.1, stratum 3, offset -0.000003, delay 0.02565
23 Sep 17:12:00 ntpdate[11502]: adjust time server 127.0.0.1 offset -0.000003 sec

4、將其他節點133.133.10.*的時間與133.133.10.2同步

在其他每一個節點運行命令

root@slave1:~# ntpdate 133.133.10.2

這時候發現節點間的時間同步了,但ntpdate只在開機運行,我們若要設定為1小時同步一次,那麼進行第5步

5、將其他節點133.133.10.*的時間定期與133.133.10.2同步

安裝完以後使用crontab來自動更新時間:
#crontab -e
添加如下行:
* */1 * * * /usr/sbin/ntpdate 133.133.10.2

這行的意思是每個小時與與133.133.10.2時間同步一次。

6、通過date命令發現所有節點時間都一致了

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.