Ubuntu 配置NTP Server

来源:互联网 发布:用idle打开python文件 编辑:程序博客网 时间:2024/06/08 16:13

Ubuntu安装NTP Server很简单,分位3步走:

第一步:安装NTP

复制代码
root@cephadmin:~/ceph-cluster# apt-get install ntpReading package lists... DoneBuilding dependency tree       Reading state information... DoneThe following extra packages will be installed:  libopts25Suggested packages:  ntp-docThe following NEW packages will be installed:  libopts25 ntp0 upgraded, 2 newly installed, 0 to remove and 217 not upgraded.Need to get 477 kB of archives.After this operation, 1,682 kB of additional disk space will be used.Do you want to continue? [Y/n] yGet:1 http://cn.archive.ubuntu.com/ubuntu/ trusty/main libopts25 amd64 1:5.18-2ubuntu2 [55.3 kB]Get:2 http://cn.archive.ubuntu.com/ubuntu/ trusty-updates/main ntp amd64 1:4.2.6.p5+dfsg-3ubuntu2.14.04.10 [421 kB]Fetched 477 kB in 0s (757 kB/s)Selecting previously unselected package libopts25:amd64.(Reading database ... 55341 files and directories currently installed.)Preparing to unpack .../libopts25_1%3a5.18-2ubuntu2_amd64.deb ...Unpacking libopts25:amd64 (1:5.18-2ubuntu2) ...Selecting previously unselected package ntp.Preparing to unpack .../ntp_1%3a4.2.6.p5+dfsg-3ubuntu2.14.04.10_amd64.deb ...Unpacking ntp (1:4.2.6.p5+dfsg-3ubuntu2.14.04.10) ...Processing triggers for ureadahead (0.100.0-16) ...ureadahead will be reprofiled on next rebootProcessing triggers for man-db (2.6.7.1-1) ...Setting up libopts25:amd64 (1:5.18-2ubuntu2) ...Setting up ntp (1:4.2.6.p5+dfsg-3ubuntu2.14.04.10) ... * Starting NTP server ntpd                                                                                                                                                 [ OK ] Processing triggers for libc-bin (2.19-0ubuntu6) ...Processing triggers for ureadahead (0.100.0-16) ...root@cephadmin:~/ceph-cluster# service ntp status * NTP server is running
复制代码

第二步:修改ntp.conf配置文件

1) 添加公网NTP Server 

 server 120.24.166.46   (阿里云NTP Server)

2) 允许的NTP Client网段

restrict 10.138.0.0 mask 255.255.0.0 nomodify
复制代码
# Enable this if you want statistics to be logged.#statsdir /var/log/ntpstats/    #也可以开启NTP logstatistics loopstats peerstats clockstatsfilegen loopstats file loopstats type day enablefilegen peerstats file peerstats type day enablefilegen 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 120.24.166.46   # 阿里云NTP Serverserver 127.127.1.0     # 如果公网NTP不可用时,将使用Local时间作为NTP服务提供给NTP Client。# Use Ubuntu's ntp server as a fallback.server ntp.ubuntu.com# 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 noqueryrestrict -6 default kod notrap nomodify nopeer noqueryrestrict 192.168.20.0 mask 255.255.255.0 nomodify   # 允许的NTP Client网段# Local users may interrogate the ntp server more closely.restrict 127.0.0.1restrict ::1
复制代码

第三步:重启NTP服务,并在客户端验证

root@ntpserver:~/ceph-cluster# service ntp restart * Stopping NTP server ntpd                                           [ OK ]  * Starting NTP server ntpd                                           [ OK ] 
第四步:测试root@docker1:/# ntpq -c assocind assid status  conf reach auth condition  last_event cnt===========================================================  1 14912  8011   yes    no  none    reject    mobilize  1  2 14913  963a   yes   yes  none  sys.peer    sys_peer  3
root@docker1:/# ntpq -c peers     remote           refid      st t when poll reach   delay   offset  jitter============================================================================== ntp-sz.chl.la   .INIT.          16 u    -   64    0    0.000    0.000   0.000*LOCAL(0)        .LOCL.           5 l    1   64   37    0.000    0.000   0.000root@docker1:/# 
第五步  如果时间还是不能同步,那么看下当前各个节点的时区是不是一致,查看和修改方法如下

1. 查看当前时区命令 : "date -R"

2. 修改设置Linux服务器时区方法 A命令 : "tzselect"

方法 B 仅限于RedHat Linux 和 CentOS命令 : "timeconfig"

方法 C 适用于Debian命令 : "dpkg-reconfigure tzdata"

3. 复制相应的时区文件,替换系统时区文件;或者创建链接文件cp /usr/share/zoneinfo/$主时区/$次时区 /etc/localtime

例如:在设置中国时区使用亚洲/上海(+8)

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

0 0