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
)以排查异常登录行为。