Centos7防火墙配置firewalld过程与iptables nat 应用

管理防火墙的两种方式:firewalld与iptables

(1)firewalld  管理火墙的工具,相对简单 -->windows

(2)iptables  复杂,功能强大  -->route

(1)、(2)不能同时起作用

一、firewalld相关配置

(一)图形化管理

打开图形化管理火墙界面,并打入后台不影响终端使用

[[email protected] ~]# firewall-config &

##runtime 临时生效,立即生效

##permanent 更改配置文件,永久的,重新加载生效

动态监控配置火墙时的动态情况

[[email protected] ~]# watch -n 1 firewall-cmd --list-all

临时更改:在runtime下是临时生效,并且是立刻生效

(1)可以看到初始化支持的服务有dhcpv6-client ssh

 Centos7防火墙配置firewalld过程与iptables nat 应用

(2)在public区域下勾选http服务,可以看到已经立即生效

 Centos7防火墙配置firewalld过程与iptables nat 应用

其他服务类似,读者自行尝试

永久更改:permanent下,更改配置文件,重新加载后永久生效

(1)在public区域下勾选http服务,可以看到并没有生效

Centos7防火墙配置firewalld过程与iptables nat 应用

(2)重新加载

Centos7防火墙配置firewalld过程与iptables nat 应用

(3)加载后,可以看到已经生效

Centos7防火墙配置firewalld过程与iptables nat 应用

更改默认指定区域:修改默认区域

(1)一般情况下,默认的区域是public

(2)选择Change Default Zone

Centos7防火墙配置firewalld过程与iptables nat 应用

(3)以选择trusted为例,点击ok

Centos7防火墙配置firewalld过程与iptables nat 应用

(4)可以看到默认区域已经是trusted

 Centos7防火墙配置firewalld过程与iptables nat 应用

 

更改区域配置文件:通过直接更改指定区域的配置文件,并重启火墙服务,达到永久更改服务的目的

演示思路:安装httpd然后测试火墙服务

[[email protected] ~]# yum install httpd -y

为查看方便,这里编写标记

[[email protected] ~]# vim /var/www/html/index.html

 Centos7防火墙配置firewalld过程与iptables nat 应用

开启httpd

[[email protected] ~]# systemctl start httpd

测试:在浏览器中可以看到无法连接

 Centos7防火墙配置firewalld过程与iptables nat 应用

查看防火墙状态:发现没有服务中httpd

 Centos7防火墙配置firewalld过程与iptables nat 应用

可通过编写火墙默认的public区域配置文件,来永久开启服务

[[email protected] ~]# vim /etc/firewalld/zones/public.xml

在第行加入http服务: <service name="http"/>

 Centos7防火墙配置firewalld过程与iptables nat 应用

重启火墙

[[email protected] ~]# systemctl restart firewalld

再测试:

在浏览器中可以看到已经成功访问

Centos7防火墙配置firewalld过程与iptables nat 应用

 

再次查看防火墙状态:

Centos7防火墙配置firewalld过程与iptables nat 应用

 

(二)命令行管理firewalld

关于网络区域的配置:

 Centos7防火墙配置firewalld过程与iptables nat 应用

安装软件

[[email protected] ~]# yum install firewalld firewall-config -y

(1)常用命令

开启防火墙

[[email protected] ~]# systemctl start firewalld

开机自启防火墙

[[email protected] ~]# systemctl enable firewalld

关闭防火墙

[[email protected] ~]# systemctl stop firewalld

开机自动关闭防火墙

[[email protected] ~]# systemctl disable firewalld

查看防火墙状态

[[email protected] ~]# firewall-cmd --state

查看防火墙管理的设备

[[email protected] ~]# firewall-cmd --get-active-zones

查看防火墙生效的区域

[[email protected] ~]# firewall-cmd --get-default-zone

查看防火墙所有的区域

[[email protected] ~]# firewall-cmd --get-zones

列出关于public区域的服务设置

[[email protected] ~]# firewall-cmd --zone=public --list-all

列出关于trusted区域的服务设置

[[email protected] ~]# firewall-cmd --zone=trusted --list-all

列出可使用的服务

[[email protected] ~]# firewall-cmd --get-services

修改默认区域为trusted

[[email protected] ~]# firewall-cmd --set-default-zone=trusted

修改默认区域为public

[[email protected] ~]# firewall-cmd --set-default-zone=public

举例演示:

Centos7防火墙配置firewalld过程与iptables nat 应用

 

(2)高级配置

