Fix “Unit iptables.service failed to load: No such file or directory” Error In CentOS7

最近在升级CentOS7遇到问题

systemctl restart iptables.service
Failed to issue method call: Unit iptables.service failed to load: No such file or directory.
iptables-failThis simple means you do not have iptables-services package installed.

解决

yum install iptables-services

Re-run command to restart iptables and it should pass successfully.

systemctl restart iptables.service
iptables_passed

另外CentOS7 的默认防火墙是firewalld


systemctl disable firewalld.service#禁止firewall开机启动

然后

systemctl enable iptables.serivce
systemctl start iptables.serivce<br></br><br></br>

CentOS7默认的防火墙不是iptables,而是firewalle.

安装iptable iptable-service

DAY 157 关于centos防火墙问题教程

#先检查是否安装了iptables
service iptables status
#安装iptables
yum install -y iptables
#升级iptables
yum update iptables 
#安装iptables-services
yum install iptables-services

DAY 157 关于centos防火墙问题教程

禁用/停止自带的firewalld服务

#停止firewalld服务
systemctl stop firewalld
#禁用firewalld服务
systemctl mask firewalld

设置现有规则

DAY 157 关于centos防火墙问题教程

#查看iptables现有规则
iptables -L -n
#先允许所有,不然有可能会杯具
iptables -P INPUT ACCEPT
#清空所有默认规则
iptables -F
#清空所有自定义规则
iptables -X
#所有计数器归0
iptables -Z
#允许来自于lo接口的数据包(本地访问)
iptables -A INPUT -i lo -j ACCEPT
#开放22端口
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
#开放21端口(FTP)
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
#开放80端口(HTTP)
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
#开放443端口(HTTPS)
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
#允许ping
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
#允许接受本机请求之后的返回数据 RELATED,是为FTP设置的
iptables -A INPUT -m state --state  RELATED,ESTABLISHED -j ACCEPT
#其他入站一律丢弃
iptables -P INPUT DROP
#所有出站一律绿灯
iptables -P OUTPUT ACCEPT
#所有转发一律丢弃
iptables -P FORWARD DROP

DAY 157 关于centos防火墙问题教程

其他规则设定

DAY 157 关于centos防火墙问题教程

#如果要添加内网ip信任(接受其所有TCP请求)
iptables -A INPUT -p tcp -s 45.96.174.68 -j ACCEPT
#过滤所有非以上规则的请求
iptables -P INPUT DROP
#要封停一个IP,使用下面这条命令:
iptables -I INPUT -s ***.***.***.*** -j DROP
#要解封一个IP,使用下面这条命令:
iptables -D INPUT -s ***.***.***.*** -j DROP

DAY 157 关于centos防火墙问题教程

保存规则设定

#保存上述规则
service iptables save

开启iptables服务

DAY 157 关于centos防火墙问题教程

#注册iptables服务
#相当于以前的chkconfig iptables on
systemctl enable iptables.service
#开启服务
systemctl start iptables.service
#查看状态
systemctl status iptables.service

DAY 157 关于centos防火墙问题教程

解决vsftpd在iptables开启后,无法使用被动模式的问题

1.首先在/etc/sysconfig/iptables-config中修改或者添加以下内容

#添加以下内容,注意顺序不能调换
IPTABLES_MODULES="ip_conntrack_ftp"
IPTABLES_MODULES="ip_nat_ftp"

2.重新设置iptables设置

iptables -A INPUT -m state --state  <strong>RELATED,</strong>ESTABLISHED -j ACCEPT

以下为完整设置脚本

DAY 157 关于centos防火墙问题教程

#!/bin/sh
iptables -P INPUT ACCEPT
iptables -F
iptables -X
iptables -Z
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
service iptables save
systemctl restart iptables.service

DAY 157 关于centos防火墙问题教程

Linux关闭防火墙命令

1) 永久性生效,重启后不会复原
开启:chkconfig iptables on
关闭:chkconfig iptables off

