server端:能够访问外网的机器
client端:在内网里的机器
1、server端安装ntp校时包,修改独立主机
rm -rf /etc/localtime #先清除原有的连接文件
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #修改时区到东8区。
date -R #查看的时区设置。
接下来调整系统时间与时间服务器同步linux
Debian系统安装NTP校时包: 代码以下: apt-get install ntpdate #安装ntp CentOS系统安装NTP校时包: 代码以下: yum -y install ntpdate ntp #安装ntp Ubuntu系统安装NTP校时包: 代码以下: sudo apt-get install -y ntpdate ntp
2、server端修改/etc/ntp.conf配置文件
编辑 /etc/ntp.confwindows
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文件中添加以下的记录:centos
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 # 检查时间是否同步bash
3、server端修改ntpd 的系统配置
vi /etc/sysconfig/ntpd
SYNC_HWCLOCK=yes #同步独立主机的硬件时钟
而后:wq退出
chkconfig --levels 235 ntpd on #配置开机启动ntp服务,按期同步时间
/etc/init.d/ntpd start#启动ntp同步服务器
4、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 加入开机启动项rest
5、检查ntpd的工做状况(网络连通性(是否开放了123端口)和同步状况)
ntpq -p查看是否能正确输出
date -R 每台机器查看时间
linux下防火墙规则若是极严格的话可能会影响ntpd对时,打开 sport 123 便可
iptables -I INPUT -p udp -m udp --sport 123 -j ACCEPT
code
脚本待开发...server
waiting...
编辑/etc/ntp.conf文件
在里面加上
restrict 127.0.0.1 # 开启内部递归网络接口 lo
restrict 192.168.5.0 mask 255.255.255.0 nomodify #在内部子网里面的客户端能够 进行网络校时,但不能修改NTP服务器的时间参数
server 127.127.1.0 fudge 127.127.1.0 stratun 10
在以上的记录中:
指定的IP地址是一个“伪”IP地址,肯定本地系统为时间源。
指定的IP地址127.127.1.1告诉NTP使用内部时钟做为时间源。
"fudge"定义了这个时钟的级别,若是没有这个记录,节点就是一级服务器。将级别从新定义为10是个好的办法,这样客户端在查询这个服务器的时候就会知道这个服务器不是一个可靠的时间源.
这种时间同步的方式只应该在本地的网络不能使用外部的时间源的时候使用
service ntpd start
chkconfig ntpd on
* * * * * 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
1、安装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
2、修改/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 # 检查时间是否同步
3、修改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`