OpenSSH 服务器并启动/启用服务的完整流程

📋 完整流程图

复制代码
检查系统版本 → 安装 OpenSSH → 启动服务 → 启用自启 → 验证状态 → 配置防火墙

一、检查系统版本

bash 复制代码
cat /etc/os-release
发行版 服务名称 安装工具
Ubuntu/Debian ssh apt
RHEL/CentOS/Fedora/Rocky sshd dnf/yum
Arch Linux sshd pacman

二、安装 OpenSSH 服务器

Ubuntu/Debian

bash 复制代码
sudo apt update
sudo apt install openssh-server -y

RHEL/CentOS/Fedora/Rocky

bash 复制代码
sudo dnf install openssh-server -y
# 旧版本用:sudo yum install openssh-server -y

Arch Linux

bash 复制代码
sudo pacman -S openssh

三、启动 SSH 服务

bash 复制代码
# Ubuntu/Debian
sudo systemctl start ssh

# RHEL/CentOS/Fedora/Arch
sudo systemctl start sshd

四、设置开机自启

bash 复制代码
# Ubuntu/Debian
sudo systemctl enable ssh

# RHEL/CentOS/Fedora/Arch
sudo systemctl enable sshd

五、验证状态

bash 复制代码
# 查看服务状态(Ubuntu/Debian 用 ssh,其他用 sshd)
sudo systemctl status ssh

# 检查是否开机自启
sudo systemctl is-enabled ssh

# 检查端口监听
sudo ss -tlnp | grep :22

✅ 成功标志:

  • Active: active (running)
  • Loaded: ... enabled;
  • 端口 22 处于 LISTEN 状态

六、配置防火墙

UFW (Ubuntu)

bash 复制代码
sudo ufw allow ssh
sudo ufw reload

firewalld (RHEL/CentOS/Fedora)

bash 复制代码
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload

🔧 一键命令(复制即用)

Ubuntu/Debian 一键安装

bash 复制代码
sudo apt update && \
sudo apt install openssh-server -y && \
sudo systemctl start ssh && \
sudo systemctl enable ssh && \
sudo ufw allow ssh && \
sudo systemctl status ssh

RHEL/CentOS/Fedora 一键安装

bash 复制代码
sudo dnf install openssh-server -y && \
sudo systemctl start sshd && \
sudo systemctl enable sshd && \
sudo firewall-cmd --permanent --add-service=ssh && \
sudo firewall-cmd --reload && \
sudo systemctl status sshd

⚠️ 常见问题

问题 解决
Unit ssh.service not found sshd 代替 ssh
Connection refused 检查防火墙/SELinux
端口冲突 sudo lsof -i :22
启动失败 sudo journalctl -xeu ssh 看日志
相关推荐
maxmaxma21 小时前
ROS2 机器人 少年创客营:Day 7
人工智能·python·机器人·ros2
winfreedoms1 天前
ROS 2 Humble 编译报错全记录:APT 签名、镜像与 rosidl 工具链缺失
ros2
时空未宇1 天前
Hi3403开发板 + openEuler Embedded 运行IB-Robot + 控制 Lerobot 机械臂跳舞
ros2·具身智能·lerobot·hi3403·ib-robot
kyle~2 天前
ROS2 ---- TF2坐标变换(1.动态、静态发布,2.缓存,3.监听)
c++·机器人·ros2
知秋贺2 天前
ROS2的安装和使用
ros2
maxmaxma2 天前
ROS2机器人少年创客营:编程思维启蒙
机器人·ros2
maxmaxma3 天前
ROS2 机器人 少年创客营:Day6 - colcon
机器人·ros2
maxmaxma3 天前
ROS2 机器人 少年创客营:Day 6
机器人·ros2
kyle~3 天前
ROS2---客户端服务(rclcpp::Client)
c++·物联网·机器人·ros2
maxmaxma4 天前
ROS2 机器人 少年创客营:Day 5
机器人·ros2