本章实验需要两台主机(nodea、nodeb)
nodea为双网卡主机(ip为:172.25.254.117和172.25.17.117)
nodeb为单网卡主机(ip为:172.25.17.217)
*因为nodeb单网卡主机ip的网段根之前的不同所以需要重新配置软件仓库;真机在连接nodeb的时候因为网段不同也需要先连接nodea双网卡之后在连接nodeb
单网卡nodeb中下载火狐:dnf install firefox -y
1.火墙介绍
1)netfilter
2)iptables
3)iptables|firewalld
2.火墙管理工具切换
在rhel8中默认使用的是firewalld
1)firewalld----->iptables
dnf install iptables-services -y #下载iptables服务
systemctl status firewalld #查看火墙状态
systemctl stop firewalld
systemctl disable --now firewalld #关闭火墙
systemctl mask firewalld #冻结火墙服务
systemctl enable --now iptables #开启iptables服务
systemctl status iptables #查看iptables服务状态
2)iptales -------> fiewalld
systemctl stop iptables.service
systemctl disable --now iptables #关闭iptables服务
systemctl mask iptables #冻结iptables服务
systemctl unmask firewalld #解冻火墙【不解冻的话火墙服务不能开启】
systemctl enable --now firewalld #开启火墙服务
systemctl status firewalld.service #查看火墙状态
一、iptables 的使用
1.火墙策略的永久保存
iptables 策略记录文件:/etc/sysconfig/iptables
2.永久保存策略
iptales-save > /etc/sysconfig/iptables
service iptables save3.火墙默认策略
1)默认策略中的5条链
input ##输入
output ##输出
forward ##转发
postrouting ##路由之后
prerouting ##路由之前
2)默认的3张表
filter ##经过本机内核的数据(input output forward)
nat ##不经过内核的数据(postrouting,prerouting,input,output)
mangle ##当filter和nat表不够用时使用(input output forward postrouting,prerouting,)3)iptables命令
iptables -t ##指定表名称 iptables -n ##不做解析 iptables -L ##查看 iptables -A ##添加策略 iptables -p ##协议 iptables --dport ##目的地端口 iptables -s ##来源 iptables -j ##动作 ACCEPT #允许 DROP #丢弃 REJECT #拒绝 SNAT #源地址转换 DNAT #目的地地址转换 iptables -N ##新连接 iptables -E ##更改连接名称 iptables -X ##删除链 iptables -D ##删除规则 iptables -I ##插入规则 iptables -R ##更改规则 iptables -P ##更改默认规则
![]()
![]()
![]()
4)数据包状态
RELATED ##建立过连接的
ESTABLISHED ##正在连接的
NEW ##新的
【iptables读取内容是:从上到下依次读取】iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT #允许RELATED和ESTABLISHED状态的数据连接 iptables -A INPUT -m state --state NEW -i lo -j ACCEPT #允许回环接口中NEW状态的数据连接 iptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT #允许状态为NEW的数据访问53端口 iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT #允许状态为NEW的数据访问80端口 iptables -A INPUT -m state --state NEW -j REJECT #其他状态为NEW的数据访问全部拒绝
5)nat表中的dnat snat【内核路由功能一定要打开】
snat(内网连接外网)iptables -F #清除默认列表(filter) sysctl -a | grep ip_forward #检查基本配置【=1的话类似于firewalld中开启地址伪装,要是=0的话,要需要修改文件】 vim /etc/sysctl.conf 【检测基本配置要是net.ipv4.ip_forward = 0的话要在此文件中添加】 添加:net.ipv4.ip_forward = 1 sysctl -p #刷新 iptable -t nat -A POSTROUTING -o ens3 -j SNAT --to-source 172.25.254.117 #将内网访问外网的,通过此路路由器的ip转换为172.25.254.117 iptables -t nat -nL #查看nat表【读取内容从上到下】
在单网卡主机:nodeb中检测:
ping 172.25.254.16 #可以ping成功,则内网连接外网成功
dnat(外网连接内网)
iptables -t nat -A PREROUTING -i ens3 -j DNAT --to-dest 172.25.17.217 #将外网连接内网的,通过此路由的ip全部转换为172.25.17.217
在真机中:
ssh -l root 172.25.254.117 #连接ip为172.25.254.117的主机,实际上连接成功的是172.25.17.217 ifconfig #可以查看到我们实际连接的是172.25.17.217
二、firewalld
1 firewalld的开启systemctl stop iptables systemctl disable iptables systemctl mask iptables systemctl unmask firewalld systemctl enable --now firewalld
2.关于firewalld的域
trusted ##接受所有的网络连接
home ##用于家庭网络,允许接受ssh mdns ipp-client samba-client dhcp-client
work ##工作网络 ssh ipp-client dhcp-client
public ##公共网络 ssh dhcp-client
dmz ##军级网络 ssh
block ##拒绝所有
drop ##丢弃.所有数据全部丢弃无任何回复
internal ##内部网络 ssh mdns ipp-client samba-client dhcp-client
external ##ipv4网络地址伪装转发 sshd实验:【例如】
dnf install httpd -y systemctl enable --now httpd firewall-cmd --get-default-zone #查看到是public的域 firewall-cmd --list-all
检测:用真机火狐连接172.25.254.117 连接不上
firewall-cmd --set-default-zone=trusted #把域改为接受所有的网络 firewall-cmd --list-all
检测:用真机火狐连接172.25.254.117 连接成功
3.关于firewalld的设定原理及数据存储
/etc/firewalld ##火墙配置目录
/lib/firewalld ##火墙模块目录4.firewalld的管理命令
firewall-cmd --state ##查看火墙状态 firewall-cmd --get-active-zones ##查看当前火墙中生效的域 firewall-cmd --get-default-zone ##查看默认域 firewall-cmd --list-all ##查看默认域中的火墙策略 firewall-cmd --list-all --zone=work ##查看指定域的火墙策略 firewall-cmd --set-default-zone=trusted ##设定默认域 firewall-cmd --get-services ##查看所有可以设定的服务 firewall-cmd --permanent --remove-service=cockpit ##移除服务 firewall-cmd --reload #重启服务 firewall-cmd --permanent --add-source=172.25.254.0/24 --zone=block ##指定数据来源访问指定域 firewall-cmd --reload firewall-cmd --permanent --remove-source=172.25.254.0/24 --zone=block ##删除自定域中的数据来源 firewall-cmd --permanent --remove-interface=ens11 --zone=public ##删除指定域的网络接口 firewall-cmd --permanent --add-interface=ens11 --zone=block ##添加指定域的网络接口 firewall-cmd --permanent --change-interface=ens11 --zone=public ##更改网络接口到指定域
![]()
![]()
5. firewalld的高级规则
firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -s 172.25.254.17 -p tcp --dport 80 -j REJECT #允许ip为172.25.254.17的主机访问nodea的http,其他人不可以 firewall-cmd --direct --get-all-rules ##查看高级规则
永久添加:firewall-cmd --permanent--direct --add-rule ipv4 filter INPUT 1 -s 172.25.254.17 -p tcp --dport 80 -j REJECT
永久删除:firewall-cmd --permanent--direct --remove-rule ipv4 filter INPUT 1 -s 172.25.254.17 -p tcp --dport 80 -j REJECT6.firewalld中的NAT
SNAT【如何连接外网】
在nodeb中:ping 172.25.254.17 #不能ping ip route add default via 172.25.17.117 #给单网卡主机nodeb添加一个网关 route -n #查看网关
在nodea中:firewall-cmd --permanent --add-masquerade #添加地址伪装 firewall-cmd --reload #刷新 firewall-cmd --list-all #查看
在nodeb中:ping 172.25.254.17 #成功
DNAT【外网连接单网卡主机nodeb】
在nodea中firewall-cmd --permanent --add-forward-port=proto=tcp:port=22:toaddr=172.25.17.217:toport=22 #登陆本主机(nodea)时自动转换到217这台主机上 firewall-cmd --reload #刷新 firewall-cmd --list-all #查看
在真机中:
ssh -l root 172.25.254.117 #连接双网卡主机nodea,其实连接上的ip是单网卡nodeb的ip:172.25.17.217 ifconfig #查看连接上的ip