1. 程式人生 > >三、配置SSH免密登入

三、配置SSH免密登入

企業級大資料平臺Ambari搭建與管理

本節中我們將介紹Ambari叢集的SSH免密登入

1、安裝openssh-client客戶端:

此步驟中所有操作都使用“傳送鍵輸入到所有回話功能”進行配置:

安裝openssh-client客戶端

[[email protected] ~]# yum install -y openssh-client
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.cn99.com
 * extras: mirrors.zju.edu.cn
 * updates: mirrors.cn99.com
No package openssh-client available.
Error: Nothing to do

在這裡插入圖片描述 這裡我們發現open-ssh client已經隨系統安裝,如果沒有安裝,此命令會自動安裝。

2、配置時鐘同步:

此步驟中所有操作都使用“傳送鍵輸入到所有回話功能”進行配置:

配置時區

[[email protected] ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
cp: overwrite ‘/etc/localtime’?

會詢問是否覆蓋檔案,這裡回車確定即可。 在這裡插入圖片描述

安裝ntp服務:

[[email protected] ~]# yum install -y ntp
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.cn99.com
 * extras: mirrors.zju.edu.cn
 * updates: mirrors.cn99.com
Resolving Dependencies
--> Running transaction check
---> Package ntp.x86_64 0:4.2.6p5-28.el7.centos will be installed
--> Processing Dependency: ntpdate = 4.2.6p5-28.el7.centos for package: ntp-4.2.6p5-28.el7.centos.x86_64
--> Processing Dependency: libopts.so.25()(64bit) for package: ntp-4.2.6p5-28.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-28.el7.centos will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===================================================================================================================
 Package                       Arch                 Version                               Repository          Size
===================================================================================================================
Installing:
 ntp                           x86_64               4.2.6p5-28.el7.centos                 base               549 k
Installing for dependencies:
 autogen-libopts               x86_64               5.18-5.el7                            base                66 k
 ntpdate                       x86_64               4.2.6p5-28.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): ntpdate-4.2.6p5-28.el7.centos.x86_64.rpm                                             |  86 kB  00:00:00     
(3/3): ntp-4.2.6p5-28.el7.centos.x86_64.rpm                                                 | 549 kB  00:00:00     
-------------------------------------------------------------------------------------------------------------------
Total                                                                              1.1 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-28.el7.centos.x86_64                                                            2/3 
  Installing : ntp-4.2.6p5-28.el7.centos.x86_64                                                                3/3 
  Verifying  : ntpdate-4.2.6p5-28.el7.centos.x86_64                                                            1/3 
  Verifying  : autogen-libopts-5.18-5.el7.x86_64                                                               2/3 
  Verifying  : ntp-4.2.6p5-28.el7.centos.x86_64                                                                3/3 

Installed:
  ntp.x86_64 0:4.2.6p5-28.el7.centos                                                                               

Dependency Installed:
  autogen-libopts.x86_64 0:5.18-5.el7                    ntpdate.x86_64 0:4.2.6p5-28.el7.centos                   

Complete!

在這裡插入圖片描述

將ntp伺服器指向阿里雲的ntp伺服器:

[[email protected] ~]# ntpdate ntp1.aliyun.com
 1 Oct 01:34:56 ntpdate[1242]: adjust time server 120.25.115.20 offset -0.002358 sec

在這裡插入圖片描述

檢查本地時間:

[[email protected] ~]# date
Mon Oct  1 01:36:37 EDT 2018

在這裡插入圖片描述

3、新建Hadoop使用者並配置sudo許可權

此步驟中所有操作都使用“傳送鍵輸入到所有回話功能”進行配置:

新建Hadoop使用者並設定密碼:

[[email protected]
~]# useradd -m hadoop [[email protected] ~]# passwd hadoop Changing password for user hadoop. New password: BAD PASSWORD: The password is a palindrome Retype new password: passwd: all authentication tokens updated successfully.

在這裡插入圖片描述

為Hadoop使用者設定sudo許可權:

[[email protected] ~]# visudo
末尾新增:
hadoop ALL=(ALL) NOPASSWD:ALL

在這裡插入圖片描述 在這裡插入圖片描述

切換到Hadoop使用者並測試Hadoop使用者的sudo許可權:

[[email protected] ~]# su hadoop
[[email protected] root]$ sudo visudo

在這裡插入圖片描述 能不要密碼開啟就證明Hadoop使用者的sudo許可權配置成功。

4、配置ssh免密登入:

此步驟中所有操作都使用“傳送鍵輸入到所有回話功能”進行配置:

首先執行ssh localhost一次:

[[email protected] root]$ ssh localhost
The authenticity of host 'localhost (::1)' can't be established.
ECDSA key fingerprint is SHA256:PhYAJ1EUOafFIL3VfE1Ooh/dbbSTglNV9Hx9dzyQ2js.
ECDSA key fingerprint is MD5:32:36:59:ff:b8:93:b5:88:1b:1b:16:4e:b5:69:e2:e4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
[email protected]'s password: 
Last failed login: Mon Oct  1 02:09:58 EDT 2018 from localhost on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Mon Oct  1 02:02:19 2018

在這裡插入圖片描述

退出ssh

