linux

FetchLinux如何配置防火墙

小樊
34
2025-05-12 00:01:03
栏目: 智能运维

在Linux系统中,防火墙配置是一个重要的步骤,用于保护系统免受恶意攻击和未经授权的访问。以下是如何配置Linux防火墙的详细步骤:

使用iptables配置防火墙

  1. 安装iptables(如果未安装):

    • Debian/Ubuntu: sudo apt-get install iptables
    • RHEL/CentOS: sudo yum install iptables
  2. 启动并设置iptables服务

    • Debian/Ubuntu:
      sudo systemctl start iptables
      sudo systemctl enable iptables
      
    • RHEL/CentOS:
      sudo systemctl start iptables
      sudo systemctl enable iptables
      
  3. 查看当前iptables规则

    sudo iptables -L -n -v
    
  4. 配置默认策略

    • 设置默认策略为DROP以提高安全性:
      sudo iptables -P INPUT DROP
      sudo iptables -P FORWARD DROP
      sudo iptables -P OUTPUT ACCEPT
      
  5. 添加规则

    • 允许特定流量通过(例如允许SSH和HTTP流量):
      sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
      sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
      
  6. 保存当前配置

    • Debian/Ubuntu:
      sudo iptables-save /etc/iptables/rules.v4
      
    • RHEL/CentOS:
      sudo service iptables save
      

使用firewalld配置防火墙

  1. 安装firewalld(如果未安装):

    • RHEL/CentOS: sudo yum install firewalld
    • Fedora: sudo dnf install firewalld
  2. 启动并设置firewalld服务

    sudo systemctl start firewalld
    sudo systemctl enable firewalld
    
  3. 查看当前区域配置

    sudo firewall-cmd --get-default-zone
    
  4. 添加规则到区域

    • 允许HTTP和HTTPS服务:
      sudo firewall-cmd --zone=public --add-service=http --permanent
      sudo firewall-cmd --zone=public --add-service=https --permanent
      
  5. 重新加载配置: 每次修改配置后,需重新加载防火墙以使更改生效:

    sudo firewall-cmd --reload
    

通过以上步骤,你可以有效地配置Linux系统的防火墙,以确保系统的安全性。

0
看了该问题的人还看了