时间同步概述
- 原理:以集群中的一台服务器为时间服务器,其它机器定时进行同步
时间服务器
-
root用户或具有root权限
-
检查ntp是否安装:
rpm -qa|grep ntp
- 如果没有使用yum进行安装,
yum install -y ntp
- 如果没有使用yum进行安装,
-
修改ntp配置文件:
vi /etc/ntp.conf
-
修改1(授权192.168.0.0-192.168.0.255网段上的所有机器可以从这台机器上查询和同步时间)
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap为 restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap
-
修改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
-
添加3(当该节点丢失网络连接,依然可以采用本地时间作为时间服务器为集群中的其他节点提供时间同步)
server 127.127.1.0 fudge 127.127.1.0 stratum 10
-
-
修改/etc/sysconfig/ntpd 文件
-
vim /etc/sysconfig/ntpd 增加内容如下(让硬件时间与系统时间一起同步)
SYNC_HWCLOCK=yes
-
启动ntpd服务:
systemctl start ntpd
-
开机启动:
systemctl enable ntpd
集群时间同步
- 必须是root用户或又root权限
- 集群免密登录
- 集群host主机映射
- 安装ntp
- 在其他机器配置10分钟与时间服务器同步一次
-
crontab -e
-
编辑内容并保存运行
*/10 * * * * /usr/sbin/ntpdate server1
-