Linux中的Firewalld与iptables

配置两台虚拟机的网络环境:
Desktop:
添加两块网卡,配置两个网络段的IP:172.25.254.120/192.168.0.120

ip addr show

【1】
Server:
配置192网段的IP:192.168.0.220

ip addr show
ping 192.168.0.120

【2】

使用命令接口配置firewalld

firewall-config                         /图形化管理firewalld
firewall-cmd --state 			        /查看火墙状态
firewall-cmd --get-active-zones		    /查看正在使用的域
firewall-cmd --get-default-zone		    /查看默认域
firewall-cmd --get-zones		        /查看所有域
firewall-cmd --zone=public --list-all   /显示public域的所有信息
firewall-cmd --get-services				/查看允许添加的服务
firewall-cmd --list-all-zones			/显示所有的域和它的信息
firewall-cmd --set-default-zone=dmz     /设定默认域为dmz

【3】

firewall-cmd --permanent --zone=internal --add-source=172.25.0.0/24      /设置网络地址到指定区域

【4】

firewall-cmd --permanent --zone=internal --remove-source=172.25.0.0/24   /删除指定区域中的网络地址

【5】

firewall-cmd --permanent --zone=internal --add-interface=eth0            /添加网络接口

【6】

firewall-cmd --permanent --zone=internal --change-interface=eth0         /改变网络接口

firewall-cmd --permanent --zone=internal --remove-interface=eth0 	 /删除网络接口
firewall-cmd --permanent --zone=public --add-service=smtp		 /添加服务
firewall-cmd --permanent --zone=public --remove-service=smtp   		 /删除服务
firewall-cmd --zone=public --list-ports					 /列出端口
firewall-cmd --permanent --zone=public --add-port=8080/tcp		 /添加端口

【7】

firewall-cmd --permanent --zone=public --remove-port=8080/tcp 	 	 /删除端口

【8】

firewall-cmd --reload 								 /重载防火墙
firewall-cmd --complete-reload 						 /中断已建立连接重载火墙

火墙添加服务两种方式:

默认使用的域为public时:

firewall-cmd --permanent --add-service=http  /自动在/etc/firewalld/zones/public(现在使用的域中)文件中添加http服务
firewall-cmd --reload  						 /刷新当前的状态

【9】

vim /etc/firewalld/zones/public.xml
 <service name="https"/>                     /添加https服务
firewall-cmd --reload 

【10】

火墙中添加服务端口

临时修改

firewall-cmd --list-ports  		/查看火墙上开启的端口
firewall-cmd --add-port=8080/tcp  	/添加端口
firewall-cmd --list-ports  		/查看此时已开启
firewall-cmd --remove-port==8080/tcp

【11】

永久修改

firewall-cmd --permanent  --add-port=8080/tcp 		/此时/etc/firewalld/zones/public.xml中生成端口状态
vim /etc/firewalld/zones/public
<port protocol="tcp" port="8080"/> 
firewall-cmd --permanent  --remove-port=8080/tcp

【12】

内核插件上的三个表

三表五链
(1)filter表
input output forward
input output都要经过本机内核
经过内核都叫forward(相当于大脑思考)
内核上开启服务ftp http
inout从外边传输数据给内核
ouput从内核将结果传送出来
从本机进来绕过本机服务,从本机出来
经过了内核,但不使用内核服务,不是把数据给内核,只是做转换
(2)nat表
input output 都不经过内核
postouting 路由之后,做原地址封装,数据访问目的地主机,output时在路由端口做NAT转换,发生在路由之后
preouting 路由之前,数据从目的地主机传回时经过路由,在路由端口转换
(3)mangle表
input
prerouting
forward
postrouting
output
数据传输流程:数据input--->prerouting做NAT转换--->forward内核操作--->postrouting--->output
设置让单独某台主机不能访问

firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 1 -s 172.25.254.220 -p tcp --dropt 80 -j REJECT
firewall-cmd --permanent --direct --remove-rule ipv4 filter INPUT 1 -s 172.25.254.220 -p tcp --dropt 80 -j REJECT

-s      数据来源
-o      协议
-dport  目的地端口
-sport  数据端口
-j      行为
--direct   后边跟规则
--add-rule 添加规则
filter  表
INPUT   表中规则(输入)
1       排在第一个
DROP    拒绝不提醒
ACCEPT  接收
REJECT  拒绝提醒,不停加载,不提示

地址伪装和端口转发

端口转发:
ssh服务开启端口 22