临时添加服务(默认的区域)

[[email protected] ~]# firewall-cmd --add-service=https

临时删除服务(默认的区域)

[[email protected] ~]# firewall-cmd --remove-service=https  

 

永久添加服务(默认的区域)

[[email protected] ~]# firewall-cmd --add-service=https  --permanent

永久删除服务(默认的区域)

[[email protected] ~]# firewall-cmd --remove-service=https  --permanent

 

临时添加端口(默认的区域)

[[email protected] ~]# firewall-cmd --add-port=8080/tcp

临时删除端口(默认的区域)

[[email protected] ~]# firewall-cmd --remove-port=8080/tcp

 

永久添加端口(默认的区域)

[[email protected] ~]# firewall-cmd --add-port=8080/tcp  --permanent

永久删除端口(默认的区域)

[[email protected] ~]# firewall-cmd --remove-port=8080/tcp  --permanent

 

添加接口(默认的区域)

[[email protected] ~]# firewall-cmd --add-interface=eth1

删除接口(默认的区域)

[[email protected] ~]# firewall-cmd --remove-interface=eth1

 

临时拒绝主机172.25.254.86的所有网络连接

[[email protected] ~]# firewall-cmd --add-source=172.25.254.86 --zone=block

永久拒绝主机172.25.254.86的所有网络连接

[[email protected] ~]# firewall-cmd --add-source=172.25.254.86 --zone=block --permanent

不中断连接,重启防火墙策略

[[email protected] ~]# firewall-cmd --reload

中断连接,重启防火墙策略

[[email protected] ~]# firewall-cmd --complete-reload

关闭上述

[[email protected] ~]# firewall-cmd --remove-source=172.25.254.86 --zone=block

[[email protected] ~]# firewall-cmd --remove-source=172.25.254.86 --zone=block --permanent

 

查看firewalld的服务相关配置文件

[[email protected] ~]# cd /usr/lib/firewalld/services/

 Centos7防火墙配置firewalld过程与iptables nat 应用

查看firewalld的区域相关配置文件

[[email protected] ~]# cd /etc/firewalld/zones/

Centos7防火墙配置firewalld过程与iptables nat 应用

 

临时:直接将eth1从原来的区域转到trusted这个区域

[[email protected] ~]# firewall-cmd --change-interface=eth1 --zone=trusted

[[email protected] zones]# firewall-cmd --permanent --change-interface=eth1 --zone=trusted

[[email protected] ~]# cd /etc/firewalld/zones/

[[email protected] zones]# ls

block.xml  public.xml  ROL.xml  trusted.xml

检验eth1是否在trusted这个区域写入配置

[[email protected] zones]# vim trusted.xml

Centos7防火墙配置firewalld过程与iptables nat 应用

永久:直接将eth1从原来的域转到trusted这个区域

[[email protected] zones]# firewall-cmd --permanent --change-interface=eth1 --zone=trusted

[[email protected] ~]# cd /etc/firewalld/zones/

[[email protected] zones]# ls

block.xml  public.xml  ROL.xml  trusted.xml

检验eth1已经在trusted这个区域

[[email protected] zones]# vim trusted.xml

Centos7防火墙配置firewalld过程与iptables nat 应用

 

[[email protected] zones]# firewall-cmd --permanent --change-interface=eth1 --zone=trusted

[[email protected] zones]# systemctl restart firewalld

查看:

Centos7防火墙配置firewalld过程与iptables nat 应用

确认两块网卡的IP

Centos7防火墙配置firewalld过程与iptables nat 应用

 

[[email protected] Desktop]# yum install links -y

[[email protected] Desktop]# links http://172.25.254.124

Centos7防火墙配置firewalld过程与iptables nat 应用

 

服务端:临时只拒绝主机86到本机的ssh服务

[[email protected] ~]# firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -s 172.25.254.86 -p tcp --dport 22 -j REJECT

客户端:可以看到lftp服务(lftp服务安装配置已经完成)仍然可以使用,只是ssh服务被拒绝

Centos7防火墙配置firewalld过程与iptables nat 应用

服务端:恢复主机86到本主机的ssh服务

方式一:[[email protected] ~]# firewall-cmd --direct --remove-rule ipv4 filter INPUT 1 -s 172.25.254.86 -p tcp --dport 22 -j REJECT

方式二:[[email protected] ~]# systemctl restart firewalld

客户端:检测

Centos7防火墙配置firewalld过程与iptables nat 应用

 

服务端:临时只允许主机86使用lftp服务