[[email protected] ~]$ exit
logout
Connection to localhost closed.

在這裡插入圖片描述

獲取ssh祕鑰:

一直按回車即可。

[[email protected] root]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/hadoop/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/hadoop/.ssh/id_rsa.
Your public key has been saved in /home/hadoop/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:wsBBEd6heLo5ww9NgiI9575LnVS3y/1HYPLK2BahxNc [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|   .=o.          |
|   + + .         |
|  . * . ...  .   |
| o o o . .o.+ E  |
|+ = o + S..o = . |
|o. X o o ..o. . .|
|  B + o   o+.o . |
|   B      . =.  .|
|    =o     .  .. |
+----[SHA256]-----+

在這裡插入圖片描述

將每個節點上的密碼新增到Hadoop01節點上的authorized_keys 上

[[email protected] root]$ cat ~/.ssh/id_rsa.pub | ssh [email protected] 'cat >> ~/.ssh/authorized_keys'
The authenticity of host 'hadoop01 (192.168.19.105)' can't be established.
ECDSA key fingerprint is SHA256:PhYAJ1EUOafFIL3VfE1Ooh/dbbSTglNV9Hx9dzyQ2js.
ECDSA key fingerprint is MD5:32:36:59:ff:b8:93:b5:88:1b:1b:16:4e:b5:69:e2:e4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'hadoop01,192.168.19.105' (ECDSA) to the list of known hosts.
[email protected]'s password:

在這裡插入圖片描述

檢查Hadoop01節點上的authorized_keys:(此步驟只在Hadoop01上執行)

[[email protected] root]$ cd ~/.ssh/
[[email protected] .ssh]$ cat authorized_keys 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDChMrwmNmbhg6PUC5M3eqhuoE6EtuQ
LGHXXY9B+8tSTGyD1Ic1YaBNHfBVvXVxdxFpZ9E6u9ARIVUpDcH2WeTkBN5uSse
Dg8UovtDptslZ88B2w/Qj9WD5h0atqCGVRQR8zN3xKo6+AQcrpgRe7V+ZDlYLmcQK
Y3lmrACyiIgU1EKr3yAO3SClP9qHVaWz/pdGomF9HpvdUKcjAm82y+tqUQ7JaQLP2
maNRpHpR/qqOsr2P2xI3hQkHbbiH9UZsMkUbSo7qOuLKKyZq6qRt1zkDMzTP9CSd
auSlycUT4wFWLQyLg9ZIgavS9ZTCrZ785m8RSTpZPsIOhZoIc5JlIp9 [email protected]
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC3/Y5URlQPq817/j9e60Td4fnnEseSB6lA
WiK7Pl9gsGEBvvhnqe+F0ni7rAmg4Xh1HNjFhSi3sczmaxGUj4srOvQ2ufGXdTwzO+B
fPUw4dtZO7CzSw83LWgVlIRn3HLygiB13/j2WezDbbx32xrVYAetXTYrMxj4jSbnTz1L+
puu6QsTlFjaeghVKQtNaFLbIWT0gyxv3gUFlbdA6mlythZ7GsZYoviIqVDsVvCsNycJHd
GpadPL3Sn5QdB7INu3JmHJCu6KAbem/eMfNku6uUvAD8P7MkFpGWbth3SqbEuc/4
8RSxsXasJxzxPq6QlxZuPEXb6x7bhiJxKcjPlLD [email protected]

在這裡插入圖片描述 此處應該有每個節點傳過來的Hadoop使用者的ssh祕鑰資訊,總共10個。

將Hadoop01節點上的authorized_keys通過遠端命令傳到其他節點上:(此步驟只在Hadoop01上執行)

將authorized_keys傳到Hadoop02節點上:

[[email protected] .ssh]$ scp -r authorized_keys [email protected]:~/.ssh
The authenticity of host 'hadoop02 (192.168.19.106)' can't be established.
ECDSA key fingerprint is SHA256:LhbwpdK9glCeSAS+xXwOSrNOKDeD4fjvMN7PIzgdAnI.
ECDSA key fingerprint is MD5:93:22:34:9d:4d:87:69:8f:17:c9:ad:69:04:e1:9c:e7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'hadoop02,192.168.19.106' (ECDSA) to the list of known hosts.
[email protected]'s password: 
authorized_keys                                                                          100%  794   810.5KB/s   00:00

在這裡插入圖片描述 同樣執行命令將authorized_keys傳到其他節點上。

配置檔案的讀寫執行許可權:

[[email protected] .ssh]$ cd /home/hadoop
[[email protected] ~]$ chmod 700 .ssh
[[email protected] ~]$ chmod 600 .ssh/*

在這裡插入圖片描述

測試ssh免密登入:

[[email protected] ~]$ ssh hadoop01
Last login: Mon Oct  1 02:10:03 2018 from localhost
[[email protected] ~]$ exit
logout
Connection to hadoop01 closed.
[[email protected] ~]$ ssh hadoop02
Last login: Mon Oct  1 02:10:03 2018 from localhost
[[email protected] ~]$ exit
logout
Connection to hadoop02 closed.

在這裡插入圖片描述

到此,叢集節點之間的SSH免密登入就配置完成了。