在Debian系统下,你可以使用iptables
或ufw
(Uncomplicated Firewall)来配置防火墙。以下是两种方法的简要说明和步骤:
iptables
安装iptables(如果尚未安装)
sudo apt update
sudo apt install iptables
查看当前的iptables规则
sudo iptables -L -n -v
添加规则
sudo iptables -A INPUT -p tcp --dport 22 -s 你的IP地址 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 80 -s 你的IP地址 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -s 你的IP地址 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 3306 -s 你的IP地址 -j ACCEPT
sudo iptables -P INPUT DROP
保存iptables规则 Debian默认不保存iptables规则,你需要手动保存:
sudo sh -c "iptables-save > /etc/iptables/rules.v4"
设置iptables在启动时自动加载规则
编辑/etc/network/if-pre-up.d/iptables
文件:
sudo nano /etc/network/if-pre-up.d/iptables
添加以下内容:
#!/bin/sh
/sbin/iptables-restore < /etc/iptables/rules.v4
赋予执行权限:
sudo chmod +x /etc/network/if-pre-up.d/iptables
ufw
安装ufw(如果尚未安装)
sudo apt update
sudo apt install ufw
启用ufw
sudo ufw enable
查看ufw状态
sudo ufw status
添加规则
sudo ufw allow from 你的IP地址 to any port 22
sudo ufw allow from 你的IP地址 to any port 80
sudo ufw allow from 你的IP地址 to any port 443
sudo ufw allow from 你的IP地址 to any port 3306
禁用ufw(如果需要)
sudo ufw disable
通过以上步骤,你可以在Debian系统下配置防火墙,保护你的服务器免受不必要的网络攻击。