马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
Linux 里"关防火墙"看着简单,实际一动手就会发现有两种防火墙、临时关和永久关两回事,选错命令重启就打回原形。本文把 iptables 与 firewalld 怎么区分、临时关和永久关分别怎么做、配置文件改法是怎么个原理,一次讲清楚,并附上安全提醒。
一、先分清自己机器上跑的是哪种
这一步最省事的办法是直接用两条命令对比:
sudo firewall-cmd --state
输出 running 或 not running,说明是 firewalld。
另一种是用 iptables 直接问:
iptables -L -n
如果返回 iptables: Firewall is not running. 说明用的是 iptables 后端;返回 iptables: Firewall is running. 说明它正在工作(多数情况下是 firewalld 底层调了 iptables)。
简单记:能用 firewall-cmd 那就是 firewalld 体系,直接用它;没有这个命令就走 iptables 那套。 CentOS 7 及以上默认 firewalld,老系统(CentOS 6、Ubuntu 老版本)多为 iptables。
二、临时关闭(重启后自动恢复)
只想在调试时不挡事,重启后自动恢复原状,用这个:停服务即可。
systemctl stop firewalld
查看当前状态(running 表示开着):
systemctl status firewalld
注意:临时停服务和永久关是两件事,只停服务不会写进开机启动配置,重启之后防火墙自己就起来了。调试完想要恢复,再 systemctl start firewalld 即可。
三、永久关闭(重启后仍不启动)
firewalld 的永久关闭是"停服务 + 禁开机启动"两条一起做,只做一条都会留活口:
sudo systemctl stop firewalld
sudo systemctl disable firewalld
iptables 体系则是停服务加改开机自启:
sudo service iptables stop
sudo chkconfig iptables off
四、通过配置文件彻底关掉
除了命令,两种防火墙也都能靠改配置文件设为不开:
iptables:直接把规则文件里的规则清空保存,等于没有规则可拦:
sudo vi /etc/sysconfig/iptables
进去把内容清空或注释掉,保存退出即可。
firewalld:改它的主配置里的启动开关:
sudo vi /etc/firewalld/firewalld.conf
找到 enabled 字段,把 yes 改成 no,保存退出。
改配置属于更"硬"的做法,适用于需要完全不留服务的场景。改之前确认自己知道怎么恢复——忘了自己改过哪个文件,排查起来很费劲。
五、安全提醒(重要)
关掉防火墙会让系统暴露在带来风险的访问之下。实际操作建议:
- 只在可控的内网环境或调试窗口期关,公网机器不要长期裸奔;
- 关闭之后用其他手段兜底,比如只放行必要端口(firewalld 用
--add-port,iptables 用 -A INPUT),或者靠安全组/白名单在网关层挡住;
- 关完记得确认一次实际状态(
firewall-cmd --state 或 systemctl is-enabled firewalld),别以为执行了命令就真关了;
- 改配置前先备份:
cp 文件名 文件名.bak,改坏了一秒还原。
六、一张表记住
- 快速看类型:
firewall-cmd --state 还是 iptables -L;
- 临时关 firewalld:
systemctl stop firewalld;
- 永久关 firewalld:stop +
disable 两条;
- 永久关 iptables:
service iptables stop + chkconfig iptables off;
- 硬关 firewalld:
firewalld.conf 里 enabled 改 no。
qidao123.com企服评测-应用市场 编辑提示 |