防火墙的配置

firewalld 概述:

      动态防火墙后台程序 firewalld 提供了一个 动态管理的防火墙, 用以支持网络 “zones” , 以分配对一个网络及其相关链接和界面一定程度的信任。它具备对 IP v4 和 IP v6 防火墙设置的支持。它支持以太网桥, 并有分离运行时间和永久性配置选择。它还具备一个通向服务或者应用程序以直接增加防火墙规则的接口系统提供了图像化的配置工具 firewall-config 、 system-config-firewall, 提供命令行客户端 firewall-cmd, 用于配置 firewalld 永久性或非永久性运行时间的改变 : 它依次用iptables 工具与执行数据包筛选的内核中的 Netfilter 通信

firewalld 和 iptables service

最本质的不同是 :    iptables service 在 /etc/sysconfig/iptables 中储存配置

                             firewalld 将配置储存在 /usr/lib/firewalld/ 和/etc/firewalld/ 中的各种 XML 文件里 .

firewalld域:



firewalld

##实验只能在虚拟机的图形中操作,因为火墙会限制接口

1.启用firewalld

yum install firewalld firewall-config  -y

systemctl stop    iptables

systemctl disable iptables

systemctl start   firewalld

systemctl enable  firewalld

2.启用 iptables

yum install iptables-services -y

systemctl stop   firewalld

systemctl diable firewalld

systemctl start  iptables

systemctl enable iptables

3.firewalld管理方式:

 实验:

 ##先启用firewalld

 systemctl stop    iptables

 systemctl disable iptables

 systemctl start   firewalld

 systemctl enable  firewalld

 

 firewall-cmd --state                                          #查看火墙的状态


 firewall-cmd --get-active-zones                        #查看火墙当前默认生效(正在使用)的域


 firewall-cmd --get-default-zone                        #查看火墙的默认数据规则,默认为public


 firewall-cmd --get-zones                                   #查看所有可用的域


 firewall-cmd --zone=public --list-all                   #查看public域的信息


 firewall-cmd --zone=block --list-all                    #查看block域的信息  

 

 firewall-cmd --list-all-zones                               #查看所有域的信息


 





 firewall-cmd --list-all                                         #查看所有域的规则


 firewall-cmd --get-services                 #查看系统中可以用名称来表示的服务(用文件名称表示端口)


实验:

  yum install httpd -y

  systemctl start httpd

  echo westos > /var/www/html/index.html

测试:   (在真机中)       

 #无法访问,因为火墙没有允许


实验:

firewall-cmd --get-default-zone                             #为public

firewall-cmd --set-default-zone=trusted                #更改火墙的域为trusted,即所有访问都能通过

firewall-cmd --get-default-zone

测试:



4.火墙网络接口的设定

 ##做实验之前需要先添加eth1网卡

nm-connection-editor    直接手动添加

 实验:

 firewall-cmd --list-all                                      #为trusted

 firewall-cmd --set-default-zone=public          #改回public

 firewall-cmd --list-all                                      #只允许ssh

测试:


实验:

firewall-cmd --add-source=172.25.254.77 --zone=trusted    #添加此77主机为信任,即只允许77主机访问

测试:



##还原

firewall-cmd --remove-source=172.25.254.77     #移除此77主机

网卡所在域的更改,添加与删除:

 firewall-cmd --list-interfaces                                                     #查看系统中生效的网络接口


 firewall-cmd --get-zone-of-interface=eth1                               #查看eth1所在的域


 firewall-cmd --get-zone-of-interface=eth0                               #查看eth0所在的域


 firewall-cmd --change-interface=eth0 --zone=trusted              #改变eth0所在的域为trusted

 firewall-cmd --get-zone-of-interface=eth0                               #查看是否改变


 firewall-cmd --remove-interface=eth0 --zone=trusted  #从trusted域中移除eth0

 firewall-cmd --get-zone-of-interface=eth0                               #再次查看时,没有域  


 firewall-cmd --add-interface=eth0 --zone=public                     #添加eth0所在的域为public



5.火墙的文件管理

