Linux配置NTP时间同步

一:NTP是网络时间同步协议,就是用来同步网络中各个计算机的时间的协议。

二:NTP服务端配置

1.检查系统是否安装了NTP包(linux系统一般自带NTP4.2),没有安装我们直接使用yum命令在线安装: yum install ntp

2.NTP服务端配置文件编辑: vim /etc/ntp.conf

结果:

https://img-blog.csdn.net/20180413144739288

# @3新增-权限配置restrict 127.127.1.0restrict 192.168.31.0 mask 255.255.255.0 nomodify notrap# @3改动-注释掉上级时间服务器地址#server 0.centos.pool.ntp.org iburst#server 1.centos.pool.ntp.org iburst#server 2.centos.pool.ntp.org iburst#server 3.centos.pool.ntp.org iburst# @4新增-上级时间服务器server 127.127.1.0 # local clockfudge 127.127.1.0 stratum 10

2.启动NTP时间服务器:service ntpd start

3.设置NTP开机自动启动:chkconfig ntpd on

4.查看NTP是否正常运行:netstat -tlunp | grep ntp

5.配置防火墙过滤规则:/sbin/iptables -I INPUT -p udp --dport 123 -j ACCEPT

如何配置:/etc/sysconfig/iptables 文件内配置开放udp 123端口: -A INPUT -p udp --destination-port 123 -j ACCEPT

 

A.服务端配置文件解释

①1:设定NTP主机来源(其中prefer表示优先主机),192.168.31.134是本地的NTP服务器,所以优先指定从该主机同步时间。

server 192.168.7.49 prefer

server 0.rhel.pool.ntp.org

server 1.rhel.pool.ntp.org

server 2.rhel.pool.ntp.org

server 3.rhel.pool.ntp.org

 

②2:限制你允许的这些服务器的访问类型,在这个例子中的服务器是不容许修改运行时配置或查询您的Linux NTP服务器

restrict 192.168.0.0 mask 255.255.255.0 notrust nomodify notrap

 

在上例中,掩码地址扩展为255,因此从192.168.0.1-192.168.0.254的服务器都可以使用我们的NTP服务器来同步时间

#此时表示限制向从192.168.0.1-192.168.0.254这些IP段的服务器提供NTP服务。

restrict 192.168.0.0 mask 255.255.255.0 notrust nomodify notrap noquery

#设置默认策略为允许任何主机进行时间同步

restrict default ignore

 

二:NTP客户端配置

1.检查安装NTP服务有没有安装,未安装请自行安装

2.NTP客户端配置文件编辑: vim /etc/ntp.conf

https://img-blog.csdn.net/20180413144811458

# @1新增-权限配置restrict 192.168.31.0 mask 255.255.255.0 nomodify notrap# Use public servers from the pool.ntp.org project.# Please consider joining the pool (http://www.pool.ntp.org/join.html).# 注释掉原来的实际服务器地址#server 0.centos.pool.ntp.org iburst#server 1.centos.pool.ntp.org iburst#server 2.centos.pool.ntp.org iburst#server 3.centos.pool.ntp.org iburst# @2新增-自己的时间服务器地址server 192.168.31.223 prefer <==以這部主機為最優先#broadcast 192.168.1.255 autokey # broadcast server#broadcastclient # broadcast client#broadcast 224.0.1.1 autokey # multicast server#multicastclient 224.0.1.1 # multicast client#manycastserver 239.255.254.254 # manycast server#manycastclient 239.255.254.254 autokey # manycast client

3.手动同步一次时间:/usr/sbin/ntpdate192.168.31.134 (服务端主机IP,这里需要先关闭NTP服务哦)

4.启动NTP服务:service ntpd start

5.观察时间同步状况:ntpq -p

结果:

[root@localhost hct]# ntpq -p remote refid st t when poll reach delay offset jitter==============================================================================*192.168.31.134 LOCAL(0) 11 u 64 128 377 0.202 73.980 412.834

⑥查看时间同步结果:ntpstat

[root@hct ~]# ntpstat

unsynchronised

polling server every 8 s

同步失败,同步也需要时间嘛,需等待5-10分钟再次查询:

Every 2.0s: ntpstat Tue Jul 11 16:55:57 2017synchronised to NTP server (10.10.11.247) at stratum 12 time correct to within 605 ms polling server every 128 s

 

OK!时间同步完成,date一下是不是和服务器主机时间一致呢

 

B.客户端配置文件详解