[[email protected] ~]# firewall-cmd --direct --add-rule ipv4 filter INPUT 1 ! -s 172.25.254.86 -p tcp --dport 21 -j REJECT

客户端:

Centos7防火墙配置firewalld过程与iptables nat 应用

 

 

伪装:当客户主机访问服务主机124的22端口时,会自动转到主机224的22端口,实现IP的伪装

具体步骤有二

(1)172.25.254.124的22端口转接172.25.254.224主机的22端口

[[email protected] ~]# firewall-cmd --add-forward-port=port=22:proto=tcp:toport=22:toaddr=172.25.254.224

(2)防火墙添加伪装

[[email protected] ~]# firewall-cmd --add-masquerade

查看防火墙

[[email protected] ~]# firewall-cmd --list-all

Centos7防火墙配置firewalld过程与iptables nat 应用

测试:可以看到转接成功

[[email protected] Desktop]# ssh [email protected]

Centos7防火墙配置firewalld过程与iptables nat 应用

取消转接:

[[email protected] ~]# firewall-cmd --remove-forward-port=port=22:proto=tcp:toport=22:toaddr=172.25.254.224

[[email protected] ~]# firewall-cmd --remove-masquerade

不再赘述。

注意:在打开firewalld的情况下打开iptables方式则firewalld方式自动关闭。

Centos7防火墙配置firewalld过程与iptables nat 应用

 

二、iptables的相关配置

(1)iptables的常用命令

[[email protected] ~]# yum install iptables-services.x86_64 -y

[[email protected] ~]# systemctl start iptables

[[email protected] ~]# systemctl enable iptables

[[email protected] ~]# systemctl stop iptables

[[email protected] ~]# systemctl disable iptables

 

(2)iptables的高级配置

关闭firewalld并且开机自动关闭

[[email protected] ~]# systemctl stop firewalld

[[email protected] ~]# systemctl disable firewalld

[[email protected] ~]# systemctl mask firewalld

开启iptables,并且开机自启动

[[email protected] ~]# yum install iptables -y

[[email protected] ~]# systemctl start iptables.service

[[email protected] ~]# systemctl enable iptables.service

列出指定filter表信息

[[email protected] ~]# iptables -t filter -nL

查看配置文件

[[email protected] ~]# vim /etc/sysconfig/iptables

刷新清空iptables

[[email protected] ~]# iptables -F

保存状态

[[email protected] ~]# service iptables save

列出默认表信息

[[email protected] ~]# iptables -nL

所有INPUT服务都丢弃并且不回应

[[email protected] ~]# iptables -P INPUT DROP

Centos7防火墙配置firewalld过程与iptables nat 应用

 

添加接受来自回环地址的

[[email protected] ~]# iptables -A INPUT -i lo -j ACCEPT

添加接受tcp协议80端口的服务

[[email protected] ~]# iptables -A INPUT -p tcp --dport 80  -j ACCEPT

添加接受tcp协议22端口的服务

