原文链接:https://linuxize.com/post/how-to-install-iptables-on-centos-7/
原文作者:Linuxize
从 CentOS 7 开始,firewalld 取代了 iptables 作为默认的防火墙管理工具。
FirewallD 是一个完整的防火墙解决方案,可以通过一个名为firewall-cmd的命令行工具来控制。如果您更熟悉Iptables命令行语法,那么您可以禁用FirewallD并退回到经典的iptables设定。
本教程将向您展示如何禁用FirewallD服务并安装iptables。
先决条件
在开始本教程之前,您需要确保已登录带有 sudo 权限的用户
禁用FirewallD
在CentOS 7系统上禁用FirewallD,需要以下几步:
- 输入下面的命令,停止FirewallD服务:
1
| $ sudo systemctl stop firewalld
|
- 禁用FirewallD服务,以免开机自动启动:
1
| $ sudo systemctl disable firewalld
|
- 屏蔽FirewallD服务,防止它被其他服务启动:
1
| $ sudo systemctl mask --now firewalld
|
安装并启用Iptables
在CentOS 7系统中,安装Iptables的方法如下:
- 运行以下命令,安装CentOS存储库中的
iptables-service 包:
1
| $ sudo yum install iptables-services
|
- 安装完成后,启动Iptables服务:
1 2
| $ sudo systemctl start iptables $ sudo systemctl start ip6tables
|
- 启用Iptables服务,使其在系统启动时自动启动:
1 2
| $ sudo systemctl enable iptables $ sudo systemctl enable ip6tables
|
- 用下面的命令检查Iptables服务:
1 2
| $ sudo systemctl status iptables $ sudo systemctl status ip6tables
|
- 使用下面的命令,检查当前iptables规则:
1 2
| $ sudo iptables -nvL $ sudo ip6tables -nvL
|
默认情况下,只有SSH端口22是开放的,输出应该如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 5400 6736K ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 2 148 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 3 180 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT 4298 packets, 295K bytes) pkts bytes target prot opt in out source destination
|
至此,您已经成功地启用了iptables服务,您可以构建您的防火墙了。这些变更将在机器重启后持续存在。
结论
在本教程中,您学习了如何禁用FirewallD服务和安装iptables。
如果您有任何问题或评论,请在下方留言。
译者注:
这两个服务确实不常用,有兴趣的墨友可以试试看:
1 2 3 4 5 6 7 8 9 10 11 12
| [shawnyan@centos7 ~]$ systemctl status iptables.service ● iptables.service - IPv4 firewall with iptables Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled) Active: inactive (dead) since Tue 2022-03-29 15:10:21 JST; 20h ago Process: 76583 ExecStop=/usr/libexec/iptables/iptables.init stop (code=exited, status=0/SUCCESS) Process: 76560 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS) Main PID: 76560 (code=exited, status=0/SUCCESS)
[shawnyan@centos7 ~]$ systemctl status ip6tables.service ● ip6tables.service - IPv6 firewall with ip6tables Loaded: loaded (/usr/lib/systemd/system/ip6tables.service; disabled; vendor preset: disabled) Active: inactive (dead)
|
https://www.modb.pro/db/387746
Author:
Shawn Yan
Link:
https://shawnyan.cn/2022/modb/fanyi-centos-7-install-iptables/index.html
License:
All articles on this site are original unless otherwise stated. Please indicate the source when reprinting!