目录
想使用iptables来开放8080端口的时候,设定完之后,想保存规则,发生错误Failed to start ipatbles.service: Unit not found.原因是centos从7开始默认用的是firewalld,这个是基于iptables的,虽然有iptables的核心,但是iptables的服务是没安装的。
centos从7开始默认用的是firewalld,这个是基于iptables的,虽然有iptables的核心,但iptables的服务是没安装的。所以你停止firewalld服务且安装iptables服务。或者不用iptables服务,用系统自带的firewalld服务管理防火墙。
此文只介绍如何停用firewalld服务,启用iptables服务来管理防火墙!
一、停止firewalld服务
停止防火墙,并禁用这个服务
[root@oracle ~]# sudo systemctl stop firewalld.service #停止firewall
[root@oracle ~]# sudo systemctl disable firewalld.service #禁止firewall开机启动
二、安装iptables
如果你要改用iptables的话,需要安装iptables服务
1、安装
[root@oracle ~]# sudo yum -y install iptables-services #安装
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.cn99.com
* extras: mirrors.cn99.com
* updates: mirrors.cn99.com
base | 3.6 kB 00:00:00
docker-ce-stable | 2.9 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
正在解决依赖关系
--> 正在检查事务
---> 软件包 iptables-services.x86_64.0.1.4.21-24.1.el7_5 将被 安装
--> 解决依赖关系完成
依赖关系解决
============================================================================================================
Package 架构 版本 源 大小
============================================================================================================
正在安装:
iptables-services x86_64 1.4.21-24.1.el7_5 updates 51 k
事务概要
============================================================================================================
安装 1 软件包
总下载量:51 k
安装大小:25 k
Is this ok [y/d/N]: y
Downloading packages:
iptables-services-1.4.21-24.1.el7_5.x86_64.rpm | 51 kB 00:00:02
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : iptables-services-1.4.21-24.1.el7_5.x86_64 1/1
验证中 : iptables-services-1.4.21-24.1.el7_5.x86_64 1/1
已安装:
iptables-services.x86_64 0:1.4.21-24.1.el7_5
完毕!
[root@oracle ~]#
3、开启iptables
[root@oracle ~]# systemctl enable iptables #启用
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@oracle ~]# service iptables save #保存
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ 确定 ]
[root@oracle ~]# service iptables status #查看状态
Redirecting to /bin/systemctl status iptables.service
● iptables.service - IPv4 firewall with iptables
Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor preset: disabled)
Active: inactive (dead)
[root@oracle ~]# service iptables start #启动
Redirecting to /bin/systemctl start iptables.service
[root@oracle ~]# service iptables status #查看状态
Redirecting to /bin/systemctl status iptables.service
● iptables.service - IPv4 firewall with iptables
Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor preset: disabled)
Active: active (exited) since 四 2018-07-26 11:16:36 CST; 16s ago
Process: 11079 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS)
Main PID: 11079 (code=exited, status=0/SUCCESS)
7月 26 11:16:36 oracle systemd[1]: Starting IPv4 firewall with iptables...
7月 26 11:16:36 oracle iptables.init[11079]: iptables: Applying firewall rules: [ 确定 ]
7月 26 11:16:36 oracle systemd[1]: Started IPv4 firewall with iptables.
[root@oracle ~]#
三、常用命令
service iptables save #保存规则到iptables
service iptables start #启动
service iptables restart #重新启动
service iptables restop #停止
service iptables status #查看状态