[[email protected] ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT

拒绝全部的INPUT链服务

[[email protected] ~]# iptables -A INPUT -j REJECT

查看之,

[[email protected] ~]# iptables -nL

 Centos7防火墙配置firewalld过程与iptables nat 应用

1、配置链中的策略

 

删除指定链中的策略:

方法一:根据行数删除

[[email protected] ~]# iptables -D INPUT 3

如,删除INPUT链中的第三行

 Centos7防火墙配置firewalld过程与iptables nat 应用

直接效果立即生效

方法二:根据内容删除

如:删除指定策略内容

[[email protected] ~]# iptables -D INPUT -p tcp --dport 22 -j ACCEPT

 

插入指定链中的指定行

如,将接受协议为tcp的22端口插入到第三行

[[email protected] ~]# iptables -I INPUT 3 -p tcp --dport 22 -j ACCEPT

Centos7防火墙配置firewalld过程与iptables nat 应用

 

策略保存至配置文件

[[email protected] ~]# service iptables save

查看策略配置文件

[[email protected] ~]# cat /etc/sysconfig/iptables

验证效果:

Centos7防火墙配置firewalld过程与iptables nat 应用

 

允许主机86可以通过tcp协议22端口连接

[[email protected] Desktop]# iptables -A INPUT  -s 172.25.254.86 -p tcp --dport 22 -j ACCEPT

Centos7防火墙配置firewalld过程与iptables nat 应用

效果,其他主机无法连接

Centos7防火墙配置firewalld过程与iptables nat 应用

拉黑主机86通过tcp协议22端口连接的功能,其他主机不影响

[[email protected] Desktop]# iptables -A INPUT  ! -s 172.25.254.86 -p tcp --dport 22 -j ACCEPT

Centos7防火墙配置firewalld过程与iptables nat 应用

效果明显,不再截图。

2、关于链的相关配置

 iptables -N REDHAT             ##新建一个链,名为REDHAT

 iptables -E REDHAT Linux    ##修改指定链的名称,将REDHAT修改为Linux

 iptables -X Linux                  ##删除指定链,删除Linux

演示如下:

新建一个链,名为REDHAT

[[email protected] ~]# iptables -N REDHAT

[[email protected] ~]# iptables -nL

Centos7防火墙配置firewalld过程与iptables nat 应用

修改指定链的名称,将REDHAT修改为Linux

[[email protected] ~]# iptables -E REDHAT Linux

[[email protected] ~]# iptables -nL

Centos7防火墙配置firewalld过程与iptables nat 应用

删除指定链,删除Linux

[[email protected] ~]# iptables -X Linux

[[email protected] ~]# iptables -nL

Centos7防火墙配置firewalld过程与iptables nat 应用

 

3、防火墙策略优化

若是第一次访问可以,则第二次访问就不会再问 ,这就是优化状态。

注:系统重启会改变状态的数据包

状态:第一次连接:NEW 连接中:ESTABLISHED  连接过的:RELATED

[[email protected] ~]# iptables -F                      ##刷新清空策略

[[email protected] ~]# service iptables  save     ##保存

[[email protected] ~]# iptables -nL                    ##查看

Centos7防火墙配置firewalld过程与iptables nat 应用

添加接受状态为ESTABLISHED(连接中),RELATED(连接过)的策略

[[email protected] ~]# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEP

添加接受状态为NEW的来自回环地址策略

[[email protected] ~]# iptables -A INPUT -m state --state NEW -i lo -j ACCEPT

添加接受状态为NEW的tcp协议22端口策略

[[email protected] ~]# iptables -A INPUT -m state --state NEW -p tcp --dport 22 -j ACCEPT

添加接受状态为NEW的tcp协议80端口策略

[[email protected] ~]# iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT

添加拒绝全部的策略

[[email protected] ~]# iptables -A INPUT -j REJECT

可以查看:

[[email protected] ~]# iptables -nL

 Centos7防火墙配置firewalld过程与iptables nat 应用

 

4、nat表

已知不同网段之间不能访问彼此通信,那又如何做到172.25.86.12访问172.25.254.86  ??

Centos7防火墙配置firewalld过程与iptables nat 应用

主体思想:172.25.86.12来到eth1网络,进行伪装后,从eth0网络出去,也便和172.25.254.86成了统一网段,也就可以访问通信。

一起来做吧:

服务主机:双网卡

Centos7防火墙配置firewalld过程与iptables nat 应用

列出指定nat表信息

[[email protected] ~]# iptables -t nat -nL

Centos7防火墙配置firewalld过程与iptables nat 应用

步骤如下:

[[email protected] ~]# iptables -t nat -A POSTROUTING -o eht0 -j SNAT --to-source 172.25.254.124

[[email protected] ~]# sysctl -a | grep ip_forward

Centos7防火墙配置firewalld过程与iptables nat 应用

[[email protected] ~]# vim /etc/sysctl.conf

Centos7防火墙配置firewalld过程与iptables nat 应用

[[email protected] ~]# sysctl -p     ##生效

Centos7防火墙配置firewalld过程与iptables nat 应用

[[email protected] ~]# iptables -t nat -A PREROUTING-ieth0 -d 172.25.254.124 -j DNAT --to-dest 172.25.86.124

主机172.25.86.124:

(1)设置网关

需要将172.25.86.124设置为自己的网关

Centos7防火墙配置firewalld过程与iptables nat 应用

(2)测试成功!!!

Centos7防火墙配置firewalld过程与iptables nat 应用

附录:

关于iptables的参数:

-t 指定表

-L 列出表

-A  永远加在最后一条

-D  删除某一条链里面的策略

-i   输入  (-i lo 表示是从回环地址进来的)

-o  输出

-I   插入到某一条,默认第一条(-I INPUT 3 插入到第三行,不写3的话默认添加到第一个)

-m  状态

-j   动作(ACCEPT | REJECT)

-s  资源(无,则所有)

-P  策略

-p  协议

-R  更改

--dport 端口

-N  新建一个链

-E  修改链名称

-X  删除链