Linux Minimal系統使用SSH指南
SSH(Secure Shell)是Linux Minimal系統遠程管理的核心工具,以下是安裝、配置、連接及安全優化的全流程步驟:
Linux Minimal系統默認未安裝SSH服務,需通過包管理器安裝:
openssh-server
:sudo apt update && sudo apt install openssh-server -y
openssh-server
包:sudo yum install openssh-server -y # CentOS 7及以下
sudo dnf install openssh-server -y # CentOS 8及以上
安裝完成后,SSH服務會自動啟動(若未啟動,手動執行sudo systemctl start ssh
/sudo systemctl start sshd
)。
確保SSH服務開機自啟并當前運行:
# 啟動SSH服務
sudo systemctl start ssh # Ubuntu/Debian
sudo systemctl start sshd # CentOS/RHEL
# 設置開機自啟
sudo systemctl enable ssh # Ubuntu/Debian
sudo systemctl enable sshd # CentOS/RHEL
# 檢查服務狀態(確認"active (running)")
sudo systemctl status ssh # 或 sshd
編輯配置文件/etc/ssh/sshd_config
(需root權限),優化安全性與功能:
#Port 22
,取消注釋并修改為其他端口(如Port 2222
);#PermitRootLogin yes
,修改為PermitRootLogin no
(需用普通用戶+sudo
管理);PubkeyAuthentication yes
未被注釋(默認開啟);AllowUsers your_username
(僅允許指定用戶登錄)。修改后重啟服務生效:
sudo systemctl restart ssh # Ubuntu/Debian
sudo systemctl restart sshd # CentOS/RHEL
若系統啟用防火墻,需放行SSH端口(默認22或自定義端口):
sudo ufw allow ssh # 允許默認SSH端口(22)
sudo ufw allow 2222/tcp # 若更改端口,替換為實際端口
sudo ufw reload # 重新加載防火墻規則
sudo firewall-cmd --permanent --add-service=ssh # 允許默認端口
sudo firewall-cmd --permanent --add-port=2222/tcp # 若更改端口
sudo firewall-cmd --reload # 重新加載規則
從遠程計算機(如本地電腦)使用SSH客戶端連接:
ssh username@server_ip
例如:ssh alice@192.168.1.100
(username
為服務器上的用戶,server_ip
為服務器IP地址)。ssh -p 2222 username@server_ip
例如:ssh -p 2222 bob@192.168.1.100
。密鑰認證比密碼更安全,避免密碼泄露風險:
ssh-keygen -t rsa -b 4096
按提示操作(默認保存路徑~/.ssh/id_rsa
,可設置空密碼或自定義密碼)。ssh-copy-id -p 2222 username@server_ip # 若更改了端口,添加-p參數
輸入服務器用戶密碼后,公鑰會自動添加到服務器的~/.ssh/authorized_keys
文件中。/etc/ssh/sshd_config
,將PasswordAuthentication yes
修改為PasswordAuthentication no
,然后重啟SSH服務。通過以上步驟,即可在Linux Minimal系統上完成SSH的安裝、配置與安全使用。建議定期更新SSH軟件包(sudo apt update && sudo apt upgrade openssh-server
/sudo yum update openssh-server
),并監控日志(/var/log/auth.log
或/var/log/secure
)以排查異常登錄行為。