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

Linux使用ntpdate和ntpd进行时间同步

2017-03-06 09:54 856 查看
生产环境关联主机间常要求时间一致,若有NTP时间同步服务器,可配置各主机与时间同步服务器同步时间。

1.使用ntpdate进行时间同步

安装ntp客户端:

yum install ntpdate


同步时间:

ntpdate 192.168.160.2


配置定时同步任务,以root执行crontab -e,在调出的vi中输入以下内容:

0 1 * * *  /usr/sbin/ntpdate 192.168.160.2


说明:前边的五个值依次为分/时/日/月/星期,*表示任意。

2.使用ntpd进行时间同步
2.1 安装ntpd

yum install -y ntp


2.2 配置ntp.conf文件

cat > /etc/ntp.conf <<EOF
# 对任意IP进行限制,完全限制不提供同步服务
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
# 对本机进行限制,不做任何限制
restrict 127.0.0.1
restrict -6 ::1

# 对192.168进行配置限制,允许其以本机为时间同步服务器进行时间同步
restrict 192.168.0.0 mask 255.255.0.0 nomodify notrap
# 配置本机要进行同步的时间同步服务器,如果有多个则以带prefer的为主如果没有prefer那么从上往下为准
server 192.168.1.1
# 以下两句为没有上层时启用自身时间为准
#server 127.127.1.0
#fudge 127.127.1.0 stratum 10
# 上层服务与本机主板时钟之间时间差别记录在这个文件里
driftfile /var/lib/ntp/drift
keys /etc/ntp/keys
EOF


2.3 重启ntp服务

chkconfig ntpd on
service ntpd restart


3.ntpdate和ntpd进行时间同步的区别

ntpdate只能用来将本机时间与服务器进行同步,而ntpd还可以让别的机器将其作为时间同步服务器进行时间同步。

ntpdate是一下将本机时间修改成与服务器时间相一致,对采用了系统时间的程序而言是不友好的;ntpd是多次缓慢地对时间进行调整使本地时间与服务器时间相一致,是更推荐的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: