返回

Linux防火墙配置常用命令

默认入站禁止

iptables: iptables -P INPUT DROP

UFW: ufw default deny

firewalld: firewall-cmd --set-default-zone=public

允许特定端口被访问

iptables: iptables -A INPUT -p tcp --dport 22 -j ACCEPT

UFW: ufw allow 22

firewalld: firewall-cmd --permanent --zone=public --add-port=22/tcp

允许特定端口被指定IP访问

iptables: iptables -A INPUT -s 192.168.1.1 -p tcp --dport 22 -j ACCEPT

UFW: ufw allow from 192.168.1.1 to any port 22

firewalld: firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.1" port port="22" protocol="tcp" accept'

允许特定IP访问所有端口

iptables: iptables -A INPUT -s 192.168.1.1 -p tcp -j ACCEPT

UFW: ufw allow from 192.168.1.1

firewalld: firewall-cmd --permanent --zone=trusted --add-source=192.168.1.1

注意事项

如果要给IP范围段设置规则,直接把以上命令的IP改为192.168.1.0/24的形式即可

iptables: iptables需要寻找持久化工具保存规则

firewalld: 添加完规则后需要运行firewall-cmd --reload命令载入应用最新规则

Licensed under CC BY-NC-SA 4.0
最后更新于 Jul 17, 2021 22:48 CST
Built with Hugo
Theme Stack designed by Jimmy