温馨提示×

CentOS SSH防火墙规则怎么配置

小樊
47
2025-05-02 17:28:13
栏目: 智能运维

在CentOS系统中,配置SSH防火墙规则通常涉及使用firewalldiptables。以下是使用这两种工具配置SSH防火墙规则的步骤:

使用 firewalld

  1. 启动并启用 firewalld 服务

    sudo systemctl start firewalld
    sudo systemctl enable firewalld
    
  2. 允许SSH连接

    sudo firewall-cmd --permanent --add-service=ssh
    
  3. 重新加载防火墙规则

    sudo firewall-cmd --reload
    
  4. 验证SSH端口是否开放

    sudo firewall-cmd --list-all
    

    你应该能看到ssh服务在services列表中,并且端口22是开放的。

使用 iptables

  1. 允许SSH连接

    sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
    
  2. 保存 iptables 规则: CentOS 7及以上版本使用firewalld,但如果你使用的是CentOS 6或其他不支持firewalld的系统,可以使用以下命令保存规则:

    sudo service iptables save
    
  3. 重启 iptables 服务

    sudo service iptables restart
    
  4. 验证SSH端口是否开放

    sudo iptables -L -n | grep 22
    

    你应该能看到一条规则允许TCP端口22的流量。

注意事项

  • 安全性:确保你的SSH服务配置文件(通常是/etc/ssh/sshd_config)中的PermitRootLoginPasswordAuthentication选项设置得当,以提高安全性。
  • 防火墙状态:在生产环境中,确保防火墙始终处于活动状态,并定期检查和更新规则。
  • 备份配置:在进行任何更改之前,备份现有的防火墙配置文件,以便在需要时可以恢复。

通过以上步骤,你应该能够在CentOS系统上成功配置SSH防火墙规则。

0