在 CentOS 上安装并启用 SSH 服务(通常是 OpenSSH)的步骤如下,适用于 CentOS 7 / 8 / Stream。
rpm -qa | grep openssh
或
systemctl status sshd
如果显示 sshd 服务信息,说明已安装。
yum install -y openssh-server
dnf install -y openssh-server
systemctl start sshd
systemctl enable sshd
检查状态:
systemctl status sshd
看到 active (running) 表示正常运行。
firewall-cmd --permanent --add-service=ssh
firewall-cmd --reload
iptables -I INPUT -p tcp --dport 22 -j ACCEPT
service iptables save
ss -lntp | grep ssh
或
netstat -lntp | grep ssh
配置文件路径:
/etc/ssh/sshd_config
常见安全优化:
Port 2222 # 修改默认端口
PermitRootLogin no # 禁止 root 远程登录
PasswordAuthentication yes
修改后重启服务:
systemctl restart sshd
⚠️ 修改端口后,防火墙也要放行新端口。
从另一台机器连接:
ssh 用户名@服务器IP
如果修改了端口:
ssh -p 2222 用户名@服务器IP
UseDNS no
GSSAPIAuthentication no
如果你告诉我:
我可以给你更精确的配置方案 ✅