1参考文献:

1.鸟哥的Linux私房菜:第十五章、时间服务器: NTP 服务器(强烈建议看完)

2.http://www.crsay.com/wiki/wiki.php/server/centos/ntp-set

3.常见错误集锦:http://www.blogjava.net/spray/archive/2008/07/10/213964.html

2架构

Stratum 1/2 Internet NTP Server

Stratum 3 公司防火墙(防火墙带有ntp功能,并且能够连接外网)

Stratum 4 自己搭建ntp server(跟公司防火墙的ntp server对时,这样的好处有两个,一是降低了防火墙的压力,二是降低耦合性,防止因为防火墙的变更而带来更多的改动)

Stratum 5 公司内的各台机器,相当于是一个ntp client。

3安装

3.1在线安装

ntp在线安装的方式很简单,只需要执行以下命令即可帮你安装好ntp以及所有的依赖包

sudo apt-get install ntp

3.2离线安装

如果要离线安装,那么就需要下载ntp安装包和依赖包。我们可以在一个有线环境下运行上面的在线安装,然后到/var/cache/apt/archives这个目录下拷贝完整的ntp安装包和依赖包。当前我已经将这些安装包放到网盘上面去了:http://pan.baidu.com/s/1dDzg95f

  对于下载的deb包,我们使用dpkg安装包来安装,先安装依赖包,最后安装程序包。具体安装过程如下:

root@BJCGNMON01:/var/cache/apt/archives# dpkg  -i  libopts25_1%3a5.-.1ubuntu1_amd64.deb
Selecting previously unselected package libopts25.
(Reading database ... files and directories currently installed.)
Unpacking libopts25 (from libopts25_1%3a5.-.1ubuntu1_amd64.deb) ...
Setting up libopts25 (:5.12-.1ubuntu1) ...
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place root@BJCGNMON01:/var/cache/apt/archives# dpkg -i ntp_1%3a4.2.6.p3+dfsg-1ubuntu3.1_amd64.deb
(Reading database ... files and directories currently installed.)
Preparing to replace ntp :4.2..p3+dfsg-1ubuntu3. (using ntp_1%3a4.2.6.p3+dfsg-1ubuntu3.1_amd64.deb) ...
Unpacking replacement ntp ...
Setting up ntp (:4.2..p3+dfsg-1ubuntu3.) ...
* Starting NTP server ntpd
...done.
Processing triggers for ureadahead ...
ureadahead will be reprofiled on next reboot
Processing triggers for man-db ...

  当然还有更加简单的方法,将下载的deb包拷贝到/var/cache/apt/archives目录下,然后在执行一下命令同样可以安装。

sudo apt-get install ntp

安装完毕以后我们可以查看服务是否启动,执行以下命令:

enadmin@ubuntu-server:~/ftp/ntp_ubuntu$ service --status-all
[ ? ] acpid
[ ? ] apache
[ + ] apache2
[ - ] apparmor
[ ? ] apport
[ ? ] atd
[ - ] bootlogd
[ ? ] console-setup
[ ? ] cron
[ ? ] dbus
[ ? ] dmesg
[ ? ] dns-clean
[ ? ] friendly-recovery
[ - ] grub-common
[ ? ] hostname
[ ? ] hwclock
[ ? ] hwclock-save
[ ? ] irqbalance
[ ? ] killprocs
[ ? ] module-init-tools
[ ? ] mysql
[ + ] nagios
[ + ] nagios-bak
[ ? ] network-interface
[ ? ] network-interface-container
[ ? ] network-interface-security
[ ? ] networking
[ + ] ntp
[ ? ] ondemand

可以看到ntp服务已经启动([+]表示已经启动。)

4配置文件/etc/ntp.conf解析

sudo vim /etc/ntp.conf

这个文件是配置ntp最重要的一个文件,也是ntp配置过程中的唯一难点,之前一直不知道这个配置问价当中配置选项的含义,所以一直无从下手。

4.1配置上层server

利用 server 关键字设定上层 NTP 服务器,上层 NTP 服务器的设定方式为:

server [IP or hostname] [prefer]

