NTP设置集群时间同步

1、网上同步时钟(推荐)
检查ntp是否安装

rpm -q ntp

2、若没安装则安装ntp服务:

yum install -y ntp

3、修改时间服务器的配置文件/etc/ntp.conf
选择集群内一台机器作为时间服务器,其他机器从该时间服务器更新时间,同时时间服务器向外网时间服务器同步时间。

在第一台节点上配置:

sudo vim /etc/ntp.conf
#ntp.conf
#释放注释,允许192.168.1.0-192.168.1.255网段的机器同步此服务器
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
#注释掉默认的外网时间服务器
# 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
# 添加中国国家授时中心服务器地址
server 0.cn.pool.ntp.org
server 1.cn.pool.ntp.org
server 2.cn.pool.ntp.org
server 3.cn.pool.ntp.org

# 添加允许上层时间服务器主动修改本机时间
restrict 0.cn.pool.ntp.org nomodify notrap noquery
restrict 1.cn.pool.ntp.org nomodify notrap noquery
restrict 2.cn.pool.ntp.org nomodify notrap noquery
restrict 3.cn.pool.ntp.org nomodify notrap noquery
#释放注释 外部时间服务器不可用时,以本地时间作为时间服务
server 127.127.1.0 #local clock
fudge 127.127.1.0 stratum 10
#把系统时间写入主板,这样,即使服务器关机或断电,时间也会更新
SYNC_HWCLOCK=yes

在启动ntpd服务之前,修改时间服务器时间

sudo ntpdate cn.pool.ntp.org

启动时间服务器的ntpd服务

sudo service ntpd start

4、配置集群内其他机器向时间服务器同步时间

修改配置文件

sudo vim /etc/ntp.conf
# 默认的服务器列表注释掉
# 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

# 从node01中同步时间
server 时间服务器主机名或ip

# 允许时间服务器修改本地时间
restrict 时间服务器主机名或ip nomodify notrap noquery

# 如果时间服务器不可用,用本地的时间服务
server 127.127.1.0
fudge 127.127.1.0 stratum 10

# 同步时间后写到硬件中
SYNC_HWCLOCK=yes

每台机器在启动ntpd服务之前,手动同步时间

#root用户下
ntpdate -u 时间服务器主机名或ip

启动每台机器的ntpd服务

sudo service ntpd start

5、定时同步主服务器时间
三台服务器都要操作

#root用户下
crontab -e
#每隔十分钟从 时间服务器同步一次
*/10 * * * *  /usr/sbin/ntpdate 时间服务器主机名

6、所有需要同步的机器(包括选择的时间服务器)设置ntp开机自启动,但不需要启动服务

$ sudo chkconfig ntpd on

7、查看每台机器的状态

ntpq -p
或者
ntpstat

以上内容仅供参考学习,如有侵权请联系我删除!
如果这篇文章对您有帮助,左下角的大拇指就是对博主最大的鼓励。
您的鼓励就是博主最大的动力!

猜你喜欢

转载自blog.csdn.net/weixin_45366499/article/details/113792554