LInux高级运维(八)-IPtables及其表的应用

Iptables 防火墙-保护隔离公网
REHEL 默认firewalled防火墙 ->底层还调用的是包过滤防火墙iptables
工作层:网络层
调用包过滤防火墙iptables
写过滤规则达到过滤效果
1装包-安装防火墙软件
Systemctl status firewall d
yum list | grep -i iptables
yum -y install iptables-services.x86_64

开机自启
systemctl start iptables.service
systemctl enable iptables.service
systemctl status iptables.service

2防火墙的组成
四张表 五条链
Raw  ->状态跟踪
Mangle ->到达防火漆的IP数据包打标记
Nat  -> 地址转换
Filter ->过滤表 过滤与规则匹配的表

链是匹配ip数据包传输方向
Input ->进入防火墙主机的包
Output ->从防火墙主机出去的包
FORWARD 从防火墙主机经过的包
POSTROUTING 路由后处理  防火墙告诉怎么走->
PREROUTING路由前处理   提前选好了去哪儿浪
在这里插入图片描述

3 iptables 管理工具\
 基本用法
管理程序的位置-/sbin/iptables
注意事项/
1/可以不指定表,默认filter表
2/可以不指定链,默认为对应表的所有链
3/如果没有匹配的规则
4/所有链的初始默认规则均为accept

Iptables [-t 表名]  选项 [链名] [条件] [-j 目标操作]

选项 :-L -A -I -D -F
条件
目标操作:-J1accept 允许通过  2 drop 直接丢弃不告诉  3 reject 拒绝通过有提示  4 log 记录日志,然后传给下一条规则.
包过滤匹配规则:
顺序比对,匹配即停止
若无任何匹配,则按该链的默认规则处理
在这里插入图片描述

[root@51 ~]# iptables -t mangle -nL ->查看mangle 表所有规则
Chain PREROUTING (policy ACCEPT)
target prot opt source destination

Chain INPUT (policy 2ACCEPT)
target prot opt source destination

Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination

Chain POSTROUTING (policy ACCEPT)
target prot opt source destination

iptables -t filter -nL   查看filter 包过滤表所有规则

Chain INPUT (policy ACCEPT)
target prot opt source destination
icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target prot opt source destination
REJECT all – 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

