在Debian系统中,您可以使用iptables
或nftables
来配置防火墙规则。以下是使用这两种工具添加防火墙规则的基本步骤:
查看现有规则:
sudo iptables -L -n -v
添加规则: 例如,要允许来自特定IP地址的SSH连接(端口22):
sudo iptables -A INPUT -p tcp --dport 22 -s 192.168.1.100 -j ACCEPT
保存规则: Debian默认不保存iptables规则,所以您需要手动保存:
sudo sh -c "iptables-save > /etc/iptables/rules.v4"
恢复规则: 在系统启动时恢复规则:
sudo iptables-restore < /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
安装nftables:
sudo apt update
sudo apt install nftables
查看现有规则:
sudo nft list ruleset
添加规则: 例如,要允许来自特定IP地址的SSH连接(端口22):
sudo nft add rule inet filter input tcp dport 22 ip saddr 192.168.1.100 accept
保存规则:
sudo nft list ruleset > /etc/nftables.conf
恢复规则: 在系统启动时恢复规则:
sudo nft -f /etc/nftables.conf
设置开机自启: 创建一个systemd服务来加载nftables规则:
sudo nano /etc/systemd/system/nftables-restore.service
添加以下内容:
[Unit]
Description=Restore nftables rules
After=network.target
[Service]
Type=oneshot
ExecStart=/sbin/nft -f /etc/nftables.conf
[Install]
WantedBy=multi-user.target
启用并启动服务:
sudo systemctl enable nftables-restore
sudo systemctl start nftables-restore
通过以上步骤,您可以在Debian系统中使用iptables
或nftables
添加和管理防火墙规则。