linux防火墙配置主要通过iptables和firewalld实现,前者更底层,后者更易用。1. iptables直接操作内核规则,使用-a添加规则,-d删除规则,-p设置默认策略,需手动保存规则至配置文件;2. firewalld采用区域管理方式,使用--add-port、--add-source等命令添加规则,--permanent设为永久生效,并通过--reload加载配置;3. 性能上iptables略优,但firewalld更便于动态管理;4. 策略选择应基于服务器用途开放必要端口并限制访问来源;5. 配置错误可通过检查状态、规则顺序、抓包分析等方式排查。

Linux防火墙配置,说白了就是设置哪些流量能进,哪些流量不能进。这事儿在Linux上主要靠
iptables
firewalld
iptables
netfilter
firewalld
iptables

解决方案
配置Linux防火墙,你可以选择直接使用
iptables
firewalld

1. 使用 iptables:
iptables

查看当前规则:
iptables -L
iptables -L -n
添加规则:
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -s 192.168.1.100 -j ACCEPT
iptables -A INPUT -j DROP
解释一下:
-A
INPUT
-p
--dport
-s
-j
ACCEPT
DROP
删除规则:
iptables -D INPUT -p tcp --dport 80 -j ACCEPT
或者,你可以先用
iptables -L --line-numbers
iptables -D INPUT <编号>
保存规则:
iptables-save > /etc/iptables/rules.v4
ip6tables-save > /etc/iptables/rules.v6
不同Linux发行版保存规则的方式可能不一样,比如CentOS用
service iptables save
iptables-save
加载规则:
iptables-restore < /etc/iptables/rules.v4
ip6tables-restore < /etc/iptables/rules.v6
2. 使用 firewalld:
firewalld
iptables
启动、停止和查看状态:
systemctl start firewalld
systemctl stop firewalld
systemctl status firewalld
查看默认区域:
firewall-cmd --get-default-zone
查看当前区域的规则:
firewall-cmd --list-all --zone=public
添加规则:
firewall-cmd --zone=public --add-port=80/tcp --permanent
--permanent
firewall-cmd --zone=public --add-service=http --permanent
firewall-cmd --zone=public --add-source=192.168.1.0/24 --permanent
删除规则:
firewall-cmd --zone=public --remove-port=80/tcp --permanent
重新加载防火墙:
firewall-cmd --reload
列出所有可用服务:
firewall-cmd --get-services
firewalld
/etc/firewalld/
代码示例 (iptables):
#!/bin/bash # 清空所有规则 iptables -F iptables -X iptables -Z # 设置默认策略 iptables -P INPUT DROP iptables -P FORWARD DROP iptables -P OUTPUT ACCEPT # 允许 loopback 接口 iptables -A INPUT -i lo -j ACCEPT iptables -A OUTPUT -o lo -j ACCEPT # 允许已建立的连接和相关连接 iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT # 允许 SSH 连接 (22 端口) iptables -A INPUT -p tcp --dport 22 -j ACCEPT # 允许 HTTP 和 HTTPS 连接 (80 和 443 端口) iptables -A INPUT -p tcp --dport 80 -j ACCEPT iptables -A INPUT -p tcp --dport 443 -j ACCEPT # 保存规则 iptables-save > /etc/iptables/rules.v4
代码示例 (firewalld):
#!/bin/bash # 设置默认区域为 public firewall-cmd --set-default-zone=public # 允许 SSH 服务 firewall-cmd --zone=public --add-service=ssh --permanent # 允许 HTTP 和 HTTPS 服务 firewall-cmd --zone=public --add-service=http --permanent firewall-cmd --zone=public --add-service=https --permanent # 允许特定端口 (例如 8080) firewall-cmd --zone=public --add-port=8080/tcp --permanent # 允许来自特定 IP 地址的流量 firewall-cmd --zone=public --add-source=192.168.1.100 --permanent # 重新加载防火墙 firewall-cmd --reload
iptables和firewalld性能差异?
iptables
netfilter
firewalld
firewalld
iptables
iptables
firewalld
如何选择合适的防火墙策略?
选择防火墙策略,首先要明确服务器的用途。如果是 Web 服务器,就要开放 80 和 443 端口。如果是数据库服务器,就要开放数据库的端口。总的原则是:只开放必要的端口,关闭所有不必要的端口。 另外,还要考虑安全性。 可以使用白名单策略,只允许特定的 IP 地址访问服务器。 还可以使用端口转发,将外部端口映射到内部端口,隐藏服务器的真实端口。
防火墙配置错误的常见问题及排查方法?
防火墙配置错误会导致各种问题,比如无法访问 Web 页面,无法连接数据库等等。排查方法一般是:
tcpdump
wireshark
另外,还要注意规则的顺序。
iptables
firewalld
以上就是Linux中的防火墙如何配置?_Linuxiptables与firewalld区别的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号