centos

如何配置CentOS SSH防火墙规则

小樊
42
2025-08-04 08:54:14
栏目: 智能运维

配置CentOS SSH防火墙规则通常涉及使用firewalldiptables。以下是使用这两种方法的步骤:

使用 firewalld

  1. 启动并启用 firewalld 服务

    sudo systemctl start firewalld
    sudo systemctl enable firewalld
    
  2. 开放SSH端口(默认是22)

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

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

    sudo firewall-cmd --list-all
    

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

使用 iptables

  1. 启动并启用 iptables 服务

    sudo systemctl start iptables
    sudo systemctl enable iptables
    
  2. 开放SSH端口(默认是22)

    sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
    
  3. 保存 iptables 规则: CentOS 7及以上版本使用firewalld,但如果你使用的是CentOS 6或更早版本,可以使用以下命令保存规则:

    sudo service iptables save
    
  4. 验证规则是否已添加

    sudo iptables -L -n
    

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

注意事项

通过以上步骤,你应该能够成功配置CentOS的SSH防火墙规则,确保SSH服务的安全性和可用性。

0
看了该问题的人还看了