2) 即时生效,重启后复原
开启:service iptables start
关闭:service iptables stop

3)在开启了防火墙时,做如下设置,开启相关端口,
修改/etc/sysconfig/iptables 文件,添加以下内容:
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT

实际上centos7以后默认防火墙为firewalld了,所以前面的iptables已经不再有用,

学习apache安装的时候需要打开80端口,由于centos 7版本以后默认使用firewalld后,网上关于iptables的设置方法已经不管用了,想着反正iptable也不会用,索性直接搬官方文档,学习firewalld了,好像比iptables要简单点了。

官方文档地址:https://access.redhat.com/documentation/en-US/Red\_Hat\_Enterprise\_Linux/7/html/Security\_Guide/sec-Using\_Firewalls.html#sec-Introduction\_to\_firewalld

1、firewalld简介

firewalld是centos7的一大特性,最大的好处有两个:支持动态更新,不用重启服务;第二个就是加入了防火墙的“zone”概念

firewalld有图形界面和工具界面,由于我在服务器上使用,图形界面请参照官方文档,本文以字符界面做介绍

firewalld的字符界面管理工具是 firewall-cmd

firewalld默认配置文件有两个:/usr/lib/firewalld/ (系统配置,尽量不要修改)和 /etc/firewalld/ (用户配置地址)

zone概念:

硬件防火墙默认一般有三个区,firewalld引入这一概念系统默认存在以下区域(根据文档自己理解,如果有误请指正):

drop:默认丢弃所有包

block:拒绝所有外部连接,允许内部发起的连接

public:指定外部连接可以进入

external:这个不太明白,功能上和上面相同,允许指定的外部连接

dmz:和硬件防火墙一样,受限制的公共连接可以进入

work:工作区,概念和workgoup一样,也是指定的外部连接允许

home:类似家庭组

internal:信任所有连接

对防火墙不算太熟悉,还没想明白public、external、dmz、work、home从功能上都需要自定义允许连接,具体使用上的区别还需高人指点

2、安装firewalld

root执行 \# yum install firewalld firewall-config

3、运行、停止、禁用firewalld

启动:\# systemctl start firewalld

查看状态:\# systemctl status firewalld 或者 firewall-cmd --state

停止:\# systemctl disable firewalld

禁用:\# systemctl stop firewalld

4、配置firewalld

查看版本:$ firewall-cmd --version

查看帮助:$ firewall-cmd --help

查看设置:

显示状态:$ firewall-cmd --state

查看区域信息: $ firewall-cmd --get-active-zones

查看指定接口所属区域:$ firewall-cmd --get-zone-of-interface=eth0

拒绝所有包:\# firewall-cmd --panic-on

取消拒绝状态:\# firewall-cmd --panic-off

查看是否拒绝:$ firewall-cmd --query-panic

更新防火墙规则:\# firewall-cmd --reload

\# firewall-cmd --complete-reload

两者的区别就是第一个无需断开连接,就是firewalld特性之一动态添加规则,第二个需要断开连接,类似重启服务

将接口添加到区域,默认接口都在public

\# firewall-cmd --zone=public --add-interface=eth0

永久生效再加上 --permanent 然后reload防火墙

设置默认接口区域

\# firewall-cmd --set-default-zone=public

立即生效无需重启

打开端口(貌似这个才最常用)

查看所有打开的端口:

\# firewall-cmd --zone=dmz --list-ports

加入一个端口到区域:

\# firewall-cmd --zone=dmz --add-port=8080/tcp

若要永久生效方法同上

打开一个服务,类似于将端口可视化,服务需要在配置文件中添加,/etc/firewalld 目录下有services文件夹,这个不详细说了,详情参考文档

\# firewall-cmd --zone=work --add-service=smtp

移除服务

\# firewall-cmd --zone=work --remove-service=smtp

标签: firewalld, centos, ACCEPT, INPUT, iptables, firewall, DAY

相关文章推荐

添加新评论,含*的栏目为必填