【Linux应用层】防火墙iptables命令
来源:互联网 发布:office for mac 15.36 编辑:程序博客网 时间:2024/06/03 17:02
iptables四个表作用图:
iptables的四个表:
raw表有2个链:prerouting、output
mangle表有5个链:prerouting、postrouting、input、output、forward
nat表有3个链:prerouting、postrouting、output
filter表中有3个链:input、output、forward
命令
1. iptables打印信息解释
2. 规则查看与清除
规则查看:
iptables [-t tables] [-L] [-nv]
例:
iptables -L -n //列出filter table 3条链的规则iptables -t nat -L -n //列出nat table 3条链的规则
规则清除:
iptables-save //列出完整防火墙规则iptables-save [-t table] //列出NAT表规则iptables-save [-t table] [-FXZ]
例:
iptables -F iptables -Xiptables -Z
3. 定义默认策略(policy)
当数据包不在我们设定的规则之内时,则该数据包的通过与否,是以Policy的设置为准
iptables [-t table] -P [INPUT, OUTPUT, FORWORD] [ACCEPT, DROP]
范例:将本机的INPUT设置为DROP,其他设置为ACCEPT
iptables -P INPUT DROPiptables -P OUTPUT ACCEPTiptables -P FORWORD ACCEPT
4. 数据包的基础比对:IP、网络及接口设备
iptables [-AI 链名] [-io 网络接口] [-p 协议] [-s 来源 IP/网络] [-d 目标 IP/网络] -j [ACCEPT|DROP|REJECT|LOG]
范例1:设置lo成为受信任的设备,即 进出lo的数据包都予以接受
iptables -A INPUT -i lo -j ACCEPT
范例2:只要是来自内网的(192.168.100.0/24)的数据包就通通接受
iptables -A IMPUT -i eth1 -s 192.168.100.0/24 -j ACCEPT
范例3:接受来自192.168.100.10的数据,丢弃来自192.168.100.230的数据
iptables -A INPUT -i eth1 -s 192.168.100.10 -j ACCEPTiptables -A INPUT -i eth1 -s 192.168.100.230 -j DROP
范例4: 记录某个规则记录
iptables -A INPUT -s 192.168.2.200 -j LOG
只要数据包来自192.168.2.200这个IP时,该数据包的相关信息就会被写入到内核日志文件(var/log/messages)中,然后该数据包会继续进行后续的规则比对。
5. TCP、UDP的规则比对:针对端口设置
iptables [-AI 链] [-io 网络接口] [-p tcp,udp] [-s 来源 IP/网络] [--sport 端口范围] [-d 目标 IP/网络] [--drop 端口范围] -j [ACCEPT|DROP|REJECT]
注意:因为仅有TCP与UDP数据包具有端口,因此想要使用-dport、–sport时,需要加上 -p tcp 或 -p udp 的参数才会成功。
范例1:阻挡进入本机 port 21 的数据包
iptables -A INPUT -i eth0 -p tcp --dport 21 -j DROP
范例2:想连到本台主机的网上邻居(udp port 137, 138 tcp port 139, 445)就放行
iptables -A INPUT -i eth0 -p udp --dport 137:138 -j ACCEPTiptables -A INPUT -i eth0 -p tcp --dport 139 -j ACCEPTiptables -A INPUT -i eth0 -p tcp --dport 445 -j ACCEPT
范例3:来自 192.168.1.0/24 的 1024:65535 端口的数据包,且想要连接到本机的 ssh port 就予以阻挡
iptables -A INPUT -i eth0 -p tcp -s 192.168.1.0/24 --port 1024:65534 --dport ssh -j DROP
范例4:将来自任何地方来源 port 1:1023 的主动连接到本机端的 1:1023 连接丢弃
iptables -A INPUT -i eth0 -p tcp --sport 1:1023 --drop 1:1023 --syn -j DROP
6. iptables外挂模块: mac 与 state
网络是双向的,当我们放行数据包出去的时候同样也要放行对应的数据包进来,在管理上就有一定的困难。
iptables可以通过一个模块来分析这个想要进入的数据包是否为刚刚发出去的响应,如果是,则予以接受放行。这样就不用考虑远程主机是否进来的问题了。
iptables -A INPUT [-m state] [--state 状态]
范例1:只要以建立连接或与已发出请求相关的数据包就予以通过,不合法的数据包就丢弃
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -A INPUT -m state --state --state INVALID -j DROP
范例2:针对局域网内的 aa:bb:cc:dd:ee:ff 主机放开其连接
iptables -A INPUT -m mac --mac-source aa:bb:cc:dd:ee:ff -j ACCEPT
7. ICMP数据包规则的比对:针对是否相应ping来设计
iptables -A INPUT [-p icmp] [--icmp-type 类型] -j ACCEPT
范例:让 0,3,4, 11,12,14,16,18 的 ICMP type 可以进入本机
[root@F12 ~]# vi somefile#!/bin/bashicmp_type="0 3 4 11 12 14 16 18"for typeicmp in $icmp_typedo iptables -A INPUT -i eth0 -p icmp --icmp-type $typeicmp -j ACCEPTdone[root@F12 ~]# vi somefile
- 【Linux应用层】防火墙iptables命令
- Linux 防火墙iptables命令详解
- Linux 防火墙 iptables 命令详解
- Linux网络安全_防火墙(iptables命令)
- linux下通过iptables命令设置防火墙
- linux中iptables防火墙命令详解
- Linux—防火墙Iptables命令使用示例
- Linux下iptables防火墙的基本应用
- Linux iptables 防火墙 学习与应用
- linux下如何禁用防火墙? linux 防火墙配置命令 iptables
- Linux防火墙iptables详解(三)--iptables命令详解和举例
- 编译内核实现iptables防火墙layer7应用层过滤 (三)
- centos iptables 防火墙 命令
- iptables防火墙相关命令
- 防火墙iptables 应用
- linux防火墙(iptables)
- linux 防火墙 iptables
- Linux防火墙配置文件(iptables)
- Python之可读写的socket逻辑
- Java IO 体系结构
- eclipse编译调试tomcat7.0.65
- iOS--同时请求的问题(用不到)
- java中的并发集合类概述
- 【Linux应用层】防火墙iptables命令
- LinkedHashMap解读
- Java IO字节流和字符流的区别
- 边城
- [实验]linux虚拟机不进单用户重置密码
- 数据库 shard 分片存储
- CityEngine CGA语法之金字塔式屋顶函数 roofPyramid
- POJ1887 Testing the CATCHER
- 数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历