在Linux系统中,配置防火墙规则通常使用iptables或firewalld工具。以下是使用这两个工具配置防火墙规则的基本步骤:
iptables查看当前规则:
sudo iptables -L -n -v
允许特定IP访问:
sudo iptables -A INPUT -s 192.168.1.100 -j ACCEPT
拒绝特定IP访问:
sudo iptables -A INPUT -s 192.168.1.101 -j DROP
允许特定端口访问:
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
拒绝特定端口访问:
sudo iptables -A INPUT -p tcp --dport 22 -j DROP
保存规则:
sudo iptables-save > /etc/iptables/rules.v4
恢复规则:
sudo iptables-restore < /etc/iptables/rules.v4
firewalld查看当前区域和规则:
sudo firewall-cmd --list-all
允许特定IP访问:
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.100" accept'
拒绝特定IP访问:
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.101" reject'
允许特定端口访问:
sudo firewall-cmd --permanent --add-port=80/tcp
拒绝特定端口访问:
sudo firewall-cmd --permanent --remove-port=22/tcp
重新加载防火墙配置:
sudo firewall-cmd --reload
sudo。iptables的规则在系统重启后会丢失,除非使用iptables-save和iptables-restore命令保存和恢复规则。firewalld的规则可以通过--permanent选项设置为持久化。通过以上步骤,你可以根据需要配置Linux系统的防火墙规则,以保护你的系统免受不必要的网络访问。