修改/etc/ntp/stpe-tickers文件,内容如下(当ntpd服务启动时,会自动与该文件中记录的上层NTP服务进行时间校对

https://img-blog.csdn.net/2018041314483676

C.系统时间与硬件时间同步

 如果主从服务时间超过1000秒则不再进行同步了,这时候要手动同步,即:/usr/sbin/ntpdate命令,如果怕服务器时差会经常变动比较大可以再Linux中添加计划任务,例如:

10 5 * * * root /usr/sbin/ntpdate 192.168.31.223 && /sbin/hwclock -w

 

     ntp服务,默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件,在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes 这样,就可以让硬件时间与系统时间一起同步。

 

D:ntpq -p各个选项相关信息

restrict 控制相关权限。

语法为: restrict IP地址 mask 子网掩码 参数

其中IP地址也可以是default ,default 就是指所有的IP

参数有以下几个:

ignore  :关闭所有的 NTP 联机服务

nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。

notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网

noquery :不提供客户端的时间查询:用户端不能使用ntpq,ntpc等命令来查询ntp服务器

notrap :不提供trap远端登陆:拒绝为匹配的主机提供模式 6 控制消息陷阱服务。陷阱服务是 ntpdq 控制消息协议的子系统,用于远程事件日志记录程序。

nopeer :用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟

kod : 访问违规时发送 KoD 包。

restrict -6 表示IPV6地址的权限设置。

 

root@www ~]# vim /etc/ntp.conf# 1. 先處理權限方面的問題,包括放行上層伺服器以及開放區網用戶來源:restrict default kod nomodify notrap nopeer noquery <==拒絕 IPv4 的用戶restrict -6 default kod nomodify notrap nopeer noquery <==拒絕 IPv6 的用戶restrict 220.130.158.71 <==放行 tock.stdtime.gov.tw 進入本 NTP 伺服器restrict 59.124.196.83 <==放行 tick.stdtime.gov.tw 進入本 NTP 伺服器restrict 59.124.196.84 <==放行 time.stdtime.gov.tw 進入本 NTP 伺服器restrict 127.0.0.1 <==底下兩個是預設值,放行本機來源restrict -6 ::1restrict 192.168.100.0 mask 255.255.255.0 nomodify <==放行區網來源# 2. 設定主機來源,請先將原本的 [0|1|2].centos.pool.ntp.org 的設定註解掉:server 220.130.158.71 prefer <==以這部主機為最優先server 59.124.196.83server 59.124.196.84# 3.預設時間差異分析檔案與暫不用到的 keys 等,不需要更動它:driftfile /var/lib/ntp/driftkeys /etc/ntp/keys

 

ntpd、ntpdate的区别

下面是网上关于ntpd与ntpdate区别的相关资料。如下所示所示:

使用之前得弄清楚一个问题,ntpd与ntpdate在更新时间时有什么区别。ntpd不仅仅是时间同步服务器,它还可以做客户端与标准时间服务器进行同步时间,而且是平滑同步,并非ntpdate立即同步,在生产环境中慎用ntpdate,也正如此两者不可同时运行。

时钟的跃变,对于某些程序会导致很严重的问题。许多应用程序依赖连续的时钟——毕竟,这是一项常见的假定,即,取得的时间是线性的,一些操作,例如数据库事务,通常会地依赖这样的事实:时间不会往回跳跃。不幸的是,ntpdate调整时间的方式就是我们所说的”跃变“:在获得一个时间之后,ntpdate使用settimeofday(2)设置系统时间,这有几个非常明显的问题:

第一,这样做不安全。ntpdate的设置依赖于ntp服务器的安全性,攻击者可以利用一些软件设计上的缺陷,拿下ntp服务器并令与其同步的服务器执行某些消耗性的任务。由于ntpdate采用的方式是跳变,跟随它的服务器无法知道是否发生了异常(时间不一样的时候,唯一的办法是以服务器为准)。

第二,这样做不精确。一旦ntp服务器宕机,跟随它的服务器也就会无法同步时间。与此不同,ntpd不仅能够校准计算机的时间,而且能够校准计算机的时钟。

第三,这样做不够优雅。由于是跳变,而不是使时间变快或变慢,依赖时序的程序会出错(例如,如果ntpdate发现你的时间快了,则可能会经历两个相同的时刻,对某些应用而言,这是致命的)。因而,唯一一个可以令时间发生跳变的点,是计算机刚刚启动,但还没有启动很多服务的那个时候。其余的时候,理想的做法是使用ntpd来校准时钟,而不是调整计算机时钟上的时间。

NTPD 在和时间服务器的同步过程中,会把 BIOS 计时器的振荡频率偏差——或者说 Local Clock 的自然漂移(drift)——记录下来。这样即使网络有问题,本机仍然能维持一个相当精确的走时。

 

友情链接:

http://linux.vbird.org/linux_server/0440ntp.php#server_ntp.conf

http://blog.csdn.net/blueskyliulan/article/details/49278339

---------------------

作者:潮鳴

来源:CSDN

原文:https://blog.csdn.net/willinge/article/details/79928726

版权声明:本文为博主原创文章,转载请附上博文链接!

一:NTP是网络时间同步协议,就是用来同步网络中各个计算机的时间的协议。

二:NTP服务端配置

1.检查系统是否安装了NTP包(linux系统一般自带NTP4.2),没有安装我们直接使用yum命令在线安装: yum install ntp

2.NTP服务端配置文件编辑: vim /etc/ntp.conf

结果:

https://img-blog.csdn.net/20180413144739288

# @3新增-权限配置restrict 127.127.1.0restrict 192.168.31.0 mask 255.255.255.0 nomodify notrap# @3改动-注释掉上级时间服务器地址#server 0.centos.pool.ntp.org iburst#server 1.centos.pool.ntp.org iburst#server 2.centos.pool.ntp.org iburst#server 3.centos.pool.ntp.org iburst# @4新增-上级时间服务器server 127.127.1.0 # local clockfudge 127.127.1.0 stratum 10

2.启动NTP时间服务器:service ntpd start

3.设置NTP开机自动启动:chkconfig ntpd on

4.查看NTP是否正常运行:netstat -tlunp | grep ntp

5.配置防火墙过滤规则:/sbin/iptables -I INPUT -p udp --dport 123 -j ACCEPT

如何配置:/etc/sysconfig/iptables 文件内配置开放udp 123端口: -A INPUT -p udp --destination-port 123 -j ACCEPT

 

A.服务端配置文件解释

①1:设定NTP主机来源(其中prefer表示优先主机),192.168.31.134是本地的NTP服务器,所以优先指定从该主机同步时间。

server 192.168.7.49 prefer

server 0.rhel.pool.ntp.org

server 1.rhel.pool.ntp.org

server 2.rhel.pool.ntp.org

server 3.rhel.pool.ntp.org

 

②2:限制你允许的这些服务器的访问类型,在这个例子中的服务器是不容许修改运行时配置或查询您的Linux NTP服务器

restrict 192.168.0.0 mask 255.255.255.0 notrust nomodify notrap

 

在上例中,掩码地址扩展为255,因此从192.168.0.1-192.168.0.254的服务器都可以使用我们的NTP服务器来同步时间

#此时表示限制向从192.168.0.1-192.168.0.254这些IP段的服务器提供NTP服务。

restrict 192.168.0.0 mask 255.255.255.0 notrust nomodify notrap noquery

#设置默认策略为允许任何主机进行时间同步

restrict default ignore

 

二:NTP客户端配置

1.检查安装NTP服务有没有安装,未安装请自行安装

2.NTP客户端配置文件编辑: vim /etc/ntp.conf

https://img-blog.csdn.net/20180413144811458

# @1新增-权限配置restrict 192.168.31.0 mask 255.255.255.0 nomodify notrap# Use public servers from the pool.ntp.org project.# Please consider joining the pool (http://www.pool.ntp.org/join.html).# 注释掉原来的实际服务器地址#server 0.centos.pool.ntp.org iburst#server 1.centos.pool.ntp.org iburst#server 2.centos.pool.ntp.org iburst#server 3.centos.pool.ntp.org iburst# @2新增-自己的时间服务器地址server 192.168.31.223 prefer <==以這部主機為最優先#broadcast 192.168.1.255 autokey # broadcast server#broadcastclient # broadcast client#broadcast 224.0.1.1 autokey # multicast server#multicastclient 224.0.1.1 # multicast client#manycastserver 239.255.254.254 # manycast server#manycastclient 239.255.254.254 autokey # manycast client

3.手动同步一次时间:/usr/sbin/ntpdate192.168.31.134 (服务端主机IP,这里需要先关闭NTP服务哦)

4.启动NTP服务:service ntpd start

5.观察时间同步状况:ntpq -p

结果:

[root@localhost hct]# ntpq -p remote refid st t when poll reach delay offset jitter==============================================================================*192.168.31.134 LOCAL(0) 11 u 64 128 377 0.202 73.980 412.834

⑥查看时间同步结果:ntpstat

[root@hct ~]# ntpstat

unsynchronised

polling server every 8 s

同步失败,同步也需要时间嘛,需等待5-10分钟再次查询:

Every 2.0s: ntpstat Tue Jul 11 16:55:57 2017synchronised to NTP server (10.10.11.247) at stratum 12 time correct to within 605 ms polling server every 128 s

 

OK!时间同步完成,date一下是不是和服务器主机时间一致呢

 

B.客户端配置文件详解

