Centos 集群同步服务器时间

让 master, slave1, slave2 同步 宿主机(192.168.137.100) 的时间

宿主机: CENTOS7

master, slave1, slave2: Centos8

时间服务器配置(必须root用户)

(1)在所有节点关闭ntp服务和自启动

systemctl stop ntpd
systemctl disable ntpd

(2)修改ntp配置文件

vim /etc/ntp.conf

修改内容如下

a)修改1(授权192.168.1.0-192.168.1.255网段上的所有机器可以从这台机器上查询和同步时间)

#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
#为 去掉注释
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

b)修改2(集群在局域网中,不使用其他互联网上的时间)

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.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

c)添加3(当该节点丢失网络连接,依然可以采用本地时间作为时间服务器为集群中的其他节点提供时间同步)

server 127.127.1.0
fudge 127.127.1.0 stratum 10

(3)修改/etc/sysconfig/ntpd 文件

vim /etc/sysconfig/ntpd

增加内容如下(让硬件时间与系统时间一起同步)

SYNC_HWCLOCK=yes

(4)重新启动ntpd服务,及开机启动

systemctl start ntpd
systemctl enable ntpd

其他机器配置(必须root用户)

(1)在其他机器配置10分钟与时间服务器同步一次

(1)编写定时任务如下:

]# crontab -e
# 输入如下定时策略
*/10 * * * * /usr/sbin/ntpdate hadoop102

(2)修改任意机器时间

date -s "2017-9-11 11:11:11"

(3)十分钟后查看机器是否与时间服务器同步

date "+%F %T"

说明:测试的时候可以将10分钟调整为1分钟,节省时间。

==========================================

相关命令:

1. 宿主机:

[root@192 ~]# rpm -q ntp

未安装软件包 ntp

[root@192 ~]# ping www.baidu.com

PING www.a.shifen.com (180.101.49.11) 56(84) bytes of data.

64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=1 ttl=50 time=67.5 ms

[root@192 ~]# yum -y install ntp

已加载插件:fastestmirror

[root@192 ~]# ps -ef | grep ntp

root 10626 10415 0 09:34 pts/4 00:00:00 grep –color=auto ntp

[root@192 ~]# vi /etc/ntp.conf

# 根据上文配置

[root@192 ~]# systemctl status ntpd

● ntpd.service – Network Time Service

Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)

Active: inactive (dead)

[root@192 ~]# systemctl start ntpd

[root@192 ~]# systemctl enable ntpd

Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

[root@192 ~]# date “+%F %T”

[root@192 ~]# systemctl status crond

● crond.service – Command Scheduler

Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)

Active: active (running) since 日 2022-01-16 17:12:11 CST; 17h ago

Main PID: 661 (crond)

[root@192 ~]# ntpdate -q localhost

server ::1, stratum 11, offset 0.000006, delay 0.02565

server 127.0.0.1, stratum 11, offset 0.000008, delay 0.02567

17 Jan 13:45:23 ntpdate[16785]: adjust time server ::1 offset 0.000006 sec

================================================

2. master, slave1, slave2 配置;

Centos8

首先安装 ntp

[root@master /]# yum -y install ntp

Failed to set locale, defaulting to C.UTF-8

Last metadata expiration check: 1:12:28 ago on Mon Jan 17 03:58:58 2022.

No match for argument: ntp

Error: Unable to find a match: ntp

[root@master /]# rpm -q centos-replease

package centos-replease is not installed

[root@master /]# rpm -q centos-release 

package centos-release is not installed

[root@master /]# lsb-release

bash: lsb-release: command not found

[root@master /]# uname -a

Linux master 3.10.0-1160.36.2.el7.x86_64 #1 SMP Wed Jul 21 11:57:15 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

[root@master /]# uname

Linux:

[root@master /]# cat /etc/redhat-release

CentOS Linux release 8.4.2105

安装 ntp 源

[root@master /]# rpm -ivh http://mirrors.wlnmp.com/centos/wlnmp-release-centos.noarch.rpm
...
[root@master /]# yum install wntp

Failed to set locale, defaulting to C.UTF-8

wlnmp 449 kB/s | 2.2 MB 00:05

Last metadata expiration check: 0:00:04 ago on Mon Jan 17 05:20:08 2022.

[root@master /]# ntpdate

usage: ntpdate [-46bBdqsuv] [-a key#] [-e delay] [-k file] [-p samples] [-o version#] [-t timeo] server …

[root@master /]# whereis ntpdate

ntpdate: /usr/local/bin/ntpdate

[root@master /]# ln -s
...
[root@master sbin]# ln -s /usr/local/bin/ntpdate /usr/sbin/ntpdate
...

[root@master sbin]# date "+%F %T"
2022-01-17 05:26:57
...
[root@master sbin]# tail -f /var/log/cron.log
tail: cannot open '/var/log/cron.log' for reading: No such file or directory
tail: no files remaining

[root@master sbin]# crontab -l
*/10 * * * * /usr/sbin/ntpdate 192.168.137.100

[root@master log]# systemctl status  crond 

crond.service – Command Scheduler

Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)

Active: active (running) s…

Jan 17 05:40:01 master crond[301]: pam_systemd(crond:session): Failed to create session: Unit systemd-logind.service is masked.

Jan 17 05:40:01 master CROND[302]: (root) CMD (/usr/sbin/ntpdate 192.168.137.100)

[root@master log]# systemctl start systemd-logind

Failed to start systemd-logind.service: Unit systemd-logind.service is masked.

[root@master log]# systemctl unmask systemd-logind

Removed /etc/systemd/system/systemd-logind.service.

[root@master log]# systemctl status systemd-logind

systemd-logind.service – Login Service

Loaded: loaded (/usr/lib/systemd/system/systemd-logind.service; static; vendor preset: disabled)

Active: active (running) since….

[root@master log]# date "+%F %T"
2022-01-17 06:00:21
[root@master log]# /usr/sbin/ntpdate 192.168.137.100
17 Jan 06:00:53 ntpdate[347]: adjust time server 192.168.137.100 offset +0.000006 sec

[root@master log]# date "+%F %T"
2022-01-17 06:01:08

# 查看时间 中国 +0800, 美国 -0800

[root@master log]# date -R
Mon, 17 Jan 2022 06:02:24 +0000(#)
[root@master log]# cat /etc/sysconfig/clock
cat: /etc/sysconfig/clock: No such file or directory
[root@master log]# systemctl status crond

创建定时任务 (crontabs)

# centos8
yum install cronie

# centos7
[root@CentOS ~]# yum install vixie-cron
[root@CentOS ~]# yum install crontabs

# 然后
systemctl start crond

# 查看配置的 crontab
crontab -l

添加任务

]# crontab -e
*/30 * * * * /usr/sbin/ntpdate 210.72.145.44
# 30 分种执行一次同步

[问题]: No match for argument: vixie-cron

centos8 已经改为 cronie

欢迎您的到来,感谢您的支持!

为您推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注