firewall-cmd --add-masquerade
firewall-cmd --add-forward-port=port=22:proto=tcp:toport=22:toaddr=172.25.254.20
firewall-cmd --list-all

客户端:
server虚拟机:设置IP为192.168.0.220
真机:设置IP为172.25.254.20

ssh root@192.168.0.119  	/如果此时连接不上,需删掉提示中的/root/.ssh/know_hosts

输入真机密码
/input进来时,端口已经被转发

ip addr show		    /真机IP
w -i  					/查看真机上显示的是谁连接

/output时地址已经被伪装
进来时通过22端口,我帮你自动转换到别人,别人连我时我让你去连另一个人
出去时经过地址包装,展示个给别的的是路由地址

iptables

1.安装

iptables -nL  				/默认查看filter
iptables -t filter -nL
iptables -t nat -nL
yum install iptables-servers
systemctl stop firewalld
systemctl disable firewalld
systemctl mask firewalld
systemctl start iptables

2.保存查看过的表中的数据

永久

service iptables save
cat /etc/sysconfig/iptables

【13】

iptables-save 					/保存
iptables-save > /etc/sysconfig/iptables 	/永久保存

刷新掉再保存就清空保存的内容

iptables -F
service iptables save

3.操作参数

t 指定表
-p 协议
-A 添加
-D 删除
-C 检查策略
-I 插入
-R 替换
-F 刷新,清除所有
-N 添加新链
-X 删除链
-E 替换名称
-S 查看规则中的命令
-j 指定行为 ACCEPUT|REJECT|DROP
-P 修改默认规则,不能用reject


iptables -t filter -A INPUT -p tcp --dport 22 -j ACCEPT 	/指定filter表添加输入,遵循tcp协议,目的地端口为22,接收连接
iptables -nL

【14】

iptables -t filter -A INPUT -s 172.25.254.19 -p tcp --dport 22 -j ACCEPT  	/指定主机可输入(ssh连接)
iptables -nL

【15】

iptables -t filter -I INPUT 1 -p tcp --dport 22 -j ACCEPT  	/指定插入到第一位,每一个策略都会读,从上往下
iptables -nL

iptables -S INPUT			/查看INPUT中的所有使用过的命令

【16】

iptables -N redhat 	/默认添加到filter组中
iptables -nL

【17】

iptables -E redhat westos  	/修改链名
iptables -nL

【18】

iptables -X westos   	/删除westos链
iptables -nL 

【19】

intables -P INPUT DROP  #修改filter表中INPUT链默认DROP
intables -nL

4.调整查询速度

设置原来连接过的连接时不需要依次读取,新的依次查找

netstat -antlupe /查看所有开启的端口
-i       接口
-p       协议
-m       后边接状态
--state  状态 
ESTABLISHED 第一次连接上未断开情况下,继续查询
RELATED 连接过的查询
intables -A INPUT -m state ESTABLISHED,RELATED -j ACCEPT
intables -A INPUT -m state --state NEW -i lo -j ACCEPT  /通过本机回环接口的所有数据都允许
intables -A INPUT -m state --state NEW -p tcp --dport 22 -j ACCEPT
intables -A INPUT -m state --state NEW -p tcp --dport 22 -j ACCEPT
iptables -A INPUTE -j REJECT

5.

从eth0这出去的信息经过路由之后(地址伪装)传输给别人
我接收的来自eth0信息是在发生在路由之前(未经过内核,进行地址转换)
注意1:这里的进来出去均是对于路由器说的
DNAT 接收 地址伪装
SNAT 发送 地址转换 POSTROUTING
–to-source #去向

iptables -t nat -A POATROUTING -o eth0 -J SNAT --to-source 172.25.254.120 /从eth0进来的数据,将其源地址转换为172.25.254.120
iptables -t nat PREROUTING -i eth0 -p tcp --dport 22 -j DNAT --to-dest 192.168.0.220:22  /从eth0网卡的22端口进来的数据,将其目的地转换为192.168.0.120

实验:

> /etc/sysconfig/iptables  /清空iptables所有缓存
iptables -nL /查看策略信息是否已经清空
iptables -nL -t nat

【20】

sysctl -a | grep ip_forward
vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
sysctl -p

双网卡主机上

iptables -t nat -A POSTROUTING -o eth0 -J SNAT --to-source 172.25.254.120

【21】
此时在192.168.0.220主机上测试

ping 172.25.254.20(此网段所有主机均可ping通)
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 22 -j DNAT --to-dest 192.168.0.220:22 

【22】
此时在172.25.254.20主机上

ssh root@172.25.254.120
ip addr show
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值