防火墙是一种位于内部网络与外部网络之间的网络安全系统。一项信息安全的防御系统,依照特定的规则,容许或是限制传输的数据经过。防火墙一般工做在网络层,也即IPv4或IPv6的IP包上。web
是否容许包经过防火墙,取决于防火墙配置的规则。这些规则既能够是内建的,也能够是用户自定义的。每个包要进出防火墙,均须要知足防火墙配置的规则。centos
每一条规则均有一个目标动做,具备相同动做的规则能够分组在一块儿。对于Linux系统,最经常使用的防火墙有:FirewallD或iptables。Linux的发行版种类极多,可是公认的仍然是这两种。安全
FirewallD即Dynamic Firewall Manager of Linux systems,Linux系统的动态防火墙管理器。FirewallD是一个服务,用于配置网络链接,从而哪些内外部网络的数据包能够容许穿过网络或阻止穿过网络。网络
FirewallD容许两种类型的配置:永久类型和运行时类型。运行时类型的配置在防火墙被重启后会丢失相应的规则配置;而永久类型的配置即便遇到系统重启,也会保留生效。svg
对应于上面两种类型的配置,FirewallD相应的有两个目录:针对运行时类型配置的目录/usr/lib/firewall;以及针对永久类型配置的目录/etc/firewall.在RHEL/CentOS 7或Fedora 18的默认服务能够看到。工具
防火墙栈的总体图以下:
.net
iptables是另外一种服务,它能够决定是否容许、删除或返回IP数据包。iptables服务管理IPv4数据包,而ip6tables则管理IPv6数据包。此服务管理了一堆规则表,其中每一个表分别用于维护不一样的目的,好比过滤表(filter table)为防火墙规则,NAT表供新链接查询使用,mangle表用于数据包的转换等。命令行
更进一步,每一个表还具备规则链,规则链能够是内建的或是用户自定义的,它表示适用于一个数据包的规则集合,从而决定数据包应该执行哪些目标动做,好比容许ALLOWED、阻塞BLOCKED或返回RETURNED。iptables服务在RHEL/CentOS 6/五、Fedora、ArchLinux、Ubuntu等Linux发行版中是系统默认的服务。code
对于CentOS/RHEL 7或Fedora 18以上版本的系统,要管理FirewallD服务,使用以下命令。xml
启动FirewallD服务
# systemctl firewalld start
中止FirewallD服务
# systemctl firewalld stop
检查FirewallD服务的状态
# systemctl status firewalld
检查FirewallD服务的状态
# firewall-cmd --state
可能会返回running,表示正在运行。
还能够禁用FirewallD服务,关闭那些规则。
禁用FirewallD服务
# systemctl disable firewalld
启用FirewallD服务
# systemctl enable firewalld
屏蔽FirewallD服务
# systemctl mask firewalld
还能够经过建立一个firewall.service到/dev/null的符号链接来屏蔽防火墙服务。
反屏蔽FirewallD服务
# systemctl unmask firewalld
这是反屏蔽FirewallD服务,它会移除屏蔽FirewallD服务时建立的符号连接,故能从新启用服务。
检查是否已安装防火墙
# yum install firewalld firewall-config
在RHEL/CentOS 6/5/4系统和Fedora 12-18系统中,iptables是默认的防火墙,若是服务不存在,能够这样安装:
# yum install iptables-services
而后就能够对iptables服务进行启动、中止、重启等操做了。
启动iptables服务
# systemctl start iptables
或
# service iptables start
中止iptables服务
# systemctl stop iptables
或
# service iptables stop
禁用iptables服务
# systemctl disable iptables
或
# service iptables save # service iptables stop
启用iptables服务
# systemctl enable iptables
或
# service iptables start
检查iptables服务的状态
# systemctl status iptables
或
# service iptables status
在Ubuntu及其它Linux发行版中,ufw是用于管理iptables防火墙服务的工具。ufw提供了一个简易的界面让用户能够很方便的处理iptables防火墙服务。
启用ufw iptables防火墙服务
$ sudo ufw enable
禁用ufw iptables防火墙服务
$ sudo ufw disable
检查ufw iptables防火墙服务的状态
$ sudo ufw status
可是,若是想列出iptables包含的全部规则链列表,应使用以下命令:
$ iptables -L -n -v
在CentOS/RHEL 7系统中,基于用户对网络中设备和通讯所给与的信任程度,防火墙可用于将网络划分红不一样的区域,区域类型以下:
对于区域的修改,可以使用网络管理器NetworkManager搞定。
FirewallD包含了一个名为直接接口(direct interface)的概念,意思是能够直接经过iptables、ip6tables和ebtables的规则。直接接口适用于应用程序,不适用于用户。若是不熟悉iptables,那么使用直接接口是很危险的,由于可能会致使防火墙被入侵。
FirewallD保持对所增长规则项的追踪,因此能质询FirewallD,发现由使用直接端口模式的程序形成的更改。要使用直接端口,增长–direct选项到firewall-cmd命令来使用。
在CentOS/RHEL 7系统中,要用iptables和ip6tables服务代替FirewallD服务,须要以root身份运行如下命令,先禁用FirewallD:
# systemctl disable firewalld # systemctl stop firewalld
而后安装iptables-services程序包,以root身份输入如下命令:
# yum install iptables-services
iptables-services程序包包含了iptables和ip6tables服务。而后,以root身份运行iptables和ip6tables命令:
# systemctl start iptables # systemctl start ip6tables # systemctl enable iptables # systemctl enable ip6tables
用命令行启动图形化防火墙配置工具,则以root用户身份输入如下命令:
# firewall-config
能够看到以下界面: