SSH(Secure Shell) 是Linux服务器远程管理的核心工具,它加密所有通信,防止中间人攻击和密码嗅探。无论你是新手还是老手,掌握SSH教程都是必备技能。本文将详细讲解在两大主流Linux发行版家族——CentOS/RHEL 和 Ubuntu/Debian 上配置和管理SSH服务的完整步骤,并加入安全加固建议,助你轻松构建安全的远程连接环境。
SSH是一种网络协议,用于在不安全的网络上安全地运行网络服务。最常见的用途是远程登录到Linux服务器并执行命令。默认端口是22。通过Linux SSH配置,我们可以控制访问权限、加密方式等。
大多数Linux发行版默认已安装OpenSSH服务器。若未安装,请根据系统使用以下命令:
CentOS/RHEL 系列:sudo yum install openssh-server -y 或 sudo dnf install openssh-server -y(CentOS 8+)
Ubuntu/Debian 系列:sudo apt update && sudo apt install openssh-server -y
安装后,启动服务并设置开机自启:
CentOS/RHEL:sudo systemctl start sshd && sudo systemctl enable sshd
Ubuntu/Debian:sudo systemctl start ssh && sudo systemctl enable ssh(注意服务名可能为ssh)
检查状态:sudo systemctl status sshd 或 sudo systemctl status ssh
修改配置文件前建议备份:sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
#Port 22 改为 Port 2222(自定义高位端口)。#PermitRootLogin yes 改为 PermitRootLogin no。AllowUsers user1 user2 仅允许指定用户。PubkeyAuthentication yes 未被注释。PasswordAuthentication no。修改后重启服务生效:sudo systemctl restart sshd(或ssh)。
基本命令:ssh 用户名@服务器IP -p 端口。例如:ssh admin@192.168.1.100 -p 2222。
在客户端生成密钥对:ssh-keygen -t rsa -b 4096。然后将公钥复制到服务器:ssh-copy-id 用户名@服务器IP -p 端口。之后即可免密码登录。
Q: 连接被拒绝? A: 检查服务是否运行,防火墙是否放行端口(sudo firewall-cmd --add-port=2222/tcp --permanent for CentOS;sudo ufw allow 2222/tcp for Ubuntu)。
Q: 密钥登录失败? A: 检查.ssh目录权限(应为700)和authorized_keys权限(应为600),以及SELinux(CentOS需restorecon -R -v /home/用户名/.ssh)。
通过以上步骤,你已经掌握了CentOS SSH和Ubuntu SSH的基本配置与安全加固,可以自信地管理你的Linux服务器了。
本文由主机测评网于2026-02-14发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://vpshk.cn/20260225131.html