Linux SSH無密碼登入

Federico發表於2017-05-26

Linux伺服器常見的登入方式有兩種:密碼登入、祕鑰登入。工作中我們最常使用的是用祕鑰登入的方法,因為使用祕鑰登入更高效、更安全。

如何實現SSH無密碼登入:

原理:無密碼ssh登入的主要操作為將本機中的ssh金鑰對中的公鑰如id_rsa.pub拷貝到目標機器的ssh驗證檔案authorized_keys中。

下面我將在docker中實現SSH無密碼登入的操作

1.首先執行一個centos容器作為跳板機使用

sudo docker run -it centos /bin/bash

2.安裝openssh及其相關軟體包,使用命令獲取公鑰與私鑰

yum -y install openssh*

[root@75940a29d36a ~]# ssh-keygen -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
8a:13:2b:96:45:78:fc:61:b6:c8:f5:67:50:3f:4a:19 root@75940a29d36a
The key's randomart image is:
+--[ RSA 2048]----+
| E |
| o . + |
| . + = . o o |
| + * + o . . |
| = o S + |
| o + . o |
| + + . |
| . . . |
| |
+-----------------+

從輸出資訊中我們可以看到,我們是以root身份進行登入,並在/root下建立了.ssh/目錄用於存放公鑰和私鑰,當然authorized_keys也要放到.ssh/目錄下

3.使用docker開啟第二個centos容器作為線上伺服器使用

sudo docker run -it centos /bin/bash

4.同樣我們安裝openssh及其相關的軟體包,並獲取一個金鑰對,但是需要注意的是我們的金鑰對並沒有實際的作用,我們獲取金鑰對主要是因為它可以在使用者的家目錄下自動建立.ssh/目錄,當然我們也可以手動建立。

yum -y install openssh*

[root@710a756dfa5b ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
20:07:a2:d9:33:bd:51:9f:87:f8:b8:e1:72:c8:6a:b2 root@710a756dfa5b
The key's randomart image is:
+--[ RSA 2048]----+
| . . . |
| + o o o o |
|o + + + + . |
| o = + . |
| . o S |
| . o o |
| + + |
|. .. o |
|E+. |
+-----------------+

5.回到我們的跳板機上將我們的公鑰傳送到線上伺服器的使用者家目錄的.ssh/目錄下

[root@75940a29d36a ~]# ssh-copy-id -i .ssh/id_rsa.pub root@172.18.0.3
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/bin/ssh-copy-id: ERROR: ssh: connect to host 172.18.0.3 port 22: Connection refused

[root@75940a29d36a ~]# ssh-copy-id -i .ssh/id_rsa.pub root@172.18.0.3
The authenticity of host '172.18.0.3 (172.18.0.3)' can't be established.
ECDSA key fingerprint is 76:f8:d5:28:0c:f6:cc:16:1f:58:be:26:23:3f:95:e6.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@172.18.0.3's password:

Number of key(s) added: 1

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

現在我們已經將公鑰傳送至線上伺服器的對應的使用者家目錄下的.ssh/目錄下,通過輸出資訊我們也可以看到,現在讓我們嘗試使用ssh root@172.18.0.3測試是否可以連線到線上伺服器

[root@75940a29d36a ~]# ssh 'root@172.18.0.3'
[root@710a756dfa5b ~]#

注意:使用ssh-copy-id命令等同與將本地的公鑰貼上到遠端伺服器的.ssh/目錄下的authorized_keys檔案中,只不過使用這種方式更快捷,降低了錯誤率。

 

我們現在發現連線沒有問題,也就是我們成功的設定了SSH無密碼登入!但是還有一個問題就是,ssh預設的埠號為22,但是實際工作中我們很少使用預設的埠號,所以我們就出現了新的問題,那就是如何ssh-copy-id非22埠,與直接使用ssh命令或scp命令指定埠有些不同:

方法一:執行命令時就直接指定

ssh-copy-id -i ~/.ssh/id_rsa.pub "-p 1111 root@172.18.0.3"

方法二:通過修改配置檔案定義埠

#vim ~/.ssh/config
加上內容:
Host server
Hostname ip
Port 1111

方法三:修改全域性配置檔案實現

vim /etc/ssh/ssh_config

Port 1111

失敗時需要注意的項:

1.實際工作中我們可能同一金鑰對多臺伺服器使用,這樣就需要我們通過拷貝金鑰對來實現,但是這樣就可能會出現新的問題,那就是許可權問題。

[root@710a756dfa5b .ssh]# ll -ah
total 24K
drwx------ 2 root root 4.0K May 26 03:28 .
dr-xr-x--- 3 root root 4.0K May 26 03:01 ..
-rw------- 1 root root 399 May 26 03:11 authorized_keys
-rw------- 1 root root 1.7K May 26 03:01 id_rsa
-rw-r--r-- 1 root root 399 May 26 03:01 id_rsa.pub
-rw-r--r-- 1 root root 172 May 26 03:28 known_hosts

我們需要注意的是公鑰的許可權為644,私鑰的許可權為600還需要注意對應的公鑰私鑰對應的屬主屬組。

2.當我們操作沒問題卻連線失敗時,很有可能是使用者名稱關係對應的問題。

例如:我們實際工作中線上伺服器很少使用root使用者,我們的跳板機是test使用者,但是我們新上線了一批伺服器,最開始的操作需要使用root使用者的許可權,要求我們在跳板機上使用test使用者通過ssh連線新上線的伺服器可以直接轉換為root使用者,這種情況我們就需要注意使用者名稱對應的問題,我們的公鑰與私鑰一定是在/home/test/.ssh/目錄下,遠端伺服器則是在/root/.ssh/下有寫有我們的公鑰的authorized_keys,並且連線時注意指定要登入的使用者名稱。

3.現在使用ansible自動化工具的使用者,一定要設定SSH祕鑰登入,並且最好使用相互對應的使用者名稱,會減少很多不必要的麻煩。

4.出現錯誤時不要慌張,本能的第一反應是檢視輸出內容,理清排錯思路,檢視服務程式是否啟用,埠號是否已經佔用。

 

結束語:本人新手,對技術時刻保持著敬畏之心,如有錯誤望有志之士告知,不勝感激!!!

 

 

相關文章