您的位置:首页 > 运维架构 > Linux

linux ntp server

2016-05-17 15:25 447 查看
UTC:协调世界时(英:Coordinated Universal Time ,法:Temps Universel Coordonné),又称世界统一时间,世界标准时间,国际协调时间。英文(CUT)和法文(TUC)的缩写不同,作为妥协,简称UTC。协调世界时是以原子时秒长为基础,在时刻上尽量接近于世界时的一种时间计量系统。
NTP :network time protocol
软件时钟,1970/1/1 0:0:0 计算的总秒数
硬件时钟,bios时间

server主机的port : UDP 123 端口

选择多部time server 来做NTPserver,避免单台坏了,时间无法同步

NTP 的阶层概念, 当前主机向上层获取时间,向下提供时间

ntp
tzdata :time zone data 提供各个市区对应的显示格式
配置文件:

/etc/ntp.conf 主要的唯一的配置文件

/usr/share/zoneinfo/* 由tzdata提供,为各时区的时间格式对应挡

/etc/sysconfig/clock 设定时区,与是否使用UTC时间钟的配置文件

/etc/localtime 本地端的时间配置文件,会把clock中指定的时区 /usr/share/zoneinfo/
中的时间配置文件拷贝到/etc/localtime中

常用指令:

/bin/date 用于linux软件时间的修改与显示
/sbin/hwclock 用于bios时间的修改与显示,一般是用date修改系统时间后,再用hwclock同步到bios中
/usr/sbin/ntpdate 客户端时间校正工具
/usr/sbin/ntpd 提供NTP服务

实例:移居美国,如何修改时间

1 date 显示当前时间,注意看时区
2 vim /etc/sysconfig/clock
zone="/America/New_York" 修改时区到纽约
3 cp /usr/share/zoneinfo/America/New_York /etc/localtime 复制纽约的时间格式对应当 到本地时间配置文件中
4 date 查看时区

/etc/ntp.conf

restrict [你的IP] mask [netmask_ip] [parameter]

parameter:
ignore 拒绝所有
nomodify 客户端可以连接校正时间,但是不能用ntpc,ntpq修改时间服务器的参数
noquery 不提供网络校时
notrap 不提供trap这个远程事件登陆的功能
notrust 拒绝没有认证的客户端

特别注意: 如果没有加参数,表示没有设置任何限制

server [ip or hostname] [prefer]
设置上层ntp服务器,prefer 表示优先使用

driftfile [可以被ntpd写入的目录与档案]

该文件 需要 使用完整的路径文件名
不能是链接当
ntpd 这个daemon要有写入权限
该文件所记录的数值单位为:百万分之一秒

keys [keys_file]
客户端还可以通过密钥来认证

/etc/init.d/ntpd start
chkconfig ntpd on
tail /var/log/messages

netstat -tulnp | grep ntp

ntpstat

linux 手动校时的工作,date ,hwclock

date
date [option] [+format]
date [-u] MMDDhhmm[CC]YY.ss

hwclock [-rws]

网络校时

ntpdate [-dv] [NTP IP/hostname]

ntpdate ip地址

记得不要在ntpserver 上运行 ntpdate 命令

ntpdate 脚本如下:

#!/bin/bash
#
# chkconfig: - 57 75
# description: set the date and time via NTP

### BEGIN INIT INFO
# Provides: ntpdate
# Required-Start: $network $local_fs $remote_fs
# Should-Start: $syslog $named
# Short-Description: set the date and time via NTP
# Description: ntpdate sets the local clock by polling NTP servers
### END INIT INFO

# Source function library.
. /etc/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

prog=ntpdate
lockfile=/var/lock/subsys/$prog
ntpconf=/etc/ntp.conf
ntpstep=/etc/ntp/step-tickers

start() {
[ "$EUID" != "0" ] && exit 4
[ "$NETWORKING" = "no" ] && exit 1
[ -x /usr/sbin/ntpdate ] || exit 5
[ -f /etc/sysconfig/ntpdate ] || exit 6
. /etc/sysconfig/ntpdate

[ -f $ntpstep ] && tickers=$(sed 's/#.*//' $ntpstep) || tickers=

if ! echo "$tickers" | grep -qi '[a-z0-9]' && [ -f $ntpconf ]; then
# step-tickers doesn't specify a server,
# use servers from ntp.conf instead
tickers=$(awk '$1=="peer"||$1=="server"{print $2}' $ntpconf | \
egrep -v '127\.127\.[0-9]+\.[0-9]+')
fi

if ! echo "$tickers" | grep -qi '[a-z0-9]'; then
echo $"NTP server not specified in $ntpstep or $ntpconf"
exit 6
fi

echo -n $"$prog: Synchronizing with time server: "

[ -z "$RETRIES" ] && RETRIES=2
retry=0
while true; do
/usr/sbin/ntpdate $OPTIONS $tickers &> /dev/null
RETVAL=$?
[ $RETVAL -eq 0 ] || [ $retry -ge "$RETRIES" ] && break
sleep $[10 * (1 << $retry)]
retry=$[$retry + 1]
done

[ $RETVAL -eq 0 ] && success || failure
echo
if [ $RETVAL -eq 0 ]; then
touch $lockfile
[ "$SYNC_HWCLOCK" = "yes" ] && \
action $"Syncing hardware clock to system time" \
/sbin/hwclock --systohc
fi
return $RETVAL
}

status() {
[ -f $lockfile ] || return 3
}

stop() {
[ "$EUID" != "0" ] && exit 4
rm -f $lockfile
}

# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart|force-reload)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|status|restart|force-reload}"
exit 2
esac
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  NTP date hwclock