在 server 后端可以接 IP 或主机名,个人比较喜欢使用 IP 来设定, perfer 表示『优先使用』的服务器。

4.2利用 restrict 来管理权限控制

在 ntp.conf 档案内可以利用『 restrict 』来控管权限,这个参数的设定方式为:

restrict [IP] mask [netmask_IP] [parameter]

其中 parameter 的参数主要有底下这些:

  • ignore: 拒绝所有类型的 NTP 联机;
  • nomodify: 客户端不能使用 ntpc 与 ntpq 这两支程序来修改服务器的时间参数, 但客户端仍可透过这部主机来进行网络校时的;
  • noquery: 客户端不能够使用 ntpq, ntpc 等指令来查询时间服务器,等于不提供 NTP 的网络校时;
  • notrap: 不提供 trap 这个远程事件登录 (remote event logging) 的功能。
  • notrust: 拒绝没有认证的客户端。

如果你没有在 parameter 的地方加上任何参数的话,这表示『该 IP 或网段不受任何限制』。一般来说,我们可以先关闭 NTP 的权限,然后再一个一个的启用允许登入的网段。

ntp server配置

ntp server的需求:

  1. 我的上层 NTP 服务器共只有防火墙的ntp server:10.138.16.251。
  2. 不对Internet 提供服务,仅允许来自内部网域10.138.0.0/255.255.0.0的查询而已;
  3. 侦测一些 BIOS 时钟与 Linux 系统时间的差异并写入 /var/lib/ntp/drift 档案当中。
# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift #草稿文件

#日志文件
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 #上层ntp server
server 10.138.16.251 prefer
#让NTP Server和其自身保持同步,如果在/etc/ntp.conf中定义的server都不可用时,将使用local时间作为ntp服务提供给ntp客户端
server 127.127.1.0
fudge 127.127.1.0 stratum 5 #不允许来自公网上ipv4和ipv6客户端的访问
restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery #运行上层ntp server的所有权限
restrict 10.138.16.251 #允许这个网段的对时请求
restrict 10.138.0.0 mask 255.255.0.0 nomodify # Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1

使用 ntpd渐进式更新时间

ntp又是服务器,又是客户端,只需要通过配置即可。上面我们将10.138.16.232配置成了ntp服务器,下面这台服务器我们以10.138.16.232作为ntp服务器配置client。

# /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 10.138.16.251 prefer
server 127.127.1.0
fudge 127.127.1.0 stratum 10 # 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 10.138.16.251
restrict 10.138.0.0 mask 255.255.0.0 nomodify # 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

使用ntpdate更新时间

在客户端只需要运行一下命令接口进行对时操作

sudo ntpdate 10.138.16.232

ntpdate命令是要在ntp进程退出的情况下执行,如果执行上述命令出错,那么请先关闭ntp进程,具体操作日下所示:

root@BJCGNMON01:/etc/init.d# ntpdate 10.138.16.232
Feb :: ntpdate[]: the NTP socket is in use, exiting
root@BJCGNMON01:/etc/init.d# service ntp stop
* Stopping NTP server ntpd
...done.
root@BJCGNMON01:/etc/init.d# ntpdate 10.138.16.232
Feb :: ntpdate[]: step time server 10.138.16.232 offset -98.402560 sec

如果是windows操作系统,按照参考文献1进行修改。

