debian

debian防火墙如何集成

小樊
48
2025-04-22 09:00:58
栏目: 智能运维

在Debian系统中,防火墙可以通过多种方式集成和配置,以下是使用 ufwiptables 的详细步骤:

使用ufw配置防火墙

  1. 安装ufw
sudo apt update
sudo apt install ufw
  1. 启用ufw
sudo ufw enable
  1. 设置默认策略
sudo ufw default deny incoming
sudo ufw default allow outgoing
  1. 允许特定端口和服务
sudo ufw allow 80/tcp  # 允许HTTP(80端口)
sudo ufw allow 443/tcp # 允许HTTPS(443端口)
sudo ufw allow ssh     # 允许SSH连接(默认端口22)
  1. 查看ufw状态
sudo ufw status
  1. 启用日志记录(可选):
sudo ufw logging on

日志文件通常保存在 /var/log/ufw.log,可以使用以下命令查看日志内容:

sudo less /var/log/ufw.log

或者实时监控日志:

sudo tail -f /var/log/ufw.log

使用iptables配置防火墙

  1. 安装iptables(如果尚未安装):
sudo apt update
sudo apt install iptables
  1. 配置防火墙规则
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT  # 允许SSH端口
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT  # 允许HTTP端口
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT # 允许HTTPS端口
sudo iptables -A INPUT -j LOG --log-prefix "iptables denied: " # 记录被拒绝的连接
  1. 保存规则
sudo iptables-save /etc/iptables/rules.v4
  1. 启用防火墙
sudo systemctl enable iptables
sudo systemctl start iptables
  1. 查看防火墙状态
sudo iptables -L
  1. 永久保存iptables规则(以免重启后丢失):
sudo tee /etc/network/if-pre-up.d/iptables <<EOF
#!/bin/sh
/sbin/iptables-restore < /etc/iptables/rules.v4
EOF
sudo chmod +x /etc/network/if-pre-up.d/iptables
sudo iptables-save > /etc/iptables/rules.v4

通过上述步骤,您可以在Debian系统上成功集成和配置防火墙,确保系统的安全性和稳定性。

0
看了该问题的人还看了