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 看日志
相关推荐
风合星语2 天前
2026 ROS 2 Lyrical C++ 入门(五):Action 实战——任务反馈、取消与超时处理
c++·机器人·ros2·异步编程·lyrical
浅梦语113 天前
32-1 nav2_map_server实际作用与用法图解
ros2·nav2
米糕闯编程5 天前
鱼香ros2(七)功能包组织C++节点
c++·ros2·cmakelists
米糕闯编程5 天前
鱼香ros2(五)用C++编写节点
c++·ros2·cmakelists·鱼香
blueSatchel7 天前
c++action 编写(ros-humble)
c++·ros2
blueSatchel8 天前
ros-humble仿真-建图-导航
linux·ros2
米糕闯编程10 天前
鱼香ros2(三)Linux操作总结
linux·机器人·ros2
蝙蝠侠的小蝙蝠11 天前
【ROS2 工具】Launch
ros2·launch·多节点启动
某林21211 天前
机器人重启失联:DDS 发现机制与传输层静默故障
人工智能·python·机器人·硬件架构·ros2
米糕闯编程12 天前
鱼香ros2(二)运行第一个机器人
机器人·ros2