1)添加ip源的两种方式:

 ##临时性添加

 firewall-cmd --add-source=172.25.254.77                   #临时添加ip源,重启就会消失

 firewall-cmd --list-all                                                  #查看添加成功

 systemctl restart firewalld    

 firewall-cmd --list-all                                                  #source资源会消失


##永久性添加

 firewall-cmd --permanent --add-source=172.25.254.77           #永久添加ip源(更改了配置文件)

 firewall-cmd --reload                                                              #必须重新加载后才生效

 systemctl restart firewalld                                                       #此时即使重启火墙资源也不会消失     

 firewall-cmd --list-all


##永久添加,是通过自动写入/etc/firewalld/zones/public.xml 中实现的

 vim /etc/firewalld/zones/public.xml                 #可查看到刚添加的ip源,xml代表可扩展性标记语言


2)添加服务:(实际上是在文件中添加的80端口)

 实验:

 cd /etc/firewalld/zones/

 ls


 vim public.xml                           #永久修改public域的配置文件

########


 systemctl restart firewalld

 firewall-cmd --list-all                   #可查看到http


 cd /usr/lib/firewalld/

 ls


 cd services/

 vim http.xml                                                               #可查看到80端口


 firewall-cmd --add-port=8080/tcp --zone=public       #临时添加8080端口

 firewall-cmd --list-all


3)火墙对ssh服务的控制:

 ##临时移除

 先打开一个shell,ssh连接虚拟机

 firewall-cmd --remove-service=ssh                #临时移除ssh的访问权

 此时ssh不会断开,但再打开另一个shell,ssh连接虚拟机会失败

 firewall-cmd --reload                                    #重新加载(会重新加载文件)

 firewall-cmd --list-all                                     #ssh服务又会恢复


##永久性移除

 firewall-cmd --permanent --remove-service=ssh        #永久移除ssh的访问权

##刚ssh连接上的虚拟机并不会断开

 firewall-cmd --reload                                                 #重新加载

 firewall-cmd --list-all                                                  #ssh服务消失

##此时刚ssh连接上的虚拟机也不会断开,说明reload不会终端当前正在连接的信息


 firewall-cmd --complete-reload                                   #中断当前连接服务,并重新加载

 firewall-cmd --permanent --add-service=ssh               #永久添加ssh服务

 firewall-cmd --reload

 firewall-cmd --list-all



6.火墙的访问限制

实验:

 yum install httpd

 systemctl start httpd            #此时任何主机都可以访问,这样不安全,可以通过限定主机来保证安全

 firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -p tcp --dport 80 -s 172.25.254.77 -j ACCEPT  

具体参数的含义:

 --direct                     #添加direct规则

 filter                         #添加表

INPUT 1                     #添加第一条链

-p tcp                        #添加tcp协议

--dport 80                  #目的地端口为80

-s                               #设定被允许访问的ip

-j                               #要执行的动作,ACCEPT表示接受,DROP表示丢弃,不回显,REJECT表示拒绝,回显

测试:

77上:可以访问   


在其他主机上:拒绝访问




7.端口转发(外网访问内网)

  实验:

 firewall-cmd  --list-all

 firewall-cmd --permanent  --add-forward-port=port=22:proto=tcp:toport=22:toaddr=172.25.254.230   

#当访问130的22端口时,转向40的22端口上

具体参数的含义:

port=22 表示22端口(ssh的端口)   proto=tcp 表示tcp协议    

toport  表示将22端口转向22端口    toaddr 表示转向ip

firewall-cmd --reload                   #因为是永久添加,所以要重新加载

测试:

此时用真机连接此主机还不能转发到230这台主机上,因为marquerade服务没有开启


实验:

 firewall-cmd  --list-all


 firewall-cmd --permanent --add-masquerade      #永久性开启masquerade服务

 firewall-cmd --reload

 firewall-cmd  --list-all


测试:

此时用真机连接此主机可以转发到230这台主机上


8.地址伪装(内网访问外网)

#server将数据包由172.25.0.230发送到desktop的172.25.0.130里(能发送成功需设定网关)               进来

