Linux时间同步(内外网/内网/外网)


声明:本文转载自https://my.oschina.net/u/3715199/blog/1560464,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

内外网集群的时间同步①

server端:可以访问外网的机器
client端:在内网里的机器
一、server端安装ntp校时包,修改独立主机
rm -rf /etc/localtime #先清除原有的链接文件
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #修改时区到东8区。
date -R #查看的时区设置。
接下来调整系统时间与时间服务器同步

Debian系统安装NTP校时包: 代码如下: apt-get install ntpdate #安装ntp  CentOS系统安装NTP校时包: 代码如下: yum -y install ntpdate ntp #安装ntp  Ubuntu系统安装NTP校时包: 代码如下: sudo apt-get install -y ntpdate ntp 

二、server端修改/etc/ntp.conf配置文件
编辑 /etc/ntp.conf

server cn.pool.ntp.org restrict default nomodifynotrapnoquery restrict 127.0.0.1   # 开启内部递归网络接口 lo restrict 192.168.9.0 mask 255.255.255.0 nomodify notrap #在内部子网里面的客户端可以 进行网络校时,但不能修改NTP服务器的时间参数 

由于配置的是本地时间 ,所以还需要配置一个使用系统时钟作为时间源的NTP服务器,需要在/etc/ntp.conf文件中添加如下的记录:

server 127.127.1.0 fudge 127.127.1.0 stratum 10 

然后保存退出
接着输入下面的命令:
ntpdate -d cn.pool.ntp.org #调试查看时间差异
ntpdate cn.pool.ntp.org #手动校准系统时间
date -R # 检查时间是否同步

三、server端修改ntpd 的系统配置
vi /etc/sysconfig/ntpd
SYNC_HWCLOCK=yes #同步独立主机的硬件时钟
然后:wq退出
chkconfig --levels 235 ntpd on #配置开机启动ntp服务,定期同步时间
/etc/init.d/ntpd start#启动ntp同步

四、client端配置ntpd服务
注:client端保证与server端的网络通畅
编辑/etc/ntp.conf文件,前面的这两行注释掉

#restrict default kod nomodify notrap nopeer noquery #restrict -6 default kod nomodify notrap nopeer noquery  #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 

在里面加上

restrict 服务端地址 nomodify server 服务端地址 prefer 

service ntpd start启动ntpd服务
chkconfig ntpd on 加入开机启动项

五、检查ntpd的工作情况(网络连通性(是否开放了123端口)和同步情况)
ntpq -p查看是否能正确输出
date -R 每台机器查看时间
linux下防火墙规则如果极严格的话可能会影响ntpd对时,打开 sport 123 即可
iptables -I INPUT -p udp -m udp --sport 123 -j ACCEPT

脚本待开发...

waiting... 

内网linux服务器之间时间同步②

  1. 首先设置主机器,先设置时区,在修改配置文件
    rm -rf /etc/localtime #先清除原有的链接文件
    ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #修改时区到东8区。
    date -R #查看的时区设置。将本机时间修改为标准时间
    hwclock --systohc && hwclock -w 使用系统时间刷入硬件时间

编辑/etc/ntp.conf文件
在里面加上
restrict 127.0.0.1   # 开启内部递归网络接口 lo
restrict 192.168.5.0 mask 255.255.255.0 nomodify #在内部子网里面的客户端可以 进行网络校时,但不能修改NTP服务器的时间参数

  1. 由于配置的是本地时间 ,所以还需要配置一个使用系统时钟作为时间源的NTP服务器,需要在/etc/ntp.conf文件中添加如下的记录:
server 127.127.1.0 fudge 127.127.1.0 stratun 10 

