Linux服务器时间同步那些事

系统 Linux
本文主要介绍内外网集群的时间同步、内网linux服务器之间时间同步和外网单台机器设置时间同步的那些事,希望对大家有所帮助。

Linux服务器时间同步那些事

内外网集群的时间同步①

  • Server端:可以访问外网的机器
  • Client端:在内网里的机器

一、server端安装ntp校时包,修改独立主机

rm -rf /etc/localtime #先清除原有的链接文件

ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #修改时区到东8区。

date -R #查看的时区设置。

接下来调整系统时间与时间服务器同步

Debian系统安装NTP校时包:

代码如下:

  1. apt-get install ntpdate #安装ntp 

CentOS系统安装NTP校时包:

代码如下: 

  1. yum -y install ntpdate ntp #安装ntp 

Ubuntu系统安装NTP校时包:

代码如下:

  1. sudo apt-get install -y ntpdate ntp 

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

编辑 /etc/ntp.conf

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

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

  1. server 127.127.1.0 
  2.  
  3. fudge 127.127.1.0 stratum 10  

然后保存退出

接着输入下面的命令:

  1. ntpdate -d cn.pool.ntp.org #调试查看时间差异 
  2.  
  3. ntpdate cn.pool.ntp.org #手动校准系统时间 
  4.  
  5. date -R # 检查时间是否同步 

三、server端修改ntpd 的系统配置

  1. vi /etc/sysconfig/ntpd 
  2.  
  3. SYNC_HWCLOCK=yes #同步独立主机的硬件时钟  

然后:wq退出

  1. chkconfig --levels 235 ntpd on #配置开机启动ntp服务,定期同步时间 
  2. /etc/init.d/ntpd start#启动ntp同步 

四、client端配置ntpd服务

注:client端保证与server端的网络通畅

编辑/etc/ntp.conf文件,前面的这两行注释掉

  1. #restrict default kod nomodify notrap nopeer noquery 
  2.  
  3. #restrict -6 default kod nomodify notrap nopeer noquery 
  4.  
  5. #server 0.centos.pool.ntp.org iburst 
  6.  
  7. #server 1.centos.pool.ntp.org iburst 
  8.  
  9. #server 2.centos.pool.ntp.org iburst 
  10.  
  11. #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 即可

  1. iptables -I INPUT -p udp -m udp --sport 123 -j ACCEPT 

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

首先设置主机器,先设置时区,在修改配置文件

  1. rm -rf /etc/localtime #先清除原有的链接文件 
  2.  
  3. ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #修改时区到东8区。 
  4.  
  5. date -R #查看的时区设置。将本机时间修改为标准时间  

hwclock --systohc && hwclock -w 使用系统时间刷入硬件时间

编辑/etc/ntp.conf文件

在里面加上

  1. restrict 127.0.0.1   # 开启内部递归网络接口 lo 
  2.  
  3. restrict 192.168.5.0 mask 255.255.255.0 nomodify #在内部子网里面的客户端可以 进行网络校时,但不能修改NTP服务器的时间参数 

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

  1. server 127.127.1.0 
  2.  
  3. fudge 127.127.1.0 stratun 10  

在以上的记录中:

指定的IP地址是一个“伪”IP地址,确定本地系统为时间源。

指定的IP地址127.127.1.1告诉NTP使用内部时钟作为时间源。

"fudge"定义了这个时钟的级别,如果没有这个记录,节点就是一级服务器。将级别重新定义为10是个好的办法,这样客户端在查询这个服务器的时候就会知道这个服务器不是一个可靠的时间源.

这种时间同步的方式只应该在本地的网络不能使用外部的时间源的时候使用

启动ntp服务

  1. service ntpd start 

设置开机启动

  1. chkconfig ntpd on 

然后配置客户端,在192.168.5.103这台主机上面,编辑/etc/crontab这个文件,用于设置此节点每分钟要像主服务器同步时间

设置如下:

  1. * * * * * root /usr/sbin/ntpdate 192.168.5.102;/sbin/hwclock -w 

查看时间是否一致

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

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

自动脚本如下:

  1. #!/bin/bash 
  2.  
  3. cat << EOF  >> /etc/ntp.conf  
  4.  
  5. restrict 127.0.0.1 
  6.  
  7. restrict 192.168.5.0 mask 255.255.255.0 nomodify 
  8.  
  9. server 127.127.1.0 
  10.  
  11. fudge 127.127.1.0 stratun 10 
  12.  
  13. EOF 
  14.  
  15. service ntp restart 
  16.  
  17. chkconfig ntpd on 
  18.  
  19. #客户端修改内容 
  20.  
  21. echo >>   

