环境:
现在有三台centos7的虚拟机,分别是hadoop102,hadoop103,hadoop104。现在在hadoop102上配置ntp服务(中心服务器),hadoop103和104分别拉取hadoop102上的时间
1、查看ntp是否安装(3台都要查看和安装,并保证都不在运行,如果已经运行了,就停掉)
切换至root用户
如果ntp没有安装,就安装一下子
[root@hadoop104 ~]# rpm -qa|grep ntp
[root@hadoop104 ~]#
[root@hadoop104 ~]#
[root@hadoop104 ~]#
[root@hadoop104 ~]#
[root@hadoop104 ~]# yum -y install ntp
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.lzu.edu.cn
* extras: ap.stykers.moe
* updates: ap.stykers.moe
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package ntp.x86_64 0:4.2.6p5-29.el7.centos will be installed
--> Processing Dependency: ntpdate = 4.2.6p5-29.el7.centos for package: ntp-4.2.6p5-29.el7.centos.x86_64
--> Processing Dependency: libopts.so.25()(64bit) for package: ntp-4.2.6p5-29.el7.centos.x86_64
--> Running transaction check
---> Package autogen-libopts.x86_64 0:5.18-5.el7 will be installed
---> Package ntpdate.x86_64 0:4.2.6p5-29.el7.centos will be installed
--> Finished Dependency Resolution
Dependencies Resolved
===================================================================================================
Package Arch Version Repository Size
===================================================================================================
Installing:
ntp x86_64 4.2.6p5-29.el7.centos base 548 k
Installing for dependencies:
autogen-libopts x86_64 5.18-5.el7 base 66 k
ntpdate x86_64 4.2.6p5-29.el7.centos base 86 k
Transaction Summary
===================================================================================================
Install 1 Package (+2 Dependent packages)
Total download size: 701 k
Installed size: 1.6 M
Downloading packages:
(1/3): autogen-libopts-5.18-5.el7.x86_64.rpm | 66 kB 00:00:00
(2/3): ntp-4.2.6p5-29.el7.centos.x86_64.rpm | 548 kB 00:00:00
(3/3): ntpdate-4.2.6p5-29.el7.centos.x86_64.rpm | 86 kB 00:00:00
---------------------------------------------------------------------------------------------------
Total 1.6 MB/s | 701 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : autogen-libopts-5.18-5.el7.x86_64 1/3
Installing : ntpdate-4.2.6p5-29.el7.centos.x86_64 2/3
Installing : ntp-4.2.6p5-29.el7.centos.x86_64 3/3
Verifying : ntp-4.2.6p5-29.el7.centos.x86_64 1/3
Verifying : ntpdate-4.2.6p5-29.el7.centos.x86_64 2/3
Verifying : autogen-libopts-5.18-5.el7.x86_64 3/3
Installed:
ntp.x86_64 0:4.2.6p5-29.el7.centos
Dependency Installed:
autogen-libopts.x86_64 0:5.18-5.el7 ntpdate.x86_64 0:4.2.6p5-29.el7.centos
Complete!
[root@hadoop104 ~]#
有以下内容就可以了
[root@hadoop104 ~]#
[root@hadoop104 ~]#
[root@hadoop104 ~]# rpm -qa|grep ntp
ntp-4.2.6p5-29.el7.centos.x86_64
ntpdate-4.2.6p5-29.el7.centos.x86_64
[root@hadoop104 ~]#
[root@hadoop104 ~]#
2、配置Hadoop102
[root@hadoop103 ~]# vi /etc/ntp.conf
修改内容如下:
1、去掉以下注释,并修改为自己的这个网段
# Hosts on local network are less restricted.
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
2、注释掉向公网拿时间的
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#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
4、配置vi /etc/sysconfig/ntpd
[root@hadoop102 hadoop-2.7.7]#
[root@hadoop102 hadoop-2.7.7]# vi /etc/sysconfig/ntpd
[root@hadoop102 hadoop-2.7.7]#
添加一行:
SYNC_HWCLOCK=yes
5、重新启动ntpd服务
[root@hadoop102 hadoop-2.7.7]#
[root@hadoop102 hadoop-2.7.7]# systemctl start ntpd
[root@hadoop102 hadoop-2.7.7]#
[root@hadoop102 hadoop-2.7.7]# systemctl enable ntpd
查看是否启动成功:
[root@hadoop102 hadoop-2.7.7]#
[root@hadoop102 hadoop-2.7.7]# systemctl status ntpd
● ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2020-01-29 23:02:59 EST; 1min 22s ago
Main PID: 17717 (ntpd)
CGroup: /system.slice/ntpd.service
└─17717 /usr/sbin/ntpd -u ntp:ntp -g
Jan 29 23:02:59 hadoop102 ntpd[17717]: Listen and drop on 1 v6wildcard :: UDP 123
Jan 29 23:02:59 hadoop102 ntpd[17717]: Listen normally on 2 lo 127.0.0.1 UDP 123
Jan 29 23:02:59 hadoop102 ntpd[17717]: Listen normally on 3 ens33 192.168.1.102 ...23
Jan 29 23:02:59 hadoop102 ntpd[17717]: Listen normally on 4 lo ::1 UDP 123
Jan 29 23:02:59 hadoop102 ntpd[17717]: Listen normally on 5 ens33 fe80::e804:aa7...23
Jan 29 23:02:59 hadoop102 ntpd[17717]: Listening on routing socket on fd #22 for...es
Jan 29 23:02:59 hadoop102 ntpd[17717]: 0.0.0.0 c016 06 restart
Jan 29 23:02:59 hadoop102 ntpd[17717]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM
Jan 29 23:02:59 hadoop102 ntpd[17717]: 0.0.0.0 c011 01 freq_not_set
Jan 29 23:03:00 hadoop102 ntpd[17717]: 0.0.0.0 c514 04 freq_mode
Hint: Some lines were ellipsized, use -l to show in full.
[root@hadoop102 hadoop-2.7.7]#
[root@hadoop102 hadoop-2.7.7]#
其他机器(hadoop103和104都需要)配置(必须root用户)
配置定时任务,定时与时间服务器同步一次
[root@hadoop103 ~]# crontab -e
添加以下内容(每10分钟同步一次时间):
*/10 * * * * /usr/sbin/ntpdate hadoop102
保存完显示以下内容,表示成功了: