1. 程式人生 > >使用SSH公鑰配置Linux免密登入

使用SSH公鑰配置Linux免密登入

使用SSH公鑰配置Linux免密登入

我們平時都會使用ssh進行遠端登入 ssh [email protected],然後輸入密碼既可以登入成功。但是每次ssh登入,scp遠端複製等,都輸入密碼卻相當麻煩。為了解決這個問題,我們可以使用ssh的公鑰,配置免密登入。配置過程主要包括以下幾步:

  • step 1 使用ssh-keygen 建立公鑰
ssh-keygen -t rsa 

按回車以後可以得到類似以下輸出:

Output
Generating public/private rsa key pair.
Enter file in which to save the key (/home/demo/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/demo/.ssh/id_rsa.
Your public key has been saved in /home/demo/.ssh/id_rsa.pub.
The key fingerprint is:
4a:dd:0a:c6:35:4e:3f:ed:27:38:8c:74:44:4d:93:67 
[email protected]
The key's randomart image is: +--[ RSA 2048]----+ | .oo. | | . o.E | | + . o | | . = = . | | = S = . | | o + = + | | . o + o . | | . o | | | +-----------------+
  • step 2 將上述產生的公鑰複製到目標伺服器上
ssh-copy-id [email protected]

或者使用scp 複製到遠端伺服器.ssh/authorized_keys 下

scp id_rsa.pub [email protected]:/home/username
cat id_rsa.pub >>.ssh/authorized_keys

cat ~/.ssh/id_rsa.pub | ssh [email protected] "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >>  ~/.ssh/authorized_keys"

以上步驟就已經完成配置。