Linux防火墙IPTABLES

IPTABLES


(1)IPTABLES 是与最新的 3.5 版本 Linux 内核集成的 IP 信息 包过滤系统。如果 Linux 系统连接到因特网或 LAN、服务器或连接 LAN 和因特网的代理服务器, 则该系统有利于在 Linux 系统上更好地控制 IP 信息包过滤和防火墙配置。
(2)IPTABLES 中定义有表,分别表示提供的功能,有filter表(实现包过滤)、nat表(实现网络地址转换)、mangle表(实现包修改)、raw表(实现数据跟踪),这些表具有一定的优先级:raw–>mangle–>nat–>filter

 systemctl stop firewalld
 systemctl mask firewalld
 systemctl restart iptables
 iptables -nL                                                    ###n:不做解析,L:list
 iptables -F 或 > /etc/sysconfig/iptables                        ###清除策略,默认为filter
 service iptables save                                           ###保存策略到配置文件/etc/sysconfig/iptables
 iptables -A INPUT -j ACCEPT                                     ###添加策略
 service iptables save                                           

防火墙策略的基本设置


注意:防火墙策略的读取是按从上往下的先后顺序读取的,只要有满足的便会不再匹配,都没有时便会按默认的规则处理
参数:-A:add -s:source dport:端口 -j:动作 -I:insert -p(小写):protocol,协议 -D:delete

 iptables -t filter -nL                                                 ###不加-t默认filter
 iptables -A INPUT -j REJECE                                            ###拒绝所有
 iptables -t filter -A INPUT -s 172.25.254.225 -j ACCEPT|DROP|REJECT    ###对225的接受,忽略,拒绝
 iptables -A INPUT -s 172.25.254.25 -p tcp --dport 22 -j ACCEPT       
 iptables -I INPUT 3 -s 172.25.254.225 -p tcp --dport 22 -j ACCEPT      ###在第3行插入,p:协议,dport:端口
 iptables -D INPUT 4                                                    ###删除第4个
 iptables -R INPUT 1 -s 172.25.254.225 -p tcp --dport 22 -j ACCEPT      ###更改已有的规则
 iptables -t nat -R POSTROUTING  1  -o eth0 -j SNAT --to-source 172.25.254.125
 iptables -P INPUT DROP                                                 ###改变默认的权限P(大写)(只能ACCEPT和DROP)
 iptables -t filter -N redhat                                           ###添加自定义链名
 iptables -t filter -E redhat dream                                     ###改变链名
 iptables -t filter -X dream                                            ###删除自定义链
 iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT       ###添加连接过的,正在连接到防火墙策略
 iptables -A INPUT -m state --state NEW -i lo -j ACCEPT                 ###添加新的回旋接口
 iptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT     ###只要连接端口为53都可以连接
 iptables -A INPUT  ! -s 172.25.254.25 -j REJECT                        ###只有172.25.254.25能连接

不同网段互相访问


基础环境:

客户端:192.168.0.225,加入网关:192.168.0.125
服务器:设置2个网卡,IP:172.25.254.125/192.168.0.125

 iptables -F
 service iptables save
(1)地址伪装(路由后)
 iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to-source 172.25.254.125  ###路由后,通过172.25.254.125的协议从eth0出去

 sysctl -a |grep ip_forward
 net.ipv4.ip_forward = 0
 vim /etc/sysctl.conf
 net.ipv4.ip_forward = 1
 sysctl -p                                                                  ###刷新
测试

客户端:192.168.0.225

 ping 172.25.254.56                                                        ###发现可以ping通真机
 ssh root@172.25.254.56
 w                                                                         ###查看login

我们可以发现地址发生了转变为IP:172.25.254.125,而我们的真机IP为172.25.254.56

这里写图片描述

这里写图片描述

(2)地址转发(路由前)
 iptables -t nat -A PREROUTING -i eth0 -j DNAT --to-dest 192.168.0.225       ###把eth0过来的转移到192.168.0.225
测试

IP:172.25.254.56(真机)

 ssh root@172.25.254.125
 ifconfig 
 w                                                                      

通过w我可以发现我们只是地址发生了转发,而我们的登陆的IP还是172.25.254.56

这里写图片描述

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Wielun

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

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

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

打赏作者

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

抵扣说明:

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

余额充值