CentOS Stream 8 远程连接的常用方式与步骤
一、SSH 命令行远程(推荐)
- 安装并启动服务
- 安装 OpenSSH 服务器:sudo dnf install -y openssh-server
- 启动并设置开机自启:sudo systemctl start sshd && sudo systemctl enable sshd
- 检查状态:sudo systemctl status sshd(应为 active/running)
- 防火墙放行
- 放行默认端口:sudo firewall-cmd --permanent --add-port=22/tcp && sudo firewall-cmd --reload
- 客户端连接
- Linux/macOS:ssh 用户名@服务器IP
- Windows:在 CMD/PowerShell 执行 ssh 用户名@服务器IP,或使用 PuTTY/Xshell
- 安全加固(可选但强烈建议)
- 编辑 /etc/ssh/sshd_config:设置 PermitRootLogin no、使用 密钥登录、必要时修改端口(如 2222)
- 若修改端口,需放行新端口并(若启用 SELinux)执行:sudo semanage port -a -t ssh_port_t -p tcp 2222
- 重启服务:sudo systemctl restart sshd
- 验证连通
- 查看端口:ss -tnlp | grep :22;或 firewall-cmd --list-ports 应包含 22/tcp
二、图形桌面远程桌面 RDP(Xrdp)
- 安装与启动
- 安装 EPEL 与 Xrdp:sudo dnf install -y epel-release && sudo dnf install -y xrdp
- 启动并设置开机自启:sudo systemctl start xrdp && sudo systemctl enable xrdp
- 配置桌面会话
- 编辑 /etc/xrdp/xrdp.ini,在文件末尾添加:exec gnome-session(如使用 GNOME)
- 重启服务:sudo systemctl restart xrdp
- 防火墙放行
- 放行 RDP 端口:sudo firewall-cmd --permanent --add-port=3389/tcp && sudo firewall-cmd --reload
- 客户端连接
- Windows:打开“远程桌面连接”(mstsc),输入服务器 IP,使用系统用户名与密码登录
- Linux/macOS:使用 Remmina 等 RDP 客户端,地址填 IP:3389
- 使用提示
三、VNC 远程桌面(TigerVNC 示例)
- 安装与首次启动
- 安装 TigerVNC:sudo dnf install -y tigervnc-server
- 首次运行设置口令:vncserver :1(会在 ~/.vnc 生成 passwd)
- 配置系统服务(推荐)
- 复制服务模板:sudo cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service
- 按需编辑以指定用户与参数,然后:sudo systemctl daemon-reload
- 启动并设置开机自启:sudo systemctl start vncserver@:1 && sudo systemctl enable vncserver@:1
- 防火墙放行
- 放行 VNC 端口(桌面号 1 对应 5901/tcp):sudo firewall-cmd --permanent --add-port=5901/tcp && sudo firewall-cmd --reload
- 客户端连接
- 使用任意 VNC 客户端连接 服务器IP:5901,输入 VNC 口令登录
四、常见问题与快速排查
- 无法连接
- 核对服务器 IP 与网络连通(ping/路由)
- 检查服务是否运行:systemctl status sshd/xrdp 或 ss -tnlp | grep :22/:3389/:5901
- 核对防火墙:firewall-cmd --list-ports 是否包含 22/tcp、3389/tcp、5901/tcp
- 修改 SSH 端口后连不上
- 确认已放行新端口并(启用 SELinux 时)执行:sudo semanage port -a -t ssh_port_t -p tcp 新端口;重启 sshd
- 图形会话异常
- 确认已安装并启用 GNOME(或目标桌面),以及 xrdp 配置中的 exec gnome-session 正确