CentOS时间同步方法和更新时间脚本
Linux
#centos #时间同步 #脚本2012-04-24 09:28
在CentOS 5不再区分客户端和服务器,只要配置了NTP,它就会提供NTP服务。
1)确认已经ntp程序包:
# yum install ntp
2)配置时间源
# vi /etc/ntp.conf
server pool.ntp.org #可选择其他优先服务器
server 0.pool.ntp.org
server 1.pool.ntp.org
server 2.pool.ntp.org
3)配置是否为其他PC提供时间服务
# vi /etc/ntp.conf
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
4)配置开机时自动运行时间服务
# chkconfig ntpd on
5)启动或停止时间服务
# service ntpd start
# service ntpd stop
# service ntpd restart
6)验证ntp服务已经运行
# pgrep ntpd
7)初始同步
# ntpdate -u time.lib.tsinghua.edu.cn
8)确认同步成功
# ntpq -p
9)同时建议修改一下 /etc/sysconfig/ntpd 文件
# vi /etc/sysconfig/ntpd
SYNC_HWCLOCK=yes
# 改成 yes 吧!这样主机 BIOS 的时间也会跟着一起改变的 !
当然,如果要提供时间服务,还必须相应的设置iptable防火墙的配置。CentOS的时间服务使用udp 123端口。
#! /bin/bash
PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin
export PATH
create()
{
cat >/usr/local/sbin/ntp.txt<
210.72.145.44
clock.via.net
time.windows.com
time-a.nist.gov
time-b.nist.gov
time-nw.nist.gov
time.stdtime.gov.tw
ntp.nasa.gov
EOF
}
if [ ! -f /usr/local/sbin/ntp.txt ];
create
fi
LOG=/var/log/jbmon/secure/service.txt
for svr in `cat /usr/local/sbin/ntp.txt`; do
/usr/sbin/ntpdate $svr >> $LOG
if [ $? = 0 ]; then exit; fi
done
相关文章