首先輸入指令來產生 key
ssh-keygen
接著會詢問產生的ssh key要存放的位置
這時選擇要放的位置 預設為 /root/.ssh/id_rsa
如果不同伺服器會使用不同的key 則改變key的位置 (例如: /root/.ssh/id_rsa_cantos)
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
接著會詢問要不要設定 passphrase,空白表示不設定
如果有設定passphrase,則每次用此key登入都需要輸入這個密碼 (安全性較高,可依個人需求設定)
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
接下來就會產生key到指定位置了
會有兩個key 一個有副檔名 .pub 為公鑰,而沒有副檔名的為私鑰
注意私鑰要保存好 絕對不能被竊取
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa
The key fingerprint is:
透過 ssh-copy-id
輸入指令以下指令
(將 username 與 server_host 換成自己的帳號與伺服器IP或host,而 your_key_path 換成自己的剛剛產生的key位置)
ssh-copy-id -i /root/.ssh/id_rsa username@server_host
成功後會顯示如下的訊息
/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
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'username@server_host'"
and check to make sure that only the key(s) you wanted were added.
試試看是否能正常登入使用key登入
ssh username@server_host
如果能夠不需要輸入密碼 就表示已經成功了