CentOS7中,系统自带的netfilter操做程序由iptables变为firewalld。firewall有zone和service两个概念,网口或者说nmcli下的conection可加入某个zone那么这个con来的数据就会遵循zone下的规则,而每一个zone下又有一些service,这些service不受zone默认规则的限制。能够经过con、zone、service的搭配来具体对某一业务进行规划,保证系统安全。安全
firewalld中有9个zone,各个zone的说明以下
drop
Any incoming network packets are dropped; there is no reply. Only outgoing network connections are possible.
block
Any incoming network connections are rejected with an icmp-host-prohibited message for IPv4 and icmp6-adm-prohibited for IPv6. Only network connections initiated from within the system are possible.
public
For use in public areas. You do not trust the other computers on the network to not harm your computer. Only selected incoming connections are accepted.
external
For use on external networks with masquerading enabled, especially for routers. You do not trust the other computers on the network to not harm your computer. Only selected incoming connections are accepted.
dmz
For computers in your demilitarized zone that are publicly-accessible with limited access to your internal network. Only selected incoming connections are accepted.
work
For use in work areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.
home
For use in home areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.
internal
For use on internal networks. You mostly trust the other computers on the networks to not harm your computer. Only selected incoming connections are accepted.
trusted
All network connections are accepted.网络
译文:
由firewalld 提供的区域按照从不信任到信任的顺序排序。
丢弃 drop
任何流入网络的包都被丢弃,不做出任何响应。只容许流出的网络链接。
阻塞 block
任何进入的网络链接都被拒绝,并返回 IPv4 的 icmp-host-prohibited 报文或者 IPv6 的 icmp6-adm-prohibited 报文。只容许由该系统初始化的网络链接。
公开 public
用以能够公开的部分。你认为网络中其余的计算机不可信而且可能伤害你的计算机。只容许选中的链接接入。
外部 external
用在路由器等启用假装的外部网络。你认为网络中其余的计算机不可信而且可能伤害你的计算机。只容许选中的链接接入。
隔离区dmz
用以容许隔离区(dmz)中的电脑有限地被外界网络访问。只接受被选中的链接。
工做 work
用在工做网络。你信任网络中的大多数计算机不会影响你的计算机。只接受被选中的链接。
家庭 home
用在家庭网络。你信任网络中的大多数计算机不会影响你的计算机。只接受被选中的链接。
内部 internal
用在内部网络。你信任网络中的大多数计算机不会影响你的计算机。只接受被选中的链接。
受信任的 trusted
容许全部网络链接。ssh
操做前,咱们看下当前系统中应用的是哪一种防火墙程序。ide
systemctl list-units --all --type=service |egrep 'firewalld|ip6tables|iptables'命令行
看active那一列,active的就是在用的程序,若是你看到firewalld那一行是inactive的,那咱们就用下列命令把他启动3d
systemctl start firewalld #
systemctl enable firewalld #router
若是iptables.service是active的那咱们也要把他停用xml
systemctl stop iptables
systemctl disable iptablesblog
都执行完后能够再重复第一条命令看下服务状态是否跟截图一致。排序
firewall命令有点像一个英语句子,好理解,可是输入有点烦
好比说
firewall-cmd --get-default-zone
firewall-cmd --set-default-zone=work
firewall-cmd --get-zone-interface=ens33
命令行操做
一、查看新加接口默认的zone
firewall-cmd --get-default-zone
二、设定新接口加入时的默认zone
firewall-cmd --set-default-zone=work
三、查看接口所在的zone
firewall-cmd --get-zone-of-interface=ens33
四、给指定的网卡设置zone
firewall-cmd --zone=dmz --d-interface=ens33
五、更改某个网卡的zone
firewall-cmd --zone=public --change-interface=ens33
firewall-cmd --get-active-zones
一、列出系统中存在的全部网络服务(好比http、shttp、ssh等等)
firewall-cmd --get-services
二、列出默认zone的service
firewall-cmd --list-service
三、列出特定zone的service
firewall-cmd --zone=trusted --list-service
四、将某个服务加入特定的zone
firewall-cmd --zone=work --add-service=ftp
相对的将某个service移出某个zone
firewall-cmd --zone=work --remove-service=ftp
五、将服务的改动写入配置文件重启后也生效
firewall-cmd --zone=work --add-service=ftp --permanent #就是在末尾加个permanent参数
firewalld的配置文件保存在,同时程序为咱们预备了zone控制的模板与service的模板,其中zone模板保存在/usr/lib/firewalld/zones/下,而service的模板保存在/usr/lib/firewalld/services/目录下,都是一些*.xml的文件。
模板路径 | 控制文件路径 | |
---|---|---|
zone | /usr/lib/firewalld/zones/ | /etc/firewalld/zones |
service | /usr/lib/firewalld/services/ | /etc/firewalld/services |
下面咱们经过一个例子说明如何用模板控制zone下的service
需求:将FTP service的默认端口改成1121,而且在work zone下放行
首先咱们将ftp的模板拷贝到控制文件目录中
cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services
而后把默认的21端口改成1121端口
sed -i 's/21/1121/g' ftp.xml
而后把work zone的模板拷贝到控制文件目录下
cp -v /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones #建议加个-v参数,方便检查操做正误
修改控制目录下的work.xml 把ftp服务加入白名单
(玩个花活^^)
sed -i '7i \ \ <service name="ftp"/>' work.xml
重载配置文件让添加的规则生效
firewall-cmd --reload #从新加载firewalld配置文件
firewall-cmd --zone=work --list-services #检查配置结果