修改/etc/ntp/stpe-tickers文件,内容如下(当ntpd服务启动时,会自动与该文件中记录的上层NTP服务进行时间校对

https://img-blog.csdn.net/2018041314483676

C.系统时间与硬件时间同步

 如果主从服务时间超过1000秒则不再进行同步了,这时候要手动同步,即:/usr/sbin/ntpdate命令,如果怕服务器时差会经常变动比较大可以再Linux中添加计划任务,例如:

10 5 * * * root /usr/sbin/ntpdate 192.168.31.223 && /sbin/hwclock -w

 

     ntp服务,默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件,在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes 这样,就可以让硬件时间与系统时间一起同步。

 

D:ntpq -p各个选项相关信息

restrict 控制相关权限。

语法为: restrict IP地址 mask 子网掩码 参数

其中IP地址也可以是default ,default 就是指所有的IP

参数有以下几个:

ignore  :关闭所有的 NTP 联机服务

nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。

notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网

noquery :不提供客户端的时间查询:用户端不能使用ntpq,ntpc等命令来查询ntp服务器

notrap :不提供trap远端登陆:拒绝为匹配的主机提供模式 6 控制消息陷阱服务。陷阱服务是 ntpdq 控制消息协议的子系统,用于远程事件日志记录程序。

nopeer :用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟

kod : 访问违规时发送 KoD 包。

restrict -6 表示IPV6地址的权限设置。

 

root@www ~]# vim /etc/ntp.conf# 1. 先處理權限方面的問題,包括放行上層伺服器以及開放區網用戶來源:restrict default kod nomodify notrap nopeer noquery <==拒絕 IPv4 的用戶restrict -6 default kod nomodify notrap nopeer noquery <==拒絕 IPv6 的用戶restrict 220.130.158.71 <==放行 tock.stdtime.gov.tw 進入本 NTP 伺服器restrict 59.124.196.83 <==放行 tick.stdtime.gov.tw 進入本 NTP 伺服器restrict 59.124.196.84 <==放行 time.stdtime.gov.tw 進入本 NTP 伺服器restrict 127.0.0.1 <==底下兩個是預設值,放行本機來源restrict -6 ::1restrict 192.168.100.0 mask 255.255.255.0 nomodify <==放行區網來源# 2. 設定主機來源,請先將原本的 [0|1|2].centos.pool.ntp.org 的設定註解掉:server 220.130.158.71 prefer <==以這部主機為最優先server 59.124.196.83server 59.124.196.84# 3.預設時間差異分析檔案與暫不用到的 keys 等,不需要更動它:driftfile /var/lib/ntp/driftkeys /etc/ntp/keys

 

ntpd、ntpdate的区别

下面是网上关于ntpd与ntpdate区别的相关资料。如下所示所示:

使用之前得弄清楚一个问题,ntpd与ntpdate在更新时间时有什么区别。ntpd不仅仅是时间同步服务器,它还可以做客户端与标准时间服务器进行同步时间,而且是平滑同步,并非ntpdate立即同步,在生产环境中慎用ntpdate,也正如此两者不可同时运行。

时钟的跃变,对于某些程序会导致很严重的问题。许多应用程序依赖连续的时钟——毕竟,这是一项常见的假定,即,取得的时间是线性的,一些操作,例如数据库事务,通常会地依赖这样的事实:时间不会往回跳跃。不幸的是,ntpdate调整时间的方式就是我们所说的”跃变“:在获得一个时间之后,ntpdate使用settimeofday(2)设置系统时间,这有几个非常明显的问题:

第一,这样做不安全。ntpdate的设置依赖于ntp服务器的安全性,攻击者可以利用一些软件设计上的缺陷,拿下ntp服务器并令与其同步的服务器执行某些消耗性的任务。由于ntpdate采用的方式是跳变,跟随它的服务器无法知道是否发生了异常(时间不一样的时候,唯一的办法是以服务器为准)。

第二,这样做不精确。一旦ntp服务器宕机,跟随它的服务器也就会无法同步时间。与此不同,ntpd不仅能够校准计算机的时间,而且能够校准计算机的时钟。

第三,这样做不够优雅。由于是跳变,而不是使时间变快或变慢,依赖时序的程序会出错(例如,如果ntpdate发现你的时间快了,则可能会经历两个相同的时刻,对某些应用而言,这是致命的)。因而,唯一一个可以令时间发生跳变的点,是计算机刚刚启动,但还没有启动很多服务的那个时候。其余的时候,理想的做法是使用ntpd来校准时钟,而不是调整计算机时钟上的时间。

NTPD 在和时间服务器的同步过程中,会把 BIOS 计时器的振荡频率偏差——或者说 Local Clock 的自然漂移(drift)——记录下来。这样即使网络有问题,本机仍然能维持一个相当精确的走时。

 

友情链接:

http://linux.vbird.org/linux_server/0440ntp.php#server_ntp.conf

http://blog.csdn.net/blueskyliulan/article/details/49278339

---------------------

作者:潮鳴

来源:CSDN

原文:https://blog.csdn.net/willinge/article/details/79928726

版权声明:本文为博主原创文章,转载请附上博文链接!

  • 5
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值