CentOS伺服器時間同步的2種方法

來源:互聯網
上載者:User

由於硬體的原因,機器或多或少的根標準時間對不上,一個月的誤差幾秒到幾分鐘不等。對於伺服器來說時間不準,會有很多麻煩。例如,支付的時候,無法下單,遊戲無法登入等。自己用的PC就無所謂了,手動改一下就行了。


一,用ntpdate從時間伺服器更新時間

如果你的linux系統根本沒有ntpdate這個命令

yum install ntp


安裝完了之後,你不要做什麼配置,也不需要,直接測試一下

[root@localhost ~]# ntpdate time.nist.gov 22 Oct 21:11:43 ntpdate[5014]: adjust time server 207.200.81.113 offset -0.018788 sec


如果出去上面的內容說明,同步成功了。然後在crontab裡面加上以下內容。

  1. */10 * * * * ntpdate time.nist.gov #網域名稱或IP

每隔十分鐘同步一次。推薦幾個時間伺服器。

time.nist.gov
time.nuri.net
asia.pool.ntp.org
asia.pool.ntp.org
asia.pool.ntp.org
asia.pool.ntp.org

二,用ntp搭建自己的時間伺服器

上面我們是利用別人的時間伺服器來同步時間,這些時間伺服器都是比較權威的。當我們自己搭建時間伺服器就不用crontab來定時去跑。

1,安裝時間伺服器ntp

  1. yum install ntp

2,配置ntp

查看複製列印?
[root@localhost ~]# cat /etc/ntp.conf |awk '{if($0 !~ /^$/ && $0 !~ /^#/) {print $0}}'restrict default ignore   //預設不允許修改或者查詢ntp,並且不接收特殊封包restrict 127.0.0.1        //給於本機所有許可權restrict 192.168.1.0 mask 255.255.255.0 notrap nomodify  //給於區域網路機的機器有同步時間的許可權server time.nist.gov prefer      //設定時間伺服器,加prefer表示優先server 0.asia.pool.ntp.org server 1.asia.pool.ntp.org server 2.asia.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


3,啟動 ntp

  1. [root@localhost ~]# /etc/init.d/ntpd start

4,查看並測試

[root@localhost ~]# netstat -upnl |grep ntpd   //查看時程[root@localhost ~]# ntpq -pn    //查看同步的伺服器IP remote           refid      st t when poll reach   delay   offset  jitter ==============================================================================  50.77.217.185   .INIT.          16 u    -   64    0    0.000    0.000   0.000  202.90.158.4    .INIT.          16 u    -   64    0    0.000    0.000   0.000  202.71.100.89   .INIT.          16 u    -   64    0    0.000    0.000   0.000  202.134.1.10    .INIT.          16 u    -   64    0    0.000    0.000   0.000 *127.127.1.0     .LOCL.          10 l   18   64  377    0.000    0.000   0.001 [root@localhost ~]# ntpstat   //同步的結果synchronised to local net at stratum 11  time correct to within 12 ms  polling server every 512 s

remote:即NTP主機的IP或主機名稱。注意最左邊的符號,如果由“+”則代表目前正在作用鐘的上層NTP,如果是“*”則表示也有連上線,不過是作為次要聯機的NTP主機。
refid:參考的上一層NTP主機的地址
st:即stratum階層
when:幾秒前曾做過時間同步更新的操作
poll:下次更新在幾秒之後
reach:已經向上層NTP伺服器要求更新的次數
delay:網路傳輸過程鐘延遲的時間
offset:時間補償的結果
jitter:Linux系統時間與BIOS硬體時間的差異時間

推薦第一種方法,雖然搭建個時間伺服器,還是比較簡單的,但是我覺得沒有這個必要。如果用別人的時間伺服器,每隔10分鐘同步一次,可以精確到毫秒。

作者:海底蒼鷹

地址:http://blog.51yip.com/server/1474.html


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.