154

我以最少的配置(操作系统 + 开发工具)安装了 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.
4

9 回答 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
于 2014-07-18T14:29:30.633 回答
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 地址。

带有永久选项的防火墙-cmd 更适合这一点。

检查“man firewall-cmd”或查看官方 firewalld 文档以查看选项。有很多选项可以检查区域、配置、它是如何工作的……手册页真的很完整。

我强烈建议从 Centos 7 开始不要使用 iptables-service

于 2014-08-10T15:12:07.057 回答
18

我遇到了重启无法启动 iptables 的问题。

这修复了它:

yum install iptables-services
systemctl mask firewalld
systemctl enable iptables
systemctl enable ip6tables
systemctl stop firewalld
systemctl start iptables
systemctl start ip6tables
于 2014-08-04T03:23:41.600 回答
18

试试下面的命令iptables-save

于 2014-09-11T14:30:19.270 回答
5

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

IP6TABLES_SAVE_ON_STOP="no"

至:

IP6TABLES_SAVE_ON_STOP="yes"

和这个:

IP6TABLES_SAVE_ON_RESTART="no"

至:

IP6TABLES_SAVE_ON_RESTART="yes"

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

于 2014-10-07T22:24:32.190 回答
1

将IPtables配置放在传统文件中,开机后会加载:

/etc/sysconfig/iptables

于 2014-11-22T08:40:06.337 回答
1

上个月我尝试在 LXC VM 容器上配置 iptables,但每次重启后 iptables 配置都不会自动加载。

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

yum -y 安装 iptables 服务;systemctl 禁用防火墙;systemctl 掩码防火墙;服务 iptables 重启;服务 iptables 保存

于 2017-04-05T06:53:45.177 回答
0

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

    systemctl start ip6tables.service
    systemctl enable ip6tables.service
于 2015-04-06T04:33:34.303 回答
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/

于 2015-11-13T16:54:03.723 回答