Linux系统上的防火墙体系
系统服务:firewalld
管理工具:firewall-cmd
图形管理工具firewall-confightml
预设安全区域
根据所在的网络场所区分,预设保护规则集。
最经常使用的四个区域:
– public:仅容许访问本机的sshd dhcp ping
– trusted:容许任何访问
– block:拒绝任何来访请求(明确拒绝回应)
– drop:丢弃任何来访的数据包(直接丢弃不给回应,节省服务器资源)安全
防火墙的断定规则: 匹配及中止
1.查看请求数据包中的源IP地址,并与本身全部区域规则依次比较,若是有该源IP地址的规则,则进服务器
入该区域。剩余其它区域再也不比对,数据包进入该区域后具体的处理方法依据该区域的具体规则。
2.若是全部区域都没有该源IP地址,则把数据包交给默认区域(public).网络
firewalld的安装ssh
[root@server1 ~]# rpm -q firewalld package firewalld is not installed [root@server1 ~]# yum -y install firewalld-0.4.4.4-14.el7.noarch [root@server1 ~]# systemctl start firewalld [root@server1 ~]# systemctl enable firewalld
防火墙默认区域修改curl
#查看默认区域 [root@server1 ~]# firewall-cmd --get-default-zone [root@client1 ~]# ping 192.168.4.100 #客户端client1能够Ping通 #把默认区别修改成block [root@server1 ~]# firewall-cmd --set-default-zone=block [root@server1 ~]# firewall-cmd --get-default-zone [root@server1 ~]#
[root@client1 ~]# ping 192.168.4.100 #client1不能ping通,但有回应
PING 192.168.4.100 (192.168.4.100) 56(84) bytes of data.
From 192.168.4.100 icmp_seq=1 Destination Host Prohibited
From 192.168.4.100 icmp_seq=2 Destination Host Prohibitedtcp
#修改默认区域为drop
[root@server1 ~]# firewall-cmd --set-default-zone=drop
[root@server1 ~]# firewall-cmd --get-default-zone
[root@server1 ~]# 工具
#客户端测试时,不能够通讯,没有回应
[root@client1 ~]# ping 192.168.4.100
PING 192.168.4.100 (192.168.4.100) 56(84) bytes of data.测试
在默认区域添加协议:url
#修改默认区域为public [root@server1 ~]# firewall-cmd --set-default-zone=public [root@server1 ~]# firewall-cmd --get-default-zone #查看区域详细规则 [root@server1 ~]# firewall-cmd --zone=public --list-all public (active) target: default icmp-block-inversion: no interfaces: eth0 eth1 eth2 eth3 sources: services: ssh dhcpv6-client ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: [root@server1 ~]# 在客户端上访问server1上的ftp,httpd服务 [root@client1 ~]# ftp 192.168.4.100 #ftp服务链接失败 ftp: connect: No route to host #httpd服务链接失败 [root@client1 ~]# curl http://192.168.4.100
curl: (7) Failed connect to 192.168.4.100:80; No route to host #在server1上添加规则,容许http,ftp [root@server1 html]# firewall-cmd --zone=public --add-service=http [root@server1 html]# firewall-cmd --zone=public --add-service=ftp [root@server1 html]# firewall-cmd --zone=public --list-all public (active) target: default icmp-block-inversion: no interfaces: eth0 eth1 eth2 eth3 sources: services: ssh dhcpv6-client http ftp ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: 客户端再次测试 [root@client1 ~]# curl http://192.168.4.100
hello~ #http链接成功 [root@client1 ~]# ftp 192.168.4.100 #ftp链接成功 Connected to 192.168.4.100 (192.168.4.100). #以上修改策略能够当即生效,但重启系统或服务时会丢失 [root@server1 ~]# firewall-cmd --reload [root@server1 ~]# firewall-cmd --zone=public --list-all public (active) target: default icmp-block-inversion: no interfaces: eth0 eth1 eth2 eth3 sources: services: ssh dhcpv6-client ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: [root@server1 ~]#
防火墙永久策略 permanent
#经过permanent使用策略写入到配置文件 [root@server1 ~]# firewall-cmd --permanent --zone=public --add-service=http [root@server1 ~]# firewall-cmd --permanent --zone=public --add-service=ftp #再次查看规则,由于策略被写于到了配置文件,因此当前策略中看不到 [root@server1 ~]# firewall-cmd --zone=public --list-all public (active) target: default icmp-block-inversion: no interfaces: eth0 eth1 eth2 eth3 sources: services: ssh dhcpv6-client ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: [root@server1 ~]# firewall-cmd --reload #从新加载防火墙全部配置,使配置文件中的策略生效 [root@server1 ~]# firewall-cmd --zone=public --list-all public (active) target: default icmp-block-inversion: no interfaces: eth0 eth1 eth2 eth3 sources: services: ssh dhcpv6-client http ftp ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: [root@server1 ~]#
单独拒绝虚拟机client1的访问
[root@server1 ~]# firewall-cmd --zone=block --add-source=192.168.4.1 [root@server1 ~]# firewall-cmd --zone=block --list-all block (active) target: %%REJECT%% icmp-block-inversion: no interfaces: sources: 192.168.4.1 services: ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: [root@server1 ~]# [root@client1 ~]# ftp 192.168.4.100 #客户端再次访问时会被拒绝 ftp: connect: No route to host #此方法是临时修改,当即生效,若是须要恢复 #firewall-cmd --reload
实现本机的端口映射
本地应用的端口重定向(端口1 --> 端口2)
从客户机访问 端口1 的请求,自动映射到本机 端口2
好比,访问如下两个地址能够看到相同的页面:
http://192.168.4.100:5423 ---> http://192.168.4.100:80
当client1访问server1的5423,用防火墙把端口重定义为80
[root@server1 ~]# firewall-cmd --permanent --zone=public --add-forward-port=port=5423:proto=tcp:toport=80 [root@server1 ~]# firewall-cmd --reload [root@server1 ~]# firewall-cmd --zone=public --list-all public (active) target: default icmp-block-inversion: no interfaces: eth0 eth1 eth2 eth3 sources: services: ssh dhcpv6-client http ftp ports: protocols: masquerade: no forward-ports: port=5423:proto=tcp:toport=80:toaddr= source-ports: icmp-blocks: rich rules: [root@server1 ~]# #在客户端访问5423端口时会被防火墙映射到80 [root@client1 ~]# curl http://192.168.4.100:5423 [root@client1 ~]#