Linux minimal如何使用SSH
导读:Linux Minimal系统使用SSH指南 SSH(Secure Shell)是Linux Minimal系统远程管理的核心工具,以下是安装、配置、连接及安全优化的全流程步骤: 1. 安装OpenSSH服务器 Linux Minimal系...
Linux Minimal系统使用SSH指南
SSH(Secure Shell)是Linux Minimal系统远程管理的核心工具,以下是安装、配置、连接及安全优化的全流程步骤:
1. 安装OpenSSH服务器
Linux Minimal系统默认未安装SSH服务,需通过包管理器安装:
- Ubuntu/Debian Minimal:更新软件包列表后安装
openssh-server
:sudo apt update & & sudo apt install openssh-server -y
- CentOS/RHEL Minimal:安装
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
)。
2. 启动与启用SSH服务
确保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
3. 配置SSH服务(可选但推荐)
编辑配置文件/etc/ssh/sshd_config
(需root权限),优化安全性与功能:
- 更改默认端口(避免暴力破解):找到
#Port 22
,取消注释并修改为其他端口(如Port 2222
); - 禁用root直接登录:找到
#PermitRootLogin yes
,修改为PermitRootLogin no
(需用普通用户+sudo
管理); - 启用密钥认证(更安全):确保
PubkeyAuthentication yes
未被注释(默认开启); - 限制允许登录的用户:添加
AllowUsers your_username
(仅允许指定用户登录)。
修改后重启服务生效:
sudo systemctl restart ssh # Ubuntu/Debian
sudo systemctl restart sshd # CentOS/RHEL
4. 配置防火墙允许SSH连接
若系统启用防火墙,需放行SSH端口(默认22或自定义端口):
- Ubuntu/Debian(UFW):
sudo ufw allow ssh # 允许默认SSH端口(22) sudo ufw allow 2222/tcp # 若更改端口,替换为实际端口 sudo ufw reload # 重新加载防火墙规则
- CentOS/RHEL(firewalld):
sudo firewall-cmd --permanent --add-service=ssh # 允许默认端口 sudo firewall-cmd --permanent --add-port=2222/tcp # 若更改端口 sudo firewall-cmd --reload # 重新加载规则
5. 连接到SSH服务器
从远程计算机(如本地电脑)使用SSH客户端连接:
- 基本命令(默认端口22):
例如: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
。
6. 提升安全性:配置SSH密钥认证(可选但强烈推荐)
密钥认证比密码更安全,避免密码泄露风险:
- 生成密钥对(在客户端执行):
按提示操作(默认保存路径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
)以排查异常登录行为。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Linux minimal如何使用SSH
本文地址: https://pptw.com/jishu/719475.html