2015/09/26

設定 ssh 使用 key 認證方式登入 linux 系統


目的為設定從 hnamenode 登入 hnamenode2 無須使用密碼登入

# ssh key 認證, 還沒產生 key 前的畫面
[hadoop@hnamenode ~]$ ls .ssh/
known_hosts

# 產生一對 ssh key
[hadoop@hnamenode ~]$ ssh-keygen 
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): (這是key的密碼)
Enter same passphrase again: (這是key的密碼,check 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:
cf:d3:df:a5:cb:ae:75:b9:09:59:51:e8:c2:ff:45:33 hadoop@hnamenode2
The key's randomart image is:
+--[ RSA 2048]----+
|               ..|
|              . .|
|           . . . |
|            o .Eo|
|        S    o oo|
|         o .  + o|
|          + .o.o+|
|           . +.+=|
|            .o*+.|
+-----------------+

# key 產生了, id_rsa 為私鑰, id_rsa.pub 為公鑰
[hadoop@hnamenode ~]$ ls .ssh/
-rw-------.  1 hadoop hadoop 1679  9月 26 12:38 id_rsa
-rw-r--r--.  1 hadoop hadoop  399  9月 26 12:38 id_rsa.pub
-rw-r--r--.  1 hadoop hadoop  530  9月 26 09:57 known_hosts

# 將 hnamenode key 複製到遠端 hnamenode2 的 .ssh/ 目錄底下 authorized_keys 檔案內
[hadoop@hnamenode Documents]$ ssh-copy-id hadoop@192.168.1.250
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
hadoop@192.168.1.250's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'hadoop@192.168.1.250'"
and check to make sure that only the key(s) you wanted were added.

# 驗證,無須密碼就可以遠端下指令
[hadoop@hnamenode Documents]$ ssh hadoop@192.168.1.250 whoami
hadoop

# 再 hnamenode2 就會看到 authorized_keys 檔案,從放有 hnamenode 的 id_rsa.pub 內容
[hadoop@hnamenode2 ~]$ ls -la .ssh/
-rw-------.  1 hadoop hadoop  391  9月 26 12:40 authorized_keys
-rw-r--r--.  1 hadoop hadoop  530  9月 26 09:57 known_hosts

張貼留言