再由 内核 将数据转换为172.25.254.130(打开路由功能)最后将数据包发送到172.25.254.40               出去

即地址伪装

初站地址伪装:开启masquerade服务即可

desktop里(服务端)

##添加一个不同于eth0的网段的网卡eth1,并设定其ip

eth0 172.25.254.130

eth1 172.25.0.130

 

server里(客户端)

##更改eth0的ip为172.25.0.230

 

此时ping 172.25.254.40 不通

添加网关:


desktop里

 firewall-cmd --permanent  --add-marquerade

 firewall-cmd --reload

 firewall-cmd  --list-all

 sysctl -a | grep ip_forward                  #查看路由功能是否打开 0 表示关闭,1 表示打开


 vim /etc/sysctl.conf                           #如果是0,则需要打开路由功能,使内部两个不同网端的ip能传输数据

############

添加net.ipv4.ip_forward = 1


 sysctl -p                     #使更改生效

 firewall-cmd --list-all

//

 firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=172.25.254.130 masquerade'   

 #设定高级规则 ipv4 表示协议

 firewall-cmd --reload

 firewall-cmd --list-all


server里测试:

此时ping 172.25.254.40 通

iptables service


 常用命令:

iptables -F                                                          #刷新策略

iptables -I INPUT 2 -p tcp --dport 23 -j DROP     #插入到第二条策略 dport表示目的地端口

iptables -E allowed disallowed                            #修改规则链名称

iptables -D INPUT 3                                           #删除filter表中的第三条规则

iptables -D INPUT -s 192.168.0.1 -j DROP           #按内容删除

iptables -R INPUT 3 -j ACCEPT                           #将原来编号为3的规则内容替换为ACCEPT

iptables -P INPUT DROP                                  #设置filter表INPUT链的默认规则 ,当数据包没有被规则列表里的任何规则匹                                                配到时,按此默认规则处理

iptables -t nat -F                                              #清空nat策略, -F 仅仅是清空链中规则,并不影响   -P设置的默认规则

iptables -t nat -nL                                            #查看nat策略

iptables -t nat -L PREROUTING                        #查看nat表PREROUTING链的规则

iptables -N redhat                                            #添加自定义链名

iptables-X redhat                                             #删除自定义链

iptables-A INPUT -s !192.168.0.4 -p tcp --dport 23 -j REJECT #只有4不可以连接

-D redhat 1                                                     #删除自定义链下的策略

-i eth0                                                             #匹配从网络接口 eth0 进来

-o eth0                                                           #匹配从网络接口 eth0 出去

-s 192.168.0.1                                                 #匹配来自 192.168.0.1 的数据包

-s 192.168.1.0/24                                            #匹配来自 192.168.1.0/24 网络的数据包

-d 202.106.0.20                                              #匹配去往 202.106.0.20 的数据包

-d 202.106.0.0/16                                           #匹配去往 202.106.0.0/16 网络的数据包

-d www.abc.com                                           #匹配去往域名 www.abc.com

1.启用 iptables

systemctl  stop     firewalld

systemctl  disable  firewalld

systemctl  start    iptables.service

systemctl  enable   iptables.service

 

2.iptables的三表五链


实验:

iptables -nL                             #有数据;


iptables -F                              #刷新策略; -F表示临时清除当前指定的表的数据

iptables -nL                           #没有数据


service iptables save               #保存更改到/etc/sysconfig/iptables

iptables -t filter -nL               #查看filter表,与iptables -nL看到的内容相同


##说明默认情况看到的是filter表

iptables -t nat -nL                #查看nat表


iptables -t mangle -nL         #查看mangle表,

 

iptables -nL                        #可查看到策略为ACCEPT


yum install httpd

测试:


systemctl start httpd          #打开Apache服务

测试:


3.filter表的常规配置:

1).管理策略:

实验:

iptables -P INPUT DROP                    #更改默认协议为丢弃状态,-P表示默认规则

iptables   -nL                                     #查看表的信息,默认为filter表。-n表示不做解析,显示ip     -L表示显示表的策略信息



测试:


iptables -P INPUT ACCEPT                                      #将默认协议改回接受状态