ubuntu下配置时间同步NTP的更多相关文章

  1. Ubuntu下配置python完成爬虫任务(笔记一)

    Ubuntu下配置python完成爬虫任务(笔记一) 目标: 作为一个.NET汪,是时候去学习一下Linux下的操作了.为此选择了python来边学习Linux,边学python,熟能生巧嘛. 前期目 ...

  2. 转[开发环境配置]在Ubuntu下配置舒服的Python开发环境

    在Ubuntu下配置舒服的Python开发环境 Ubuntu 提供了一个良好的 Python 开发环境,但如果想使我们的开发效率最大化,还需要进行很多定制化的安装和配置.下面的是我们团队开发人员推荐的 ...

  3. Ubuntu下配置C/C++开发环境

    在 Ubuntu 下配置 C/C++ 开发环境 转自:白巴的临时空间 Submitted by 白巴 on 2009-04-27 19:52:12. 学习笔记 虽然 Ubuntu 的版本已经是9.04 ...

  4. [转]Ubuntu下配置NFS服务

    [转]Ubuntu下配置NFS服务  http://blog.163.com/liu8821031%40126/blog/static/111782570200921021253516/ Table ...

  5. Ubuntu下配置tftp服务

    Ubuntu下配置tftp服务 1.安装TFTP软件 sudo apt-get install tftp-hpa tftpd-hpa tftp-hpa是客户端,tftpd-hpa是服务器端 2.建立t ...

  6. ubuntu下配置Apache

    ubuntu下配置Apache Apache的默认文档根目录是在Ubuntu上的/var/www目录 配置文件是/ etc/apache2/apache2.conf配置存储在的子目录在/etc/apa ...

  7. ubuntu 下配置Web服务器

    ubuntu 下配置Web服务器 1.切换管理员身份 终端/文本界面输入命令: su 根据提示输入密码 注: 如果不能使用su 点击查看如何启用su2.安装MySQL5 apt-get install ...

  8. CentOS 与Ubuntu 下配置IP地址

    1.CentOS配置方法如下: 用vi打开配置文件 [root@haha3 ~]#  vi /etc/sysconfig/network-scripts/ifcfg-eth0 写入以下配置 DEVIC ...

  9. Ubuntu下配置Nginx HTTPS

    HTTPS(全称:Hypertext Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版.即HTTP下加入S ...

随机推荐

  1. JavaScript -- Enumerator

    -----022-Enumerator.html----- <!DOCTYPE html> <html> <head> <meta http-equiv=&q ...

  2. node mysql插入中文时报错

    一开始以为是前端传参.数据类型的问题,于是就直接把sql语句中的参数直接改成字符串值,但发现还是报500错误. 所以,这就排除了前端的问题. 剩下的就是数据库了,发现我的表设置有问题.凡是有中文数据的 ...

  3. 使用Nagios打造专业的业务状态监控

    想必各个公司都有部署zabbix之类的监控系统来监控服务器的资源使用情况.各服务的运行状态,是否这种监控就足够了呢?有没有遇到监控系统一切正常确发现项目无法正常对外提供服务的情况呢?本篇文章聊聊我们如 ...

  4. EventProcessor与WorkPool用法--可处理多消费者

    单一的生产者,消费者有多个,使用WorkerPool来管理多个消费者: RingBuffer在生产Sequencer中记录一个cursor,追踪生产者生产到的最新位置,通过WorkSequence和s ...

  5. [Python学习笔记-002] lambda, map, filter and reduce

    1. lambda lambda, 即匿名函数,可以理解为跟C语言的宏类似.例如: >>> max = lambda x, y: x if x > y else y >& ...

  6. HTML Dog 初级教程中关于 forms 的翻译

    Formsforms用于搜集用户输入网页上的信息.它们可以当作网络应用的接口,或者用于发送网络数据. 仅仅使用forms,它们并不那么有用.它们通常结合某种编程语言来处理用户输入的数据.HTML中使用 ...

  7. CentOS7 apache

    1.准备环境 centos7最小化安装 yum安装wget.vim.gcc.gcc-c++.cmake 2.安装apache2.4.10 官网:http://httpd.apache.org/ 下载源 ...

  8. Go常量与运算符

    常量的定义 常量的值在编译时就已经确定 常量的定义格式与变量基本相同 等号右侧必须是常量或者常量表达式 常量表达式中的函数必须是内置函数 package main import ( "fmt ...

  9. java web 答辩总结

    今天我们组答辩.在昨天前三个组答辩之后,整理了一些试题. 在这个项目的答辩准备:首先把这个java web这本书大概的看了一遍:对整理的那些试题也把答案整理出来了:针对老师提问频率较高的试题:针对自己 ...

  10. netty源码解解析(4.0)-4 线程模型-概览

    netty线程体系概览 netty的高并发能力很大程度上由它的线程模型决定的,netty定义了两种类型的线程: I/O线程: EventLoop, EventLoopGroup.一个EventLoop ...