OpenSSH 是 Linux 系统中最常用的远程登录和文件传输工具之一。本文将指导您如何在常见 Linux 发行版中安装并启动 OpenSSH 服务。
1. 在 Ubuntu/Debian 系统中安装 OpenSSH
打开终端,执行以下命令:
sudo apt update
sudo apt install openssh-server -y
安装完成后,SSH 服务通常会自动启动。若未启动,可手动启动:
sudo systemctl start ssh
sudo systemctl enable ssh
2. 在 CentOS/RHEL/Fedora 系统中安装 OpenSSH
对于基于 Red Hat 的系统,使用以下命令:
sudo yum install openssh-server -y
sudo dnf install openssh-server -y
启动并设置开机自启:
sudo systemctl start sshd
sudo systemctl enable sshd
3. 检查 SSH 服务状态
无论哪种系统,都可以使用如下命令检查服务是否运行:
sudo systemctl status ssh
sudo systemctl status sshd
4. 防火墙设置(如启用)
确保防火墙允许 SSH(默认端口 22)通信:
# Ubuntu (UFW)
sudo ufw allow 22
# CentOS (firewalld)
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
5. 连接测试
从另一台机器使用 SSH 客户端连接:
ssh username@your_server_ip