ssh無密碼登入的設定步驟
首先我們在自己的linux系統上生成一對ssh key:ssh密鑰和ssh公鑰.密鑰儲存在自己的linux系統上。
然後公鑰上傳到linux伺服器.之後我們就能無密碼ssh登入了.ssh密鑰就好比是你的身份證明.
1在自己的linux系統上生成ssh密鑰和公鑰
打開終端,使用下面的ssh-keygen來生成rsa密鑰和公鑰.-t表示type,就是說要生成rsa加密的鑰匙.
ssh-keygen -t rsa
rsa也是預設的加密類型.是以你也可以隻輸入ssh-keygen.預設的rsa長度是2048位.如果你非常注重安全,那麼可以指定4096位的長度.
ssh-keygen -b 4096 -t rsa
生成ssh key的過程中會要求你指定一個檔案來儲存密鑰,按enter鍵使用預設的檔案就行了.然後需要輸入一個密碼來加密你的ssh key.密碼至少要20位長度.ssh密鑰會儲存在home目錄下的.ssh/id_rsa檔案中.ssh公鑰儲存在.ssh/id_rsa.pub檔案中.
generating public/private rsa key pair.
enter file in which to save the key (/home/matrix/.ssh/id_rsa): 按enter鍵
enter passphrase (empty for no passphrase): 輸入一個密碼
enter same passphrase again: 再次輸入密碼
your identification has been saved in /home/matrix/.ssh/id_rsa.
your public key has been saved in /home/matrix/.ssh/id_rsa.pub.
the key fingerprint is:
e1:dc:ab:ae:b6:19:b0:19:74:d5:fe:57:3f:32:b4:d0 matrix@vivid
the key's randomart image is:
+---[rsa 4096]----+
| .. |
| . . |
| . . .. . |
| . . o o.. e .|
| o s ..o ...|
| = ..+...|
| o . . .o .|
| .o . |
檢視.ssh/id_rsa檔案就會看到,這個檔案是經過加密的(encrypted).也就是用你輸入的密碼來加密.
less .ssh/id_rsa
ssh無密碼登入
2将ssh公鑰上傳到linux伺服器
可以使用ssh-copy-id指令來完成.
ssh-copy-id username@remote-server
輸入遠端使用者的密碼後,ssh公鑰就會自動上傳了.ssh公鑰儲存在遠端linux伺服器的.ssh/authorized_keys檔案中.
上傳完成後,ssh登入就不需要再次輸入密碼了.但是首次使用ssh key登入時需要輸入一次ssh密鑰的加密密碼.(隻需要輸入一次,将來會自動登入,不再需要輸入密鑰的密碼.)
使用scp指令來傳送檔案時也不需要輸入密碼.