在以上的记录中:
指定的IP地址是一个“伪”IP地址,确定本地系统为时间源。
指定的IP地址127.127.1.1告诉NTP使用内部时钟作为时间源。
"fudge"定义了这个时钟的级别,如果没有这个记录,节点就是一级服务器。将级别重新定义为10是个好的办法,这样客户端在查询这个服务器的时候就会知道这个服务器不是一个可靠的时间源.
这种时间同步的方式只应该在本地的网络不能使用外部的时间源的时候使用

  1. 启动ntp服务
    service ntpd start
    设置开机启动
    chkconfig ntpd on
  2. 然后配置客户端,在192.168.5.103这台主机上面,编辑/etc/crontab这个文件,用于设置此节点每分钟要像主服务器同步时间
    设置如下:
    * * * * * root /usr/sbin/ntpdate 192.168.5.102;/sbin/hwclock -w
    查看时间是否一致

最后提及一点,ntp服务,默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件。

在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes 这样,就可以让硬件时间与系统时间一起同步。

自动脚本如下:

#!/bin/bash cat << EOF  >> /etc/ntp.conf  restrict 127.0.0.1 restrict 192.168.5.0 mask 255.255.255.0 nomodify server 127.127.1.0 fudge 127.127.1.0 stratun 10 EOF service ntp restart chkconfig ntpd on #客户端修改内容 echo >>  

更新脚本2

#!/bin/bash #备份源文件 mv /etc/localtime /etc/localtimebak #修改时区为东八区 ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #校准当前时间 date -s "2017-10-24 12:09:38" #使用系统时间刷入硬件时间 hwclock --systohc #修改配置文件 cat << EOF  >> /etc/ntp.conf  restrict 127.0.0.1 restrict 192.168.9.0 mask 255.255.255.0 nomodify server 127.127.1.0 fudge 127.127.1.0 stratun 10 EOF #重启开机自启 service ntpd restart chkconfig ntpd on 

外网单台机器设置时间同步③

一、安装ntp校时包,修改独立主机
rm -rf /etc/localtime #先清除原有的链接文件
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #修改时区到东8区。
date -R #查看的时区设置。
接下来调整系统时间与时间服务器同步

Debian系统安装NTP校时包: 代码如下: apt-get install ntpdate #安装ntp  CentOS系统安装NTP校时包: 代码如下: yum -y install ntpdate ntp #安装ntp  Ubuntu系统安装NTP校时包: 代码如下: sudo apt-get install -y ntpdate ntp 

二、修改/etc/ntp.conf配置文件
vi /etc/ntp.conf 就会看到以下内容:

server 0.centos.ntp.org server time.windows.com server time.nist.gov 

这是默认的ntp同步服务器,大家可以自己改,全球ntp服务器地址:http://www.pool.ntp.org/
cn.pool.ntp.org //这中国的ntp服务器
我改成了:

server cn.pool.ntp.org server time-a.nist.gov server time.windows.com server time.nist.gov 

然后保存退出
接着输入下面的命令:
ntpdate -d cn.pool.ntp.org #调试查看时间差异
ntpdate cn.pool.ntp.org #同步时间
date -R # 检查时间是否同步

三、修改ntp 的系统配置
vi /etc/sysconfig/ntpd
SYNC_HWCLOCK=yes #同步独立主机的硬件时钟
然后:wq退出
chkconfig --levels 235 ntpd on #配置开机启动ntp服务,定期同步时间
/etc/init.d/ntpd start#启动ntp同步

自动脚本如下:

#!/bin/bash #备份源文件 mv /etc/localtime /etc/localtimebak #修改时区为东八区 ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #安装ntp服务 yum -y install ntpdate ntp #修改/etc/ntp.conf  cat << EOF  >> /etc/ntp.conf  server cn.pool.ntp.org server time-a.nist.gov server time.windows.com server time.nist.gov EOF #调试查看时间差异 ntpdate -d cn.pool.ntp.org #同步时间 ntpdate cn.pool.ntp.org && echo "SYNC_HWCLOCK=yes" >>/etc/sysconfig/ntpd || echo "Setting Filed!" #自启动 chkconfig --levels 235 ntpd on /etc/init.d/ntpd start echo `date` 

本文发表于2017年11月03日 16:34
(c)注:本文转载自https://my.oschina.net/u/3715199/blog/1560464,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 2216 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1