iptables -t raw -nL 查看raw 状态追踪表所有规则
Chain PREROUTING (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

iptables -t nat -nL 查看nat 地址转换表所有规则
Chain PREROUTING (policy ACCEPT)
target prot opt source destination

Chain INPUT (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

Chain POSTROUTING (policy ACCEPT)
target prot opt source destination

5删除防火墙那个规则
iptables -t nat -nL --line-numbers
164 iptables -t raw -F
165 iptables -t nat -F
166 iptables -t mangle -F
167 iptables -t filter -F
168 cat /etc/sysconfig/iptables -> 永久生效
169 iptables-save > /etc/sysconfig/iptables
Systemctl restart iptables.service

6自定义防火墙的规则

1写防火墙规则保护本机 -> 主机型防火墙(filter->input)
2写防火墙限制数据包通过 -> 网络型防火墙(filter -<>FORWARD)

规则链规则:
顺序比对,匹配即停止
若无任何匹配,则按该链的默认规则处理

在这里插入图片描述
需要取反条件时,使用号
In 收
Out 发

环境准备 A 主机192.168.4.50
B 主机192.168.4.51 192.168.2.51
C 主机192.1268.2.52

一 书写防火墙规则,主机型防火墙(filter->input)

iptables -t filter -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -t filter -P INPUT DROP
iptables -t filter -nL INPUT --line-numbers

systemctl stop iptables.service
184 yum -y install httpd*
186 systemctl start httpd.service
187 systemctl enable httpd.service
188 systemctl start iptables.service
189 ss -ntulp | grep :80
190 echo “hostb66666” > /var/www/html/test.html
191 cat /var/www/html/test.html

197 iptables -t filter -I INPUT -p tcp --dport 80 -s 192.168.4.0/24 -j    ACCEPT
198 iptables -t filter -nL --line-numbers
199 iptables-save > /etc/sysconfig/iptables
 200 在A4.0/主机客户端测试访问curl http://192.168.4.51/test.html
在C2.0/网段访问测试   crul http://192.168…4.51/test.html

Iptables -t filter -I INPUT -p icmp -j REJECT


环境准备 A 主机192.168.4.50
B 主机192.168.4.51 192.168.2.51
C 主机192.1268.2.52
二 主机型防火墙
 [root@51 ~]# iptables -t filter -A INPUT -p icmp -j ACCEPT
[root@51 ~]# iptables -t filter -D INPUT 3

Iptables -t filter -A INPUT  -p icmp --icmp-type echo-reply -jACCEPT 

[root@51 ~]# iptables-save > /etc/sysconfig/iptables

AC 主机不可以ping同B 主机
但是B 主机可以ping通AC 主机
[root@51 ~]# ping 192.168.2.52
PING 192.168.2.52 (192.168.2.52) 56(84) bytes of data.
64 bytes from 192.168.2.52: icmp_seq=1 ttl=255 time=0.415 ms

[root@51 ~]# ping 192.168.4.50
PING 192.168.2.52 (192.168.2.52) 56(84) bytes of data.
64 bytes from 192.168.2.52: icmp_seq=1 ttl=255 time=0.415 ms


环境准备 A 主机192.168.4.50
B 主机192.168.4.51 192.168.2.51
C 主机192.1268.2.52

三 网络型防火墙
1 删除之前主机性防火墙配置
iptables -t filter -P INPUT ACCEPT
iptables -t filter -F
iptables-save > /etc/sysconfig/iptables

2 [root@51 ~]# sysctl -a | grep -i forward
net.ipv4.ip_forward = 1 -> 默认开启了内核转发功能
[root@51 ~]# echo “net.ipv4.ip_forward=1” >> /etc/sysctl.conf -永久生效

[root@52 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.2.0 0.0.0.0 255.255.255.0 U 101 0 0 eth1

3 Systemctl stop NetworkManger
4 添加网关: route add default gw 192.168.2.51
Route del default gw 192.168.2.51


运维常用linux命令整理

1.临时添加网关
route add default gw 192.168.4.50
2.永久添加网关
vim /etc/sysconfig/network
GATEWAY=192.168.4.50

关闭网卡eth1 [root@52 ~]# ifdown eth0


5 [root@51 ~]# iptables -t filter -nL FORWARD --line-numbers
[root@51 ~]# iptables -t filter -P FORWARD DROP
iptables-save > /etc/sysconfig/iptables
此时C主机不可以ping通A主机了

6 iptables -t filter -A FORWARD -p tcp --dport 80 -j ACCEPT
iptables -t filter -A FORWARD -p tcp --sport 80 -j ACCEPT
iptables-save > /etc/sysconfig/iptables
iptables -t filter -nL FORWARD --line-numbers

7 客户端验证:->可以访问4.50页面看到内容
root@50 ~]# echo "666host A " > /var/www/html/test.html
[root@52 ~]# curl http://192.168.4.50/test.html
666host A

四 网络型防火墙,内网访问外网

环境准备 A 主机192.168.4.50
B 主机192.168.4.51 192.168.2.51
C 主机192.1268.2.52

B主机操作 iptables -t filter  -A   FORWARD  -p tcp --sport 22 -j ACCEPT

iptables -t filter A FORWARD -p tcp --dport 22 -j ACCEPT
iptables-save > /etc/sysconfig/iptables
iptables -t filter -nL FORWARD --line-numbers

A /C 互相远程对方,发现操作允许
[root@50 ~]# ssh -X root@192.168.2.52
[root@52 ~]# ssh -X root@192.168.4.50


五 扩展匹配

基本用法
-m module
在这里插入图片描述

常见三个模块:
-m Mac-source mac地址 -MAC 匹配 
-m Multiport --sport 源端口/ --dport 目标端口 -多端口匹配
-m Iprange --src-range ip1-ip2 -ip范围匹配
–drc-range ip1 -ip2

环境准备 A 主机192.168.4.50
B 主机192.168.4.51 192.168.2.51
C 主机192.1268.2.52

[root@51 ~]# iptables -t filter -nL FORWARD --line-numbers
Chain FORWARD (policy ACCEPT)
num target prot opt source destination
1 ACCEPT tcp – 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
2 ACCEPT tcp – 0.0.0.0/0 0.0.0.0/0 tcp spt:80
3 ACCEPT tcp – 0.0.0.0/0 0.0.0.0/0 tcp spt:22
4 ACCEPT tcp – 0.0.0.0/0 0.0.0.0/0 tcp dpt:22

Iptables -t filter -F
Iptables -t filter -A FORWARD -p tcp -m multiport --dport 22,80 -j ACCEPT
Iptables -t filter -A FORWARD -p tcp -m multiport --sport 22,80 -j ACCEPT
iptables-save > /etc/sysconfig/iptables
iptables -t filter -nL FORWARD --line-numbers

Iptables -t filter -A FORWARD -p icmp -j ACCEPT -所有主机都可以互通

[root@51 ~]# iptables -t filter -nL FORWARD --line-numbers

环境准备 A 主机192.168.4.50
B 主机192.168.4.51 192.168.2.51
C 主机192.1268.2.52

一 多端口匹配
[root@51 ~]# iptables -t filter -I FORWARD 3 -p icmp -m iprange --dst-range 192.168.4.50-192.168.4.60 -j REJECT
root@51 ~]# iptables-save > /etc/sysconfig/iptables

二 Mac表
root@51]:
iptables -t filter -A FORWARD -p icmp -m mac --mac-source 52:54:00:5b:6a:9a -j DROP
iptables-save > /etc/sysconfig/iptables
iptables -t filter -nL FORWARD --line-numbers

root@50]: arp -a
gateway (192.168.4.51) at 52:54:00:b7:d9:27 [ether] on eth0

root@52]: ping 192.168.4.50

三 NAT 表使用
使用NAT 表做数据包中的原地址转换,实现所有主机共享一个公网IP上网

在这里插入图片描述

1 环境准备

环境准备 A 主机192.168.4.50
B 主机192.168.4.51 192.168.2.51
C 主机192.1268.2.52

iptables -t filter -F -清空之前所有配置
iptables -t filter -nL FORWARD --line-numbers
Iptables-save > /etc/sysconfig/iptables

清空50网关配置
[root@50 ~]# route del default gw 192.168.4.51
[root@50 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.4.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0

2 自定义NAT 表防火墙规则

root@51]: Iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -p tcp --dport 80 -j SNAT --to-source 192.168.4.51
root@51] : iptables -t nat -nL POSTROUTING --line-numbers
Chain POSTROUTING (policy ACCEPT)
num target prot opt source destination
1 SNAT tcp – 192.168.2.0/24 0.0.0.0/0 tcp dpt:80 to:192.168.4.51

root@51]:iptables-save > /etc/sysconfig/iptables

root@52]: curl http: //192.168.4.50/test.html
666host A

公网地址是服务上动态分配是,自定义防火墙规则
Iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -p tcp --dport 80 -j MASQUERADE

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

~上善若水~~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值