如何在CentOS 7上使用iptables?

153

我安装了最小化配置的CentOS 7(只包括操作系统和开发工具)。 我试图为httpd服务打开80端口,但我的iptables服务出了些问题...这是什么问题?我做错了什么?

# ifconfig/sbin/service iptables save
bash: ifconfig/sbin/service: No such file or directory


# /sbin/service iptables save
The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.

# sudo service iptables status
Redirecting to /bin/systemctl status  iptables.service
iptables.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)

# /sbin/service iptables save
The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.

# sudo service iptables start
Redirecting to /bin/systemctl start  iptables.service
Failed to issue method call: Unit iptables.service failed to load: No such file or directory.

尝试执行此命令:/etc/init.d/iptables save。 - Satya
8
我在这里找到了答案。RHEL 7使用firewalld而不是iptables - Meiblorn
在我看来,firewalld比iptables更加用户友好。首先安装并启动firewalld服务 sudo yum install -y firewalld && sudo systemctl start firewalld。然后添加HTTP服务 sudo firewall-cmd --permanent --add-service=http(如果需要打开443端口,则还需添加_https_服务)。最后将新配置加载到firewalld中 sudo firewall-cmd --reload - Takman
9个回答

342

在 RHEL 7 / CentOS 7 中,引入了firewalld来管理iptables。我认为,firewalld 更适合工作站而不是服务器环境。

可以返回到更经典的 iptables 设置。首先,停止并屏蔽 firewalld 服务:

systemctl stop firewalld
systemctl mask firewalld

然后,安装iptables-services软件包:

yum install iptables-services

开机启动时启用该服务:

systemctl enable iptables

管理服务

systemctl [stop|start|restart] iptables

保存防火墙规则的方法如下:

service iptables save
或者
/usr/libexec/iptables/iptables.init save

5
无法保存iptables规则。重启服务器会导致所有更改丢失。 - roosevelt
2
在我的系统上,iptables 已经正确保存了。你确定 iptables 服务已经在启动时启动了吗?你可以通过运行“systemctl enable iptables”来实现这一点。 - Sgaduuw
1
你可能也在运行firewalld,它将向iptables规则列表中添加额外的规则(每次系统重启时)。要停止firewalld,请运行“systemctl mask firewalld”。 - TroodoN-Mike
很棒的帖子,但您认为值得投资学习firewalld或shorewall如何处理事情而不是iptables吗?(对我来说,iptables似乎很直接) - wired00
8
@Sgaduuw,你能否详细说明一下你认为firewall不适合/不太适合服务器的原因? - Alexander Groß
显示剩余7条评论

103

RHEL 和 CentOS 7 使用 firewall-cmd 而不是 iptables。您应该使用这种类型的命令:

# add ssh port as permanent opened port
firewall-cmd --zone=public --add-port=22/tcp --permanent

然后,您可以重新加载规则以确保一切正常

firewall-cmd --reload

相对于使用 iptable-save,这种方法更好,特别是如果你计划使用 lxc 或者 docker 容器。启动 docker 服务会添加一些规则,而 iptable-save 命令提示的那些规则中可能会有很多不应该被保存的规则。因为在下次重启时,docker 容器可能会更改它们的 IP 地址。

使用 firewall-cmd 命令的 permanent 选项更加合适。

查看 "man firewall-cmd" 或者 查看官方 firewalld 文档 以了解选项。有许多选项可以检查区域、配置、工作原理等等... man 页面非常完整。

我强烈建议您不要使用 Centos 7 中的 iptables-service。


1
firewall-cmd --reload 不起作用。我不得不使用 "systemctl restart firewalld" 重新启动防火墙才能使更改生效。 - Basil Musa
因为firewalld是动态的,因此可以随时进行配置更改,并立即实施。防火墙的任何部分都不需要重新加载,因此不会意外中断现有的网络连接。 - yicone

18

我遇到了一个问题,重启后iptables无法自动启动。

以下方法解决了这个问题:

yum install iptables-services
systemctl mask firewalld
systemctl enable iptables
systemctl enable ip6tables
systemctl stop firewalld
systemctl start iptables
systemctl start ip6tables

1
在CentOS 7上无法工作:# systemctl start iptables会出现以下错误:Failed to start iptables.service: Unit not found.# systemctl start ip6tables也会出现以下错误:Failed to start ip6tables.service: Unit not found. - PKHunter

18

尝试使用以下命令iptables-save


man iptables-save - 将iptables规则转储到标准输出。 - Paul Hargreaves
2
然后我们可以使用 iptables-save > /etc/sysconfig/iptables - dgregory

5

我修改了/etc/sysconfig/ip6tables-config文件,更改如下:

IP6TABLES_SAVE_ON_STOP="no"

收件人:

IP6TABLES_SAVE_ON_STOP="yes"

And this:

IP6TABLES_SAVE_ON_RESTART="no"

致:

IP6TABLES_SAVE_ON_RESTART="yes"

通过重新启动,我使用iptables命令所做的更改似乎已经保存。


这在RHEL 7上不起作用。 - pacoverflow

1
将IPtables配置放入传统文件中,它将在启动后加载:
/etc/sysconfig/iptables

1

上个月我尝试在一个LXC VM container上配置iptables防火墙,但每次重启后iptable的配置都未自动加载。

让它正常工作的唯一方法是运行以下命令:

yum -y install iptables-services; systemctl disable firewalld; systemctl mask firewalld; service iptables restart; service iptables save


我相信这是因为容器不会持久化任何东西,除非它在主机操作系统上的一个持久化卷中。大多数容器操作系统和配置通常不在卷中,因此每次容器重新启动时都会重置。 - Graham Lea

0
此外,运行systemctl mask firewalld命令后,您还应该能够对ip6tables执行相同的操作:
    systemctl start ip6tables.service
    systemctl enable ip6tables.service

0
如果您这样做了,而且正在使用fail2ban,那么您需要启用正确的过滤器/操作:
将以下行放入/etc/fail2ban/jail.d/sshd.local中。
[ssh-iptables]
enabled  = true
filter   = sshd
action   = iptables[name=SSH, port=ssh, protocol=tcp]
logpath  = /var/log/secure
maxretry = 5
bantime = 86400

启用并启动fail2ban:

systemctl enable fail2ban
systemctl start fail2ban

参考资料:http://blog.iopsl.com/fail2ban-on-centos-7-to-protect-ssh-part-ii/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接