ubuntu下配置时间同步NTP

简介:

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.12-0.1ubuntu1_amd64.deb 
Selecting previously unselected package libopts25.
(Reading database ... 51276 files and directories currently installed.)
Unpacking libopts25 (from libopts25_1%3a5.12-0.1ubuntu1_amd64.deb) ...
Setting up libopts25 (1:5.12-0.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 ... 51324 files and directories currently installed.)
Preparing to replace ntp 1:4.2.6.p3+dfsg-1ubuntu3.1 (using ntp_1%3a4.2.6.p3+dfsg-1ubuntu3.1_amd64.deb) ...
Unpacking replacement ntp ...
Setting up ntp (1:4.2.6.p3+dfsg-1ubuntu3.1) ...
 * 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
27 Feb 14:59:32 ntpdate[18546]: 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
27 Feb 14:58:13 ntpdate[18557]: step time server 10.138.16.232 offset -98.402560 sec
复制代码

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

 

 本文转自xwdreamer博客园博客,原文链接:http://www.cnblogs.com/xwdreamer/p/3448773.html,如需转载请自行联系原作者

目录
相关文章
|
2月前
|
网络协议 Java 应用服务中间件
Springboot+ubuntu+Let‘s Encrypt配置https
Springboot+ubuntu+Let‘s Encrypt配置https
35 0
|
2月前
|
Ubuntu
百度搜索:蓝易云【Ubuntu开机自启服务systemd.service配置教程】
现在,你的服务将在Ubuntu开机时自动启动,并在之后的启动中持续运行。记得根据你的实际需求修改 `your_service_name.service`文件中的相关信息。
56 2
|
3月前
|
存储 网络协议 Ubuntu
如何在Ubuntu安装配置SVN服务端并实现无公网ip访问内网资料库
如何在Ubuntu安装配置SVN服务端并实现无公网ip访问内网资料库
72 0
|
3月前
|
Ubuntu 安全 网络安全
百度搜索:蓝易云【Ubuntu系统SSH服务端配置】
现在,你已经成功在Ubuntu系统上配置了SSH服务端。这将允许其他计算机通过SSH协议连接到你的Ubuntu系统,并进行远程管理和操作。请注意,远程访问有安全风险,建议在生产环境中采取必要的安全措施来保护系统。
40 3
|
3月前
|
存储 Ubuntu
百度搜索:蓝易云【在ubuntu系统下安装配置onedrive步骤】
现在,你已经成功在Ubuntu系统下安装和配置了OneDrive,可以使用该工具与OneDrive云端存储进行同步。
41 0
|
3月前
|
Ubuntu
百度搜索:蓝易云【Ubuntu系统新增硬盘,配置自动挂载教程】
现在,您的新硬盘应该已经成功配置为开机自动挂载到 `/mnt/new_disk`目录下了。在每次开机后,Ubuntu会自动将该硬盘挂载到指定的挂载点,您可以在该挂载点下访问和使用新硬盘。
41 0
|
2月前
|
负载均衡 Ubuntu 应用服务中间件
|
1天前
|
Ubuntu Linux 测试技术
Linux(32)Rockchip RK3568 Ubuntu22.04上部署 Docker: 详细配置与功能测试(下)
Linux(32)Rockchip RK3568 Ubuntu22.04上部署 Docker: 详细配置与功能测试
19 1
|
2天前
|
Ubuntu 网络协议 Linux
Linux(20) Ubuntu 20.04 网络接口自动切换路由配置
Linux(20) Ubuntu 20.04 网络接口自动切换路由配置
23 0
|
17天前
|
Ubuntu 开发工具 git
ubuntu18.04下配置muduoC++11环境
以上步骤将在Ubuntu 18.04下配置C++11环境,并编译安装muduo库。请根据实际情况对配置步骤进行调整。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
15 0