CentOS7中firewall的使用

2018-05-08  本文已影响17人  VoidKing

firewall简介

根据官方文档,firewall简介如下:

firewalld daemon provides a dynamically managed firewall with support for network “zones” to assign a level of trust to a network and its associated connections and interfaces. It has support for IPv4 and IPv6 firewall settings. It supports Ethernet bridges and IP set and has a separation of runtime and permanent configuration options. It also has an interface for services or applications to add firewall rules directly. The complete communication with firewalld is done using D-Bus.

firewalld守护进程提供了一个动态管理的防火墙,支持网络“区域”为网络及其关联的连接和接口分配信任级别。它支持ipv4和ipv6防火墙设置。它支持以太网桥和ip集,并且有运行时和永久配置选项的分离。它还具有用于直接添加防火墙规则的服务或应用程序的界面。与firewalld的完整通信是使用d-bus完成的。

简单来说,CentOS6中使用iptables作为防火墙;而在CentOS7中,firewall作为防火墙,是iptables的升级。本文只讨论自己常用的firewall命令和配置,不涉及高级用法。

firewall常用命令

firewall配置文件

firewall的配置文件是xml格式的,存储在/usr/lib/firewalld/ 和 /etc/firewalld/ 两个目录下。

这样就有很大的灵活性,因为文件可以被编辑,写入,备份,用作其他安装的模板等等。

1、系统配置目录
ll /usr/lib/firewalld/services

目录中存放定义好的网络服务和端口参数,系统参数,不能修改。

2、用户配置目录
ll /etc/firewalld/

3、自定义添加端口
常用命令中,我们知道用户可以通过命令的方式添加端口,添加的端口会写入/etc/firewalld 目录下的相应配置文件中。
用户也可以通过直接修改配置文件的方式添加端口,比如添加8080端口。

vim /etc/firewalld/zones/public.xml,添加内容:

<port protocol="tcp" port="8080"/>

重启firewalld,即可生效。

4、public.xml文件中还可以配置放行的ip范围、端口范围等等,在此不作记录,需要的时候再说。

使用iptables

很多同学在使用时,喜欢使用iptables代替firewall,毕竟iptables用习惯了。
1、关闭firewall,禁止开机启动
systemctl stop firewalld
systemctl disable firewalld

2、安装iptables防火墙
yum install iptables-services

3、编辑iptables防火墙配置文件
vi /etc/sysconfig/iptables
添加开放22、80、3306端口:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

4、启动iptables,设置开机启动
systemctl start iptables
systemctl enable iptables

5、iptables常用命令

书签

CentOS 7中firewall防火墙详解和配置以及切换为iptables防火墙

CentOS7使用firewalld打开关闭防火墙与端口

CentOS 7 开放3306端口访问

上一篇 下一篇

猜你喜欢

热点阅读