文章目录
环境配置
Windows环境下的虚拟机
一台主机node1是双网卡:192.168.4.23,172.25.254.123
另一台主机node2是单网卡:192.168.4.223
另一台主机node3作为真机:172.25.254.23


数据库在系统中的工作原理
火墙:
数据通过内核去访问服务
火墙相当于在内核上安装数据过滤表netfilter(决定谁可以通过,是一个插件)
软件工具iptables管理内核上的插件netfilter
通过firewall或者iptables对iptables进行控制
firewall和iptables的管理方式切换方法
企业7之前没有firewall管理方式
iptables更趋近于列表的更改方式
firewall更接近于Windows的管理方式
firewall方式切换到iptables方式
关闭firewall服务
systemctl disable --now firewalld
systemctl mask firewalld
启动iptables服务
dnf install iptables-services -y
systemctl enable --now iptables
查看iptables的火墙管理策略
iptabels -nL
提示词 | 说明 |
---|---|
target | 动作 |
ACCEPT | 允许通过 |
REJECT | 拒绝 |
prot | 程序,协议名称 |
opt | 参数 |
source | 来源 |
destination | 目的地 |
最后的内容 | 状态 |
iptables方式切换到firewall方式
关闭iptables服务
systemctl disable --now iptables
systemctl mask iptables
开启firewall服务
systemctl enable --now firewalld
systemctl unmask firewalld
查看firewall服务的策略
firewall-cmd --list-all --zone=public
firewall的管理
火墙中域的管理控制
firewall管理方式
用域的方式进行管理
其使用的数据保存机制是ssml可扩展语言的标记方式
火墙中允许的服务都保存到/etc
的目录里
zones
中有写允许了哪些服务
xml
:数据封装
可以用命令添加服务,也可以用文件方式管理服务
(/etc/firewall/zones
目录中的public.xml
)
管理域
不同的域直接设定好不同的服务
默认使用public
域 | 说明 |
---|---|
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 ipp-client dhcp-client samba-client |
external | ipv4网络伪装转发sshd |
firewall-cmd --get-default-zone //查看域
firewall-cmd --get-active-zones //查看活跃的域
firewall-cmd --get-zones //查看可用的域,列出所有的域,libvirt是安装虚拟机后就会出现的
dnf install httpd
enable --now
访问主页,直接被拒绝:因为现在的域不允许(不是public)
–set-default-zone=trues
可以访问阿帕奇的主页
更改网络
firewall-cmd --set-default-zone=block
firewall-cmd --get-default-zone
firewall-cmd --list-all
(以上是临时更改)
火墙重新启动restart
,不会还原默认设置
更改域是永久的
更改成public
后,在zones
里没有文件,
因此,永久更改firewall-cmd --permanent --add-service=ssh
没有更改默认设定,默认阅读的是/lib/firewall
(不加--permanent
只是修改了内存里的firewall方式)
如果修改了设定,就会修改/etc/firewall/zones
/etc/firewall/zones
中的文件都是数据文件的格式
双网卡:添加服务
设定apache
dnf install httpd -y
systemctl enable --now httpd
vim /etc/httpd/conf/httpd.conf/index.html
临时设定:只修改了内存的信息,不需要reload。直接临时添加,就可以生效
firewall-cmd --add-service=http //临时设定
firewall-cmd --list-all
如果进行reload
,临时设定就失效了,
没有更改配置文件
临时设定不需要--reload
,直接添加,直接生效
永久设定必须--reload
才可以生效
永久设定:--permanent
(修改配置文件,生成了old,并修改public.xml
)
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
firewall-cmd --lsit-all
双网卡:查看服务
firewall-cmd --get-services
cd /lib/firewalld/services
mv http.xml westos.xml
firewall-cmd --get-services | grep westos
双网卡:查看域里允许的服务
firewall-cmd --list-services
双网卡:删除服务
firewall-cmd --permanent --remove-service=http
firewall-cmd --reload
firewall-cmd --list-all
firewall-cmd --list-services
双网卡:设定端口
修改http的端口号
vim /etc/httpd/conf/httpd.conf
// /Listen,更改端口号为8080
systemctl restart httpd
netstat -antuple | grep 8080
添加端口
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload
firewall-cmd --list-all
测试:Firefox : 172.25.254.123:8080
删除端口
firewall-cmd --permanent --remove-port=8080/tcp
firewall-cmd --reload
firewall-cmd --list-all
访问控制
实验之前,用172网段的真机172.25.254.23去访问http://172.25.254.123,访问失败(没有添加httpd服务)
firewall-cmd --permanent --add-source=172.25.254.0/24 --zone=trusted //所有访问node1,只要是172.25.254.0网段的主机,都走的是trusted域
firewall-cmd --reload
firewall-cmd --list-all --zone=trusted
再次用172网段的真机去访问Firefox:http://172.25.254.123
,可以访问成功
删除trusted域
firewall-cmd --permanent --remove-source=172.25.254.0/24 --zone=trusted
firewall-cmd --reload
firewall-cmd --list-all --zone=trusted
Firefox:http://172.25.254.123
,访问失败
双网卡接口域的设定
默认ens3和ens9走的是public域
设定访问ens3走的是block域
双网卡接口域的删除与添加
firewall-cmd --permanent --remove-interface=ens3 --zone=public //将网卡ens3从public域中移除
firewall-cmd --permanent --add-interface=ens3 --zone=block //添加网卡ens3到block域
firewall-cmd --reload
firewall-cmd --list-all -zone=trusted
真机测试:ping 172.25.254.123
,失败
双网卡接口修改域的设定
firewall-cmd --permanent --change-interface=ens3 --zone=public
firewall-cmd --relaod
firewall-cmd --list-all --zone=public
真机测试:ping 172.25.254.123
,成功
3表5链
添加规则
Apache不允许172.25.254.23主机访问,其他人可以访问
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 1 -p tcp --dport 80 -s 172.25.254.23 -j REJECT //编写规则
firewall-cmd --reload
firewall-cmd --direct --get-all-rules
真机172.25.254.23
测试:Firefox:http://172.25.254.123
,失败
node1172.25.254.123
测试:Firefox:http://172.25.254.123
,成功
第一条命令相当于编写/etc/firewalld/direct.xml
文件
不符合INPUT第一条的数据均读取默认条件
-p:协议
-s:数据来源
–dport:目的地端口
-j:动作
REJECT:拒绝,会有响应
DROP:丢弃,没有响应
删除规则
firewall-cmd --direct --permanent --remove-rule ipv4 filter INPUT 1 -p tcp --dport 80 -s 172.25.254.23 -j REJECT
DNAT SNAT
SNAT
服务端:172.25.254.123 192.168.4.23
firewall-cmd --permanent --add-masquerate //地址伪装功能必须开启
firewall-cmd --reload
sysctl -a | grep ip_forward //开启内核路由功能
##如果 net.ipv4.ip_forward=0,那么需要编辑相关文件 /etc/sysctl.confg,
##可以使用命令 sysctl -p 查看文件的结果
客户端:
ip route add default via 192.168.4.23 //添加网关
route -n
测试:
node2:ssh [email protected]
真机:w -i
设定之后,192.168.4.223
的所有数据交给网关192.168.4.23
node2 192.168.4.223
尝试ssh
连接真机之后,真机键入命令w -i
显示的是172.25.254.123
连接
DNAT
目的地地址转换(内网转换)
firewall-cmd --permanent --add-forward-port=proto=tcp:port=22:toport=22:toaddr=192.168.4.223
firewall-cmd --reload
firewall-cmd --list-all
将发给192.168.4.23(172.25.254.123)
的所有请求发到192.168.4.223
比如,真机尝试ssh [email protected]
成功连接后,ifconfig
后显示的是192.168.4.223
(源地址转换:node2通过网关,去连接真机并且进行地址伪装,显示的是伪装后的地址)
(目的地地址转换:任何主机再去连接伪装地址,所有数据和连接都是交付给node2 192.168.4.223)
iptables的管理
火墙依旧是开启的,只是不能用firewall方式管理了
systemctl disable --now firewalld //关闭firewall服务
systemctl mask firewall
dnf install iptables-service -y
systemctl enable --now iptables
用iptables方式去查看策略
iptables -nL
state:软件包状态
NEW:新来源数据
RELATED:非首次来源数据
ESTABLISHED:正在连接
iptables方式删除策略
iptables -F //刷新加载,只删除了内存中的数据
iptables -nL //显示没有数据(n:不做解析)
如果restart重启iptables的服务,火墙策略会还原,因为配置文件的内容没有被删除
vim /etc/sysconfig/iptables
service iptables save
iptables -L //做解析:anywhere
iptables -nL //不做解析:速度快一些(ip)
指定表
iptables -t nat -nL //查看指定的表
iptables -t filter -nL
iptables -t mangle -nL
添加策略(只能最后一条位置)
iptables -A INPUT -p tcp -dport 22 -j ACCEPT
iptables -nL
拒绝172.25.254.23
(如果第一条是允许所有主机连接,那么,第2条拒绝命令就不会不生效)
iptables -p tcp --dport 22 -s 172.25.254.23 -j REJECT
iptables -nL
删除策略
iptables -D INPUT 2
iptables -nL
插入,并作为第一条
iptables -I INPUT 1 -p tcp -dport 22 -s 172.25.254.23 -j ACCEPT
iptables -nL
修改策略
(DROP:不回应,REJECT:有回应)
iptables -R INPUT 1 -p tcp --dport 22 -s 172.25.254.23 -j DROP
iptables -nL
添加新的链
iptables -N west
iptables -nL
修改链的名字
iptables -E west westos
iptables -nL
删除链
iptables -X westos
iptables -nL
修改表的默认规则
默认规则是ACCEPT(只能是DROP或者ACCEPT)
iptables -P INPUT DROP
iptables -nL
状态跟踪
iptables -F
iptables -nL
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT //m:状态
iptables -A INPUT -m state --state NEW -i lo -j ACCEPT
iptables -A INPUT -m state --state NEW -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT
iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j REJECT
iptables -A INPUT -j REJECT
iptables -nL
service iptables save
真机测试:Firefox:http://172.25.254.123
DNAT SNAT
iptables -t nat -F
iptables -t nat -nL
路由之后(内网访问外网)
node2去ping -w 3 172.25..254.23
失败
服务端:
iptables -t nat -A POSTROUTING -o ens3 -j SNAT --to-source 172.25.254.123
iptables -t nat -nL
所有从node1的网卡ens3(其实是node2的数据)出去的数据都写上123的地址
客户端测试:
ping -w 3 172.25.254.23
ssh [email protected]
ifconfig //显示的是真机的IP地址
真机测试:
真机显示的是172.25.254.123
w -i
路由之前(外网访问内网)
从真机172.25.254.23
进行ssh
连接172.25.254.123
,显示的还是172.25.254.123
,此时连接失败。地址转换失败
设定DNAT
服务端:
iptables -t nat -A PREROUTING -i ens3 -j DNAT --to-dest 192.168.4.223
iptables -nL
从172.25.254.123
进来的数据,转换到192.168.4.223
网段
(!:除了的意思)
客户端测试:
ssh [email protected]
ifconfig
地址转换:iptables VS firewall
firewall的地址转换不需要进行任何操作,只需要打开masquerate