SSH證書登錄配置完整指南
本文檔詳細說明了如何配置Linux云服務(wù)器僅允許SSH證書登錄,禁止密碼登錄,以提高服務(wù)器安全性。
目錄
1. 前置條件
2. 第一步:生成SSH密鑰對
3. 第二步:將公鑰添加到云服務(wù)器
4. 第三步:修改SSH配置禁用密碼登錄
5. 第四步:重啟SSH服務(wù)并測試
6. 后續(xù)使用方法
7. 常用管理命令
8. 故障排除
? 已安裝OpenSSH客戶端(Windows 10/11默認(rèn)已安裝)
? 有PowerShell訪問權(quán)限
? 有服務(wù)器root權(quán)限或sudo權(quán)限
? 已安裝OpenSSH服務(wù)器
? 有root權(quán)限或sudo權(quán)限
? 當(dāng)前可以通過密碼登錄
在Windows PowerShell中執(zhí)行:
# 生成4096位RSA密鑰對
ssh-keygen -t rsa -b 4096 -f "$env:USERPROFILE\.ssh\id_rsa_server" -C "admin@server"參數(shù)說明:
?
-t rsa:使用RSA算法?
-b 4096:密鑰長度為4096位(更安全)?
-f:指定密鑰文件路徑?
-C:添加注釋(通常是郵箱)
執(zhí)行后會提示:
Enter passphrase (empty for no passphrase): 建議:
? 可以設(shè)置密碼短語(passphrase)以提高安全性
? 如果不需要,直接按Enter跳過
如果您已經(jīng)有SSH密鑰,可以跳過此步驟,直接使用現(xiàn)有密鑰。
生成的文件
生成后會創(chuàng)建兩個文件:
?私鑰:
C:\Users\YourUsername\.ssh\id_rsa_server?公鑰:
C:\Users\YourUsername\.ssh\id_rsa_server.pub
?? 重要提示:
? 私鑰文件必須妥善保管,不要泄露
? 公鑰文件可以公開,需要添加到服務(wù)器
ssh-copy-id -i ~/.ssh/id_rsa_server.pub root@服務(wù)器IP方法二:手動添加(Windows客戶端) 2.1 查看公鑰內(nèi)容Get-Content "$env:USERPROFILE\.ssh\id_rsa_server.pub"2.2 登錄到服務(wù)器$env:Path += ";$env:SystemRoot\System32\OpenSSH"
ssh root@服務(wù)器IP2.3 創(chuàng)建.ssh目錄(如果不存在)mkdir -p ~/.ssh
chmod 700 ~/.ssh2.4 添加公鑰到authorized_keysecho "您的公鑰內(nèi)容" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys2.5 驗證公鑰已添加cat ~/.ssh/authorized_keys方法三:使用PowerShell命令(推薦)# 一次性完成公鑰添加
$env:Path += ";$env:SystemRoot\System32\OpenSSH"
$publicKey = Get-Content "$env:USERPROFILE\.ssh\id_rsa_server.pub"
ssh root@服務(wù)器IP "mkdir -p ~/.ssh && chmod 700 ~/.ssh && echo '$publicKey' >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"測試證書登錄ssh -i "$env:USERPROFILE\.ssh\id_rsa_server" root@服務(wù)器IP如果可以成功登錄,說明公鑰配置正確!
第三步:修改SSH配置禁用密碼登錄 3.1 備份SSH配置文件
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak3.2 修改SSH配置文件 方法一:使用sed命令(推薦)方法二:使用vim編輯器# 禁用密碼登錄
sed -i 's/ yes/PasswordAuthentication no/g' /etc/ssh/sshd_config
sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config# 啟用公鑰認(rèn)證
echo 'PubkeyAuthentication yes' >> /etc/ssh/sshd_config
vim /etc/ssh/sshd_config找到并修改以下配置項:
# 禁用密碼登錄
PasswordAuthentication no
# 啟用公鑰認(rèn)證
PubkeyAuthentication yes# 確保以下配置(可選,提高安全性)
PermitRootLogin prohibit-password
保存并退出:
? 按
Esc鍵? 輸入
:wq? 按
Enter鍵
grep -E '(PasswordAuthentication|PubkeyAuthentication)' /etc/ssh/sshd_config預(yù)期輸出:
PasswordAuthentication no
PubkeyAuthentication yes第四步:重啟SSH服務(wù)并測試 4.1 重啟SSH服務(wù)systemctl restart sshd4.2 檢查SSH服務(wù)狀態(tài)systemctl status sshd預(yù)期輸出:
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since ...4.3 測試證書登錄保持當(dāng)前SSH連接不要斷開!(防止配置錯誤導(dǎo)致無法登錄)
打開新的終端窗口,測試證書登錄:
ssh -i "$env:USERPROFILE\.ssh\id_rsa_server" root@服務(wù)器IP如果可以成功登錄,說明配置正確!
4.4 測試密碼登錄(應(yīng)該失敗)
ssh -o StrictHostKeyChecking=no -o PreferredAuthentications=password root@服務(wù)器IP "echo '測試'"預(yù)期結(jié)果:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic)如果看到這個錯誤,說明密碼登錄已成功禁用!
后續(xù)使用方法 方法一:直接使用私鑰文件
$env:Path += ";$env:SystemRoot\System32\OpenSSH"
ssh -i "C:\Users\YourUsername\.ssh\id_rsa_server" -o StrictHostKeyChecking=no root@服務(wù)器IP方法二:配置SSH配置文件(推薦) 2.1 創(chuàng)建SSH配置文件在Windows上創(chuàng)建或編輯C:\Users\YourUsername\.ssh\config文件:
Host server-alias
HostName 服務(wù)器IP
User root
Port 22
IdentityFile C:\Users\YourUsername\.ssh\id_rsa_server
StrictHostKeyChecking no參數(shù)說明:
?
Host:服務(wù)器別名(自定義)?
HostName:服務(wù)器IP地址?
User:登錄用戶名?
Port:SSH端口(默認(rèn)22)?
IdentityFile:私鑰文件路徑?
StrictHostKeyChecking no:跳過主機密鑰確認(rèn)(可選)
ssh server-alias方法三:在PowerShell中設(shè)置別名編輯PowerShell配置文件$PROFILE:
notepad $PROFILE添加以下函數(shù):
function Connect-CloudServer {
$env:Path += ";$env:SystemRoot\System32\OpenSSH"
ssh -i "C:\Users\YourUsername\.ssh\id_rsa_server" -o StrictHostKeyChecking=no root@服務(wù)器IP
}# 設(shè)置別名
Set-Alias ccs Connect-CloudServer
使用時只需輸入:
ccs常用管理命令 查看SSH服務(wù)狀態(tài)systemctl status sshd查看SSH配置cat /etc/ssh/sshd_config查看已授權(quán)的公鑰cat ~/.ssh/authorized_keys重啟SSH服務(wù)systemctl restart sshd查看SSH登錄日志查看當(dāng)前登錄用戶# 查看最近的SSH登錄記錄
journalctl -u sshd -n 50# 實時監(jiān)控SSH登錄日志
journalctl -u sshd -f
who查看SSH連接數(shù)netstat -antp | grep sshd | wc -l故障排除 問題1:無法使用證書登錄可能原因:
? 私鑰文件路徑錯誤
? 私鑰文件權(quán)限不正確
? 公鑰未正確添加到服務(wù)器
解決方法:
1. 檢查私鑰文件路徑:
Test-Path "C:\Users\YourUsername\.ssh\id_rsa_server"2. 檢查私鑰文件權(quán)限(Linux):
chmod 600 ~/.ssh/id_rsa_server3. 重新添加公鑰:
cat ~/.ssh/authorized_keys問題2:密碼登錄仍然可用可能原因:
? SSH配置未正確修改
? SSH服務(wù)未重啟
解決方法:
1. 檢查SSH配置:
grep PasswordAuthentication /etc/ssh/sshd_config2. 確保配置為
no:
PasswordAuthentication no3. 重啟SSH服務(wù):
systemctl restart sshd問題3:配置后無法登錄服務(wù)器緊急恢復(fù)方法:
如果您在配置后無法登錄服務(wù)器,可以通過以下方式恢復(fù):
方法一:使用云服務(wù)商控制臺
1. 登錄云服務(wù)商控制臺(阿里云、騰訊云等)
2. 找到服務(wù)器的"VNC連接"或"遠程連接"功能
3. 通過控制臺登錄服務(wù)器
4. 恢復(fù)SSH配置:
cp /etc/ssh/sshd_config.bak /etc/ssh/sshd_config
systemctl restart sshd方法二:使用救援模式1. 通過云服務(wù)商進入救援模式
2. 掛載服務(wù)器磁盤
3. 修改SSH配置文件
4. 重啟服務(wù)器
可能原因:
? 防火墻阻止SSH連接
? SSH服務(wù)未運行
? 網(wǎng)絡(luò)問題
解決方法:
1. 檢查SSH服務(wù)狀態(tài):
systemctl status sshd2. 檢查防火墻:
# CentOS/RHEL
firewall-cmd --list-all# Ubuntu/Debian
ufw status
3. 檢查端口監(jiān)聽:
netstat -tlnp | grep 22問題5:私鑰文件損壞解決方法:
1. 檢查私鑰文件格式:
Get-Content "C:\Users\YourUsername\.ssh\id_rsa_server" | Select-Object -First 3預(yù)期輸出:
-----BEGIN OPENSSH PRIVATE KEY-----
或
-----BEGIN RSA PRIVATE KEY-----2. 如果格式不正確,重新生成密鑰對
在生成密鑰時設(shè)置密碼短語,即使私鑰泄露,攻擊者也需要密碼才能使用。
2. 定期更換SSH密鑰
建議每6-12個月更換一次SSH密鑰。
3. 限制SSH訪問IP
在/etc/hosts.allow和/etc/hosts.deny中配置IP白名單。
4. 使用非標(biāo)準(zhǔn)端口
修改SSH端口,避免暴力破解:
vim /etc/ssh/sshd_config
# 修改 Port 22 為其他端口,如 Port 2222
systemctl restart sshd5. 啟用fail2ban安裝fail2ban防止暴力破解:
6. 禁用root登錄(可選)# CentOS/RHEL
yum install fail2ban# Ubuntu/Debian
apt install fail2ban
創(chuàng)建普通用戶,使用sudo提權(quán):
附錄 A. SSH配置文件詳解# 創(chuàng)建普通用戶
adduser username
usermod -aG sudo username# 修改SSH配置
vim /etc/ssh/sshd_config
# 修改 PermitRootLogin yes 為 PermitRootLogin no
systemctl restart sshd
配置項
推薦值
PasswordAuthentication
是否允許密碼登錄
no
PubkeyAuthentication
是否允許公鑰認(rèn)證
yes
PermitRootLogin
是否允許root登錄
prohibit-password
Port
SSH監(jiān)聽端口
22(可修改)
PermitEmptyPasswords
是否允許空密碼
no
MaxAuthTries
最大認(rèn)證嘗試次數(shù)
3
B. SSH密鑰類型對比
類型
密鑰長度
安全性
兼容性
推薦度
RSA
2048/4096
極好
?????
ECDSA
256/384/521
極高
????
Ed25519
256
極高
較好
?????
C. 常用SSH命令
總結(jié)# 端口轉(zhuǎn)發(fā)
ssh -L 本地端口:目標(biāo)地址:目標(biāo)端口 用戶@服務(wù)器
# 文件傳輸
scp -i 私鑰文件 本地文件 用戶@服務(wù)器:/遠程路徑
# 遠程命令執(zhí)行
ssh 用戶@服務(wù)器 "命令"# SSH隧道
ssh -D 本地端口 用戶@服務(wù)器
通過本文檔的配置,您的服務(wù)器已經(jīng)實現(xiàn)了:
? 僅允許SSH證書登錄
? 禁用密碼登錄
? 提高服務(wù)器安全性
?? 重要提醒:
? 務(wù)必妥善保管私鑰文件
? 定期備份SSH配置
? 定期檢查SSH登錄日志
? 如果需要恢復(fù)密碼登錄,使用備份文件
文檔版本:1.0
更新日期:2026-01-16
適用系統(tǒng):CentOS 7+, Ubuntu 18+, Debian 9+
客戶端系統(tǒng):Windows 10/11, macOS, Linux
特別聲明:以上內(nèi)容(如有圖片或視頻亦包括在內(nèi))為自媒體平臺“網(wǎng)易號”用戶上傳并發(fā)布,本平臺僅提供信息存儲服務(wù)。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.