無論是個人的VPS還是企業允許公網訪問的服務器,如果開放22端口的SSH密碼登錄驗證方式,被眾多黑客暴力猜解捅破菊花也可能是經常發生的慘劇。企業可以通過防火牆來做限制,普通用戶也可能借助修改22端口和強化弱口令等方式防護,但目前相對安全和簡單的方案則是讓SSH使用密鑰登錄並禁止口令登錄。
這是最相對安全的登錄管理方式
建議設置並牢記passphrase密碼短語,以Linux生成為例
Linux:ssh-keygen -t rsa
[私鑰 (id_rsa) 與公鑰 (id_rsa.pub)]
Windows:SecurCRT/Xshell/PuTTY
[SSH-2 RSA 2048]
#生成SSH密鑰對
ssh-keygen -t rsa
Generatingpublic/private rsa key pair.
#建議直接回車使用默認路徑
Enter file in which to save the key (/root/.ssh/id_rsa):
#輸入密碼短語(留空則直接回車)
Enter passphrase (empty forno passphrase):
#重復密碼短語
Enter same passphrase again:
Your identification has been saved in/root/.ssh/id_rsa.
Yourpublic key has been saved in/root/.ssh/id_rsa.pub.
The key fingerprint is:
aa:8b:61:13:38:ad:b5:49:ca:51:45:b9:77:e1:97:e1 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
| .o. |
| .. . . |
| . . . o o |
| o. . . o E |
|o.= . S . |
|.*.+ . |
|o.* . |
| . + . |
| . o. |
+-----------------+
也可以手動在客戶端建立目錄和authorized_keys,注意修改權限
#復制公鑰到無密碼登錄的服務器上,22端口改變可以使用下面的命令
#ssh-copy-id -i ~/.ssh/id_rsa.pub "-p 10022 user@server"
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
#編輯sshd_config文件
vi /etc/ssh/sshd_config
#禁用密碼驗證
PasswordAuthenticationno
#啟用密鑰驗證
RSAAuthentication yes
PubkeyAuthentication yes
#指定公鑰數據庫文件
AuthorsizedKeysFile.ssh/authorized_keys
重啟SSH服務前建議多保留一個會話以防不測
#RHEL/CentOS系統
service sshd restart
#Ubuntu系統
service ssh restart
#debian系統
/etc/init.d/ssh restart
可以在== 後加入用戶注釋標識方便管理
echo 'ssh-rsa XXXX'>>/root/.ssh/authorized_keys
# 復查
cat /root/.ssh/authorized_keys
SSH服務遠程訪問Linux服務器登陸慢 http://www.linuxidc.com/Linux/2011-08/39742.htm
提高Ubuntu的SSH登陸認證速度的辦法 http://www.linuxidc.com/Linux/2014-09/106810.htm
開啟SSH服務讓Android手機遠程訪問 Ubuntu 14.04 http://www.linuxidc.com/Linux/2014-09/106809.htm
如何為Linux系統中的SSH添加雙重認證 http://www.linuxidc.com/Linux/2014-08/105998.htm
在 Linux 中為非 SSH 用戶配置 SFTP 環境 http://www.linuxidc.com/Linux/2014-08/105865.htm
Linux 上SSH 服務的配置和管理 http://www.linuxidc.com/Linux/2014-06/103627.htm
SSH入門學習基礎教程 http://www.linuxidc.com/Linux/2014-06/103008.htm
SSH免密碼登錄詳解 http://www.linuxidc.com/Linux/2015-03/114709.htm