Debian防火墙规则制定
导读:在Debian系统中,你可以使用iptables或nftables来制定防火墙规则。以下是使用这两种工具的基本步骤: 使用iptables 安装iptables(如果尚未安装): sudo apt update sudo apt ins...
在Debian系统中,你可以使用iptables或nftables来制定防火墙规则。以下是使用这两种工具的基本步骤:
使用iptables
-
安装iptables(如果尚未安装):
sudo apt update sudo apt install iptables -
查看当前规则:
sudo iptables -L -n -v -
添加规则:
- 允许来自特定IP的SSH连接:
sudo iptables -A INPUT -p tcp --dport 22 -s 192.168.1.100 -j ACCEPT - 阻止来自特定IP的所有连接:
sudo iptables -A INPUT -s 192.168.1.100 -j DROP - 允许特定端口的流量(例如HTTP):
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
- 允许来自特定IP的SSH连接:
-
保存规则: Debian默认不会保存iptables规则,你需要手动保存:
sudo 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.service sudo systemctl start iptables-restore.service
使用nftables
-
安装nftables(如果尚未安装):
sudo apt update sudo apt install nftables -
查看当前规则:
sudo nft list ruleset -
添加规则:
- 允许来自特定IP的SSH连接:
sudo nft add rule ip filter input tcp dport 22 ip saddr 192.168.1.100 accept - 阻止来自特定IP的所有连接:
sudo nft add rule ip filter input ip saddr 192.168.1.100 drop - 允许特定端口的流量(例如HTTP):
sudo nft add rule ip filter input tcp dport 80 accept
- 允许来自特定IP的SSH连接:
-
保存规则:
sudo nft list ruleset > /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.service sudo systemctl start nftables-restore.service
通过以上步骤,你可以在Debian系统中制定和管理防火墙规则。根据你的具体需求,可以进一步调整和扩展这些规则。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Debian防火墙规则制定
本文地址: https://pptw.com/jishu/769196.html