更新脚本2

  1. #!/bin/bash 
  2.  
  3. #备份源文件 
  4.  
  5. mv /etc/localtime /etc/localtimebak 
  6.  
  7. #修改时区为东八区 
  8.  
  9. ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 
  10.  
  11. #校准当前时间 
  12.  
  13. date -s "2017-10-24 12:09:38" 
  14.  
  15. #使用系统时间刷入硬件时间 
  16.  
  17. hwclock --systohc 
  18.  
  19. #修改配置文件 
  20.  
  21. cat << EOF  >> /etc/ntp.conf  
  22.  
  23. restrict 127.0.0.1 
  24.  
  25. restrict 192.168.9.0 mask 255.255.255.0 nomodify 
  26.  
  27. server 127.127.1.0 
  28.  
  29. fudge 127.127.1.0 stratun 10 
  30.  
  31. EOF 
  32.  
  33. #重启开机自启 
  34.  
  35. service ntpd restart 
  36.  
  37. chkconfig ntpd on 

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

一、安装ntp校时包,修改独立主机

  1. rm -rf /etc/localtime #先清除原有的链接文件 
  2.  
  3. ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #修改时区到东8区。 
  4.  
  5. date -R #查看的时区设置。  

接下来调整系统时间与时间服务器同步

Debian系统安装NTP校时包:

代码如下:

  1. apt-get install ntpdate #安装ntp 

CentOS系统安装NTP校时包:

代码如下:

  1. yum -y install ntpdate ntp #安装ntp 

Ubuntu系统安装NTP校时包:

代码如下:

  1. sudo apt-get install -y ntpdate ntp 

二、修改/etc/ntp.conf配置文件

vi /etc/ntp.conf 就会看到以下内容:

  1. server 0.centos.ntp.org 
  2.  
  3. server time.windows.com 
  4.  
  5. server time.nist.gov  

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

cn.pool.ntp.org //这中国的ntp服务器

我改成了:

  1. server cn.pool.ntp.org 
  2.  
  3. server time-a.nist.gov 
  4.  
  5. server time.windows.com 
  6.  
  7. server time.nist.gov  

然后保存退出

接着输入下面的命令:

  1. ntpdate -d cn.pool.ntp.org #调试查看时间差异 
  2.  
  3. ntpdate cn.pool.ntp.org #同步时间 
  4.  
  5. date -R # 检查时间是否同步 

三、修改ntp 的系统配置

  1. vi /etc/sysconfig/ntpd 
  2.  
  3. SYNC_HWCLOCK=yes #同步独立主机的硬件时钟  

然后:wq退出

  1. chkconfig --levels 235 ntpd on #配置开机启动ntp服务,定期同步时间 
  2.  
  3. /etc/init.d/ntpd start#启动ntp同步 

自动脚本如下:

  1. #!/bin/bash 
  2.  
  3. #备份源文件 
  4.  
  5. mv /etc/localtime /etc/localtimebak 
  6.  
  7. #修改时区为东八区 
  8.  
  9. ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 
  10.  
  11. #安装ntp服务 
  12.  
  13. yum -y install ntpdate ntp 
  14.  
  15. #修改/etc/ntp.conf  
  16.  
  17. cat << EOF  >> /etc/ntp.conf  
  18.  
  19. server cn.pool.ntp.org 
  20.  
  21. server time-a.nist.gov 
  22.  
  23. server time.windows.com 
  24.  
  25. server time.nist.gov 
  26.  
  27. EOF 
  28.  
  29. #调试查看时间差异 
  30.  
  31. ntpdate -d cn.pool.ntp.org 
  32.  
  33. #同步时间 
  34.  
  35. ntpdate cn.pool.ntp.org && echo "SYNC_HWCLOCK=yes" >>/etc/sysconfig/ntpd || echo "Setting Filed!" 
  36.  
  37. #自启动 
  38.  
  39. chkconfig --levels 235 ntpd on 
  40.  
  41. /etc/init.d/ntpd start 
  42.  
  43. echo `date`   
责任编辑:庞桂玉 来源: 运维派
相关推荐

2010-09-27 14:25:39

DHCP服务器

2009-02-13 11:06:00

时间同步服务器服务器

2015-07-23 13:10:38

服务器虚拟化

2023-03-17 08:50:00

服务器时钟服务数据库

2015-09-16 13:19:46

javascript服务器

2017-05-15 21:50:54

Linux引号

2010-08-06 14:46:51

思科路由器NTP服务器配置

2018-09-19 12:08:04

Linux服务器数据

2009-11-11 10:18:02

2010-09-02 10:50:17

时间同步服务器

2018-04-19 18:34:42

互联网

2021-08-30 12:05:46

Linux字节对齐代码

2010-03-04 14:15:38

Ubuntu MySQ

2024-03-18 00:00:05

Java服务JVM

2022-06-05 13:51:47

SentinelOpenFeign服务熔断

2010-08-20 10:29:40

NTP

2017-01-22 17:15:26

服务器

2021-08-06 11:50:49

Linux 字节对齐Linux 系统

2018-04-11 08:54:16

Linux ARM存储分布

2009-02-10 17:28:00

服务器租用服务器托管服务器
点赞
收藏

51CTO技术栈公众号