ubuntu設置 SSH 經過密鑰對登陸

1. 製做密鑰對php

首先在服務器上製做密鑰對。登陸到打算使用密鑰登陸的帳戶,而後執行如下命令:shell

[root@host ~]$ ssh-keygen  <== 創建密鑰對
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): <== 按 Enter
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): <== 輸入密鑰鎖碼,或直接按 Enter 留空
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. <== 公鑰

密鑰鎖碼在使用私鑰時必須輸入,這樣就能夠保護私鑰不被盜用。固然,也能夠留空,實現無密碼登陸。服務器

如今,在 root 用戶的家目錄中生成了一個 .ssh 的隱藏目錄,內含兩個密鑰文件。id_rsa 爲私鑰,id_rsa.pub 爲公鑰。ssh

2. 在服務器上安裝公鑰ide

鍵入如下命令,在服務器上安裝公鑰:工具

[root@host .ssh]$ chmod 600 authorized_keys
[root@host .ssh]$ chmod 700 ~/.ssh

3. 設置 SSH,打開密鑰登陸功能spa

編輯 /etc/ssh/sshd_config 文件,進行以下設置:.net

RSAAuthentication yes
PubkeyAuthentication yes

另外,請留意 root 用戶可否經過 SSH 登陸:rest

PermitRootLogin yes

當你完成所有設置,並以密鑰方式登陸成功後,再禁用密碼登陸:code

PasswordAuthentication no

最後,重啓 SSH 服務:

[root@host .ssh]$ service sshd restart

4. 將私鑰下載到客戶端,而後轉換爲 PuTTY 能使用的格式

使用 WinSCP、SFTP 等工具將私鑰文件 id_rsa 下載到客戶端機器上。而後打開 PuTTYGen,單擊 Actions 中的 Load 按鈕,載入你剛纔下載到的私鑰文件。若是你剛纔設置了密鑰鎖碼,這時則須要輸入。

載入成功後,PuTTYGen 會顯示密鑰相關的信息。在 Key comment 中鍵入對密鑰的說明信息,而後單擊 Save private key 按鈕便可將私鑰文件存放爲 PuTTY 能使用的格式。

從此,當你使用Xshell或 PuTTY 登陸時,能夠在左側的 Connection -> SSH -> Auth 中的 Private key file for authentication: 處選擇你的私鑰文件,而後便可登陸了,過程當中只需輸入密鑰鎖碼便可。

轉自:http://blog.csdn.net/permike/article/details/52386868

相關文章
相關標籤/搜索