在Debian系统中配置防火墙,你可以使用iptables
或ufw
(Uncomplicated Firewall)工具。以下是两种方法的简要说明:
iptables
更新系统:
sudo apt update && sudo apt upgrade
查看当前的iptables规则:
sudo iptables -L -n -v
添加规则: 例如,允许SSH连接(默认端口22):
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
保存规则: Debian默认不保存iptables规则,你需要手动保存:
sudo sh -c "iptables-save > /etc/iptables/rules.v4"
设置开机自启动: 创建一个systemd服务文件来加载iptables规则:
sudo nano /etc/systemd/system/iptables-restore.service
添加以下内容:
[Unit]
Description=Restore IPTables rules
After=network.target
[Service]
Type=oneshot
ExecStart=/sbin/iptables-restore < /etc/iptables/rules.v4
[Install]
WantedBy=multi-user.target
启用并启动服务:
sudo systemctl enable iptables-restore
sudo systemctl start iptables-restore
ufw
安装ufw:
sudo apt install ufw
启用ufw:
sudo ufw enable
查看ufw状态:
sudo ufw status
添加规则: 例如,允许SSH连接:
sudo ufw allow 22/tcp
删除规则: 例如,删除SSH规则:
sudo ufw delete allow 22/tcp
禁用ufw:
sudo ufw disable
ufw
或firewalld
。通过以上步骤,你可以在Debian系统中成功配置防火墙。