知识
助手
最大化  清空记录  历史记录
2018-01-03 21:18:01 版本 : Linux时间同步(内外网/内网/外网)
作者: 系统管理员 于 2017年12月06日 发布在分类 / 企业知识管理 / 权限演示 / 审核权限 / IT知识 下,并于 2018年01月03日 编辑
 历史版本

修改日期 修改人 备注
2018-01-13 21:25:29[当前版本] 系统管理员 更新标签
2018-01-03 21:18:01 系统管理员 修改預覽圖
2017-12-06 12:06:11 系统管理员 11
2017-12-06 11:59:36 系统管理员 1
搞笑程度:
来源:


内外网集群的时间同步①

server端:可以访问外网的机器
client端:在内网里的机器


package com.wcp.web.test;

public class PathClass {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(PathClass.class.getResource("/").getFile());
	}

}


一、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 就会看到以下内容:

这是默认的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`


历史版本-目录  [回到顶端]
    wcp知识分享平台 -V 5.1.3 -扁舟科技