iptables   -nL


iptables -A INPUT  -p tcp --dport 80 -j REJECT        #拒绝80端访问口 -A表示添加,

iptables   -nL


测试:



##读取方式: 从上往下依次读取,一旦找到匹配的数据,下边的内容就不再读取,以提高读取速率

iptables -A INPUT -s 172.25.254.77 -p  tcp --dport 80 -j  ACCEPT       #设定77主机可以访问80端口,默认添加到最后  

iptables   -nL


测试:(还是不能访问,因为第一条拒绝访问80端口的信息已经读取,所以后边的允许访问的信息读取不到)


iptables -D INPUT 2                                                   #删除第二条策略,因为第二条没有生效 -D表示删除

iptables -nL


iptables -I INPUT -p  tcp --dport 80  -j  ACCEPT        # -I 表示插入,可以指定添加策略的位置,默认添加到最前面

iptables -nL


测试:

此时77可以访问, 其他主机也可以访问

 

iptables -R INPUT 1 -s 172.25.254.77 -p  tcp --dport 80 -j  ACCEPT           #修改一条策略使得172.25.254.77这台主机可以访问       -R 表示修改

iptables -nL                  


测试:

只有77这台主机可以访问

在真机里:

 

在serever里:



2)对链的基本操作

##默认情况下filter表只有3条链

iptables -nL


iptables -N westos                     #添加自定义的westos链

iptables -nL

  

iptables -E westos WESTOS       #修改链的规则(重命名)

iptables -nL

  

iptables -X WESTOS                  #删除WESTOS链

iptables -nL


3)设定服务的访问控制

##显示检测每一个数据包,并且不重复检测

实验:

iptables -nL


iptables -F                                            #刷新,临时清除当前指定的表的数据

iptables -nL


iptables -A INPUT -p tcp --dport 22 -j ACCEPT                     #允许sshd服务

iptables -A INPUT -p tcp --dport 53 -j ACCEPT                     #允许dns服务

iptables -A INPUT -p tcp --dport 80 -j ACCEPT                     #允许http服务

iptables -A INPUT -p tcp --dport 3260 -j ACCEPT                 #允许iscsi服务

iptables -A INPUT -i lo -j ACCEPT     #允许记录回环接口的数据;lo表示回环接口

iptables -F

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT         #直接允许正在连接的和已经连接过的

iptables -nL


iptables -A INPUT -m state --state NEW -p tcp --dport 22 -j ACCEPT          #允许新的sshd服务

iptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT          #允许新的dns服务

iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT          #允许新的http服务

iptables -A INPUT -m state --state NEW -p tcp --dport 3620 -j ACCEPT      #允许新的iscsi服务

iptables -A INPUT -m state --state NEW -p tcp --dport 443  -j ACCEPT       #允许新的

iptables -A INPUT -m state --state NEW -i lo -j ACCEPT                             #允许新的回环接口

iptables -A INPUT -j REJECT                                                                       #剩下的全部拒绝

iptables   -nL


service iptables save


cat /etc/sysconfig/iptables


 

4.用iptable命令实现地址伪装

 实验环境:

desktop里    eth0 172.25.254.130

(双网卡)    eth1 172.25.0.130

server里    172.25.0.230


实验(在desktop里)

iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to-source 172.25.254.130        #所有从eth0出去的数据包地址都伪装成172.25.254.130输出,使内网能访问外网,SNAT就是改变转发数据包的源地址

iptables -t nat -nL


测试(在server里)

内网访问外网

此时 ping 172.25.254.44  成功

ssh [email protected]

w -i                 #可查看到连接它的是172.25.254.130

实验:(在desktop里)

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 22 -j DNAT --to-dest 172.25.254.44        #22端口是以172.25.0.130输入,使外网能访问内网,DNAT就是改变转发数据包的目的地址

iptables -t nat -nL


测试:(在真机里)

外网访问内网

ssh [email protected]  

ifconfig eth0       #实际上连接的是172.25.254.44


猜你喜欢

转载自blog.csdn.net/love_sunshine_999/article/details/80617412