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校时包:

代码如下:

apt-get install ntpdate #安装ntp 
  • 1.

CentOS系统安装NTP校时包:

代码如下: 

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

Ubuntu系统安装NTP校时包:

代码如下:

sudo apt-get install -y ntpdate ntp 
  • 1.

二、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服务器的时间参数  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

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

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

然后保存退出

接着输入下面的命令:

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

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

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

然后:wq退出

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

四、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  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

在里面加上

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 
  • 1.

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

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

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

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.
  • 2.
  • 3.

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

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

在以上的记录中:

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

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

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

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

启动ntp服务

service ntpd start 
  • 1.

设置开机启动

chkconfig ntpd on 
  • 1.

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

设置如下:

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

查看时间是否一致

***提及一点,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 >>   
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

更新脚本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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.

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

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

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

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

Debian系统安装NTP校时包:

代码如下:

apt-get install ntpdate #安装ntp 
  • 1.

CentOS系统安装NTP校时包:

代码如下:

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

Ubuntu系统安装NTP校时包:

代码如下:

sudo apt-get install -y ntpdate ntp 
  • 1.

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

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

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

这是默认的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  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

然后保存退出

接着输入下面的命令:

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

三、修改ntp 的系统配置

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

然后:wq退出

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

自动脚本如下:

#!/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`   
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
责任编辑:庞桂玉 来源: 运维派
相关推荐

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

服务器时钟服务数据库

2017-05-15 21:50:54

Linux引号

2015-09-16 13:19:46

javascript服务器

2010-08-06 14:46:51

思科路由器NTP服务器配置

2025-01-08 09:31:50

2009-11-11 10:18:02

2018-09-19 12:08:04

Linux服务器数据

2021-08-30 12:05:46

Linux字节对齐代码

2018-04-19 18:34:42

互联网

2010-09-02 10:50:17

时间同步服务器

2010-03-04 14:15:38

Ubuntu MySQ

2018-04-11 08:54:16

Linux ARM存储分布

2022-06-05 13:51:47

SentinelOpenFeign服务熔断

2024-03-18 00:00:05

Java服务JVM

2021-08-06 11:50:49

Linux 字节对齐Linux 系统

2010-08-20 10:29:40

NTP

2017-01-22 17:15:26

服务器
点赞
收藏

51CTO技术栈公众号