page counter next up previous contents
Next: 帳號與身份管理 Up: 基本的系統設定 Previous: 網路偵測與觀察   Contents

系統時間設定

  1. Network Time Protocol, NTP 伺服器設定檔 ntp.conf
    [root@demo ~]# vim /etc/ntp.conf 
    # driftfile driftfile
    # ntpd 自動計算自己主機的頻率與上層 Time server 的頻率誤差,並將其記錄在 driftfile 後面接的檔名中。
        * driftfile 後面接的檔案需要使用完整路徑檔名;
        * 該檔案不能是連結檔;
        * 該檔案需要設定成 ntpd 這個 daemon 可以寫入的權限。
        * 該檔案所記錄的數值單位為:百萬分之一秒 (ppm)。 
    driftfile /var/lib/ntp/drift
    
    ## restrict 格式
    # restrict default [flag][...]
    # restrict source [flag][...]
    # restrict address [mask mask] [flag][...]
    restrict default kod nomodify notrap nopeer noquery
    restrict -6 default kod nomodify notrap nopeer noquery
    restrict 127.0.0.1 
    restrict -6 ::1
    
    # default: address 0.0.0.0, mask 0.0.0.0
    # -6	 : DNS resolution to the IPv6 namespace
    ## flag 說明:
    # kod:      當要求server的服務超過限制,server不是直接丟棄服務要求,而是傳送 
                kiss-o'-death (KoD) packet,讓client轉向要求其他server服務,
                或等一段時間再要求server服務。  
    # nomodify: 用戶端不能更改 NTP 伺服器的時間參數。
    # notrap  : 不提供 trap 這個遠端事件登錄 (remote event logging) 的功能。
    # nopeer  : 避免其他人使用此ntpd作為time server    
    # noquery : 用戶端不能夠使用 ntpq, ntpc 等指令來查詢時間伺服器;
    
    ## server 格式
    # server address [options ...]
    server tock.stdtime.gov.tw prefer
    server 0.rhel.pool.ntp.org
    server 1.rhel.pool.ntp.org
    server 2.rhel.pool.ntp.org
    
    # perfer 表示『優先使用』的主機
    
    ## 如果外部time server無法連線或層級(stratum)10以上,則以本機時鐘為系統時間。
    #server	127.127.1.0	# local clock
    #fudge	127.127.1.0 stratum 10
    
  2. 編輯 /etc/sysconfig/ntpd
    [root@demo ~]# vim /etc/sysconfig/ntpd
    # Drop root to id 'ntp:ntp' by default.
    OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"
    SYNC_HWCLOCK=yes
    # 改成 yes,BIOS 的時間也會跟著改變。
    
  3. 啟動 NTP 服務
    [root@demo ~]# /etc/init.d/ntpd start
    Starting ntpd:                                             [  OK  ]
    
  4. 觀察 NTP 服務的 port 123
    [root@demo ~]# netstat -tlunp | grep 123
    udp        0      0 192.168.0.250:123           0.0.0.0:*     3224/ntpd           
    udp        0      0 127.0.0.1:123               0.0.0.0:*     3224/ntpd           
    udp        0      0 0.0.0.0:123                 0.0.0.0:*     3224/ntpd           
    udp        0      0 ::1:123                     :::*          3224/ntpd           
    udp        0      0 fe80::5054:ff:fe00:fa:123   :::*          3224/ntpd           
    udp        0      0 :::123                      :::*          3224/ntpd
    
  5. 設定開機啟動 ntpd
    [root@demo ~]# chkconfig ntpd on
    
  6. 查詢開機是否啟動 ntpd
    [root@demo ~]# chkconfig --list ntpd
    ntpd            0:off   1:off   2:on    3:on    4:on    5:on    6:off
    
  7. 確認 NTP 伺服器是否順利更新時間?
    [root@demo ~]# ntpstat -p
    unsynchronised
      time server re-starting
       polling server every 64 s
    ## 幾分鐘後再觀察
    [root@demo ~]# ntpstat -pn
    synchronised to NTP server (59.124.196.83) at stratum 3 
       time correct to within 782 ms
       polling server every 64 s
    
  8. 手動調校系統時間
    [root@demo ~]# ntpdate tock.stdtime.gov.tw
    18 Jul 10:40:24 ntpdate[3334]: the NTP socket is in use, exiting
    [root@demo ~]# /etc/init.d/ntpd stop
    Shutting down ntpd:                                        [  OK  ]
    [root@demo ~]# ntpdate tock.stdtime.gov.tw
    18 Jul 10:40:37 ntpdate[3341]: step time server 220.130.158.71 offset 1.909398 sec
    
  9. 調整 BIOS 時間
    [root@deyu ~]# hwclock [-rw]
    選項與參數:
    -r :亦即 read ,讀出目前 BIOS 內的時間參數;
    -w :亦即 write ,將目前的 Linux 系統時間寫入 BIOS 中。
    
    [root@demo ~]# hwclock -w; hwclock -r; date
    Mon 18 Jul 2011 10:42:26 AM CST  -0.323154 seconds
    Mon Jul 18 10:42:25 CST 2011
    
  10. 使用圖形界面做網路校時
    1. 進入 X window,依照『開始』→『系統』→『管理』→『日期與時間』打開設定軟體;
    2. 選擇網路時間協定 (Network Time Protocol);
    3. 按下『Enable Network Time Protocol』;
    4. 輸入 tock.stdtime.gov.tw;
    5. 按下 OK﹗


2015-04-13