CentOS搭建NTP伺服器

來源:互聯網
上載者:User

標籤:centos   ntp   時間同步   

 

一、說明

NTP:時間同步伺服器

ntpserver     ip:192.168.0.128    

client           ip:192.168.0.129    

 

二、準備工作

 1、查看時間

 [[email protected] ~]# date                    //查看系統時間

2015年 11月 13日 星期五 15:58:07 CST

[[email protected] ~]# hwclock               //查看硬體時間

2015年11月13日 星期五 15時59分10秒  -0.016335 seconds

注意:若發現時間和時區不對(正確的時區應為 CST),進行以下2-3步。

2、修改時區

[[email protected] ~]# vim /etc/sysconfig/clock

ZONE="Asia/Shanghai"
UTC=true
ARC=false

3、覆蓋系統時間檔案

[[email protected] ~]# cp -a /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

[[email protected] ~]# date                   //再次查看時間是否正確

 

三、搭建NTP服務

1、安裝

[[email protected] ~]# yum install -y ntp

2、檢測上層服務是否連通

國家對時伺服器有6個,可以使用下面3個

1.cn.pool.ntp.org

2.cn.pool.ntp.org

ch.pool.ntp.org

[[email protected] ~]# ntpdate -q 1.cn.pool.ntp.org       //依次測試上面3個伺服器,正確結果如下

server 202.118.1.130, stratum 2, offset 0.003763, delay 0.06880
server 202.118.1.81, stratum 2, offset 0.004612, delay 0.06927
server 202.112.29.82, stratum 2, offset 0.041528, delay 0.14401
server 202.112.31.197, stratum 0, offset 0.000000, delay 0.00000
13 Nov 16:15:02 ntpdate[3300]: adjust time server 202.118.1.130 offset 0.003763 sec

3、校對目前時間

[[email protected] ~]# ntpdate 1.cn.pool.ntp.org

13 Nov 16:18:35 ntpdate[3332]: the NTP socket is in use, exiting

注意:如果ntpd服務已經開啟了,需關閉ntpd服務才可以使用 

4、修改設定檔

[[email protected] ~]# vim /etc/ntp.conf   //添加標紅部分

restrict default kod nomodify notrap nopeer noquery             //服務預設拒絕所有NTP連線

restrict -6 default kod nomodify notrap nopeer noquery         //拒絕IPV6所有NTP連線

 

restrict 127.0.0.1
restrict -6 ::1                                                                            //以上兩條表示允許本機訪問

restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap    

                      //允許192.168.0.0/24 網段的主機來進行對時,不允許用戶端來修改,登入ntp伺服器 

 

#server 0.centos.pool.ntp.org
#server 1.centos.pool.ntp.org
#server 2.centos.pool.ntp.org
#server 3.centos.pool.ntp.org

server 1.cn.pool.ntp.org
server 2.cn.pool.ntp.org prefer              //prefer優先使用


logfile /var/log/ntp.log

5、同步更新hwclock

[[email protected] ~]# vim /etc/sysconfig/ntpd

SYNC_HWCLOCK=yes

6、啟動ntp服務

[[email protected] ~]# service ntpd start

7、查看與上層伺服器串連狀態

 [[email protected] ~]# ntpq -p

    remote           refid      st t when poll reach   delay   offset  jitter
============================================================

+news.neu.edu.cn 202.118.1.46     2 u   14  128  377   44.480    2.868  31.310
*202.118.1.130   202.118.1.46     2 u   63  128  377   41.726    1.631  21.462
 LOCAL(0)        .LOCL.          10 l    9   64  377    0.000    0.000   0.001

 說明:*表示目前正在使用的上層NTP,+表示已連線,可提供時間更新的候補伺服器

 8、確認已進行同步

[[email protected] ~]# ntpstat                     //顯示下面時間需要好幾分鐘

synchronised to NTP server (202.118.1.130) at stratum 3
   time correct to within 60 ms
   polling server every 128 s

9、加入開機啟動

[[email protected] ~]# chkconfig ntpd on

 

四、用戶端設定

1、同步時間

[[email protected] ~]# ntpdate 192.168.0.128
13 Nov 16:41:52 ntpdate[3293]: adjust time server 192.168.0.128 offset 0.003880 sec

2、加入任務計劃

[[email protected] ~]# crontab -e      //每天淩晨一點同步

0 1 * * * /usr/sbin/ntpdate 192.168.0.128

 

 

本文出自 “M四月天” 部落格,請務必保留此出處http://msiyuetian.blog.51cto.com/8637744/1712561

CentOS搭建NTP伺服器

相關文章

聯繫我們

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