关于iptables配置的心得
其实iptables并不是一个很神秘的东西,只是掌握其配置起来比较困难,因为他的语法规则比较繁琐,配置起来比较麻烦,但是如果能掌握其基本用法,配置和使用起来慢慢就会得心应手。本文将从iptables的基本规则和用法详细讲起,然后附加一下案例来对iptables详细的理解,希望本文对你学习iptables有一点帮助下面我们一块来揭开iptables的神秘面纱。
说的iptables,稍微对iptables有一点了解的人都知道,其实iptables就是linux自带的一种防火墙策略,所以我们先来了解一下防火墙分类。
防火墙分类:
防火墙有硬件防火墙 --》华为,思科,天融信,神州数码等。
软件防火墙,--》微软的RSA等
主机防火墙,网络防火墙等等
包过滤型防火墙: 安全性较好,但是效率不高
根据tcp首部或者ip首部数据进行判断
- 简单的包过滤 仅检查tcp或者ip的包头信息
- 带状态检测的包过滤型
几种状态:NEW(第一次请求连接 syn=1,ack=0) ESTABLISHED (已建立的连 接syn=0,ack=1) INVALID (非法的连接) RELATED (相关连的连接)
应用层网关防火墙: 效率好了 但是安全性不佳 是根据数据包传输的实际数据内容进行过滤判断的。
iptables 工作在第三层,即网络层 但是iptables的规则却在kernel中
- filter: 策略过滤 iptables 默认的表格 包含三个链 INPUT, OUTPUT, FORWARD 应该在此三道门中做过滤
- nat: 地址转换啦 PREROUTING, POSTROUTING, OUTPUT
- mangle: 作用是改变服务类型而使用的表 PREROUTING, INPUT, FORWARD, OUTPUT, POSTROUTING
- 其中nat 和filter 是配置和使用iptables中最常用的两个,务必要熟悉。
iptables的命令与配置详解
iptables 命令使用规则有种好记的法则就是
iptables –t 表名 指令 链表名 描述 –j 动作
即iptables [-t TABLE] COMMAND CHAIN [creteria] -j ACTION
- -t 表格,总共有四个,常用的有nat ,filter
- -A --append 追加
- -I --insert 添加,插入
- -D --delete 删除
- -R –replace 替换
链接管理类:
- -F, flush, 清空链
- -N, new, 新建链
- -X, delete, 删除自定义的空链
- -E, rename 重命名一个链
默认策略:
- -P, --policy 改变策略
清空计数器:
- -Z, zero
- 每条规则(包括默认策略)都有两个计数器:
- 被此规则匹配到的所有数据包的个数;
- 被此规则匹配到的所有数据包的大小之和;
- -s 源地址 【网络号】 【子网】
- -i INTERFACE 进口 【eth0】
- -d 目的地址 【网络号】 【子网】
- -o INTERFACE 出口
- -p {tcp|udp|icmp} 协议
- [root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth0 -j ACCEPT
- [root@localhost ~]# iptables -t nat -L -v
- Chain PREROUTING (policy ACCEPT 3 packets, 905 bytes)
- pkts bytes target prot opt in out source destination
- Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
- pkts bytes target prot opt in out source destination
- 1 108 ACCEPT all -- any eth0 192.168.0.0/24 anywhere #新添加的规则
- Chain OUTPUT (policy ACCEPT 1 packets, 108 bytes)
- pkts bytes target prot opt in out source destination
- --sport PORT
- --dport PORT
- --tcp-flags ACK,SYN,RST,FIN SYN = --syn
- --tcp-flags ACK,SYN,RST,FIN SYN,ACK,RST,FIN
- --sport 22:23
- -p UDP
- --sport PORT
- --dport PORT
- -p icmp
- --icmp-type
- 8: echo-request
- 0: echo-reply
- #########ssh远程连接 #########
- [root@localhost ~]# iptables -A INPUT -d 192.168.0.100/32 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
- [root@localhost ~]# iptables -L -n
- Chain INPUT (policy ACCEPT)
- target prot opt source destination
- ACCEPT tcp -- 0.0.0.0/0 192.168.0.100 tcp dpt:22 state NEW,ESTABLISHED
- Chain FORWARD (policy ACCEPT)
- target prot opt source destination
- Chain OUTPUT (policy ACCEPT)
- target prot opt source destination
- [root@localhost ~]# iptables -A OUTPUT -s 192.168.0.100 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
- [root@localhost ~]# iptables -L -n
- Chain INPUT (policy ACCEPT)
- target prot opt source destination
- ACCEPT tcp -- 0.0.0.0/0 192.168.0.100 tcp dpt:22 state NEW,ESTABLISHED
- Chain FORWARD (policy ACCEPT)
- target prot opt source destination
- Chain OUTPUT (policy ACCEPT)
- target prot opt source destination
- ACCEPT tcp -- 192.168.0.100 0.0.0.0/0 tcp spt:22 state ESTABLISHED
- [root@localhost ~]# iptables -P INPUT DROP
- [root@localhost ~]# iptables -P OUTPUT DROP
- [root@localhost ~]# iptables -L -n
- Chain INPUT (policy DROP)
- target prot opt source destination
- ACCEPT tcp -- 0.0.0.0/0 192.168.0.100 tcp dpt:22 state NEW,ESTABLISHED
- Chain FORWARD (policy ACCEPT)
- target prot opt source destination
- Chain OUTPUT (policy DROP)
- target prot opt source destination
- ACCEPT tcp -- 192.168.0.100 0.0.0.0/0 tcp spt:22 state ESTABLISHED
- [root@localhost ~]# ping 127.0.0.1
- PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
- ping: sendmsg: Operation not permitted
- ping: sendmsg: Operation not permitted
- ping: sendmsg: Operation not permitted
- ping: sendmsg: Operation not permitted
- ping: sendmsg: Operation not permitted
- ping: sendmsg: Operation not permitted
- --- 127.0.0.1 ping statistics ---
- 6 packets transmitted, 0 received, 100% packet loss, time 5005ms
- [root@localhost ~]# iptables -I INPUT 1 -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
- [root@localhost ~]# iptables -I OUTPUT 1 -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
- [root@localhost ~]# ping 127.0.0.1
- PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
- 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=1.94 ms
- 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.161 ms
- 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.089 ms
- 64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.105 ms
- --- 127.0.0.1 ping statistics ---
- 4 packets transmitted, 4 received, 0% packet loss, time 3003ms
- rtt min/avg/max/mdev = 0.089/0.574/1.942/0.790 ms
- #web服务器访问
- [root@localhost ~]# iptables -A INPUT -d 192.168.0.100/32 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
- [root@localhost ~]# iptables -A OUTPUT -s 192.168.0.100 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT
- #此时就可以访问内部网络中的web服务器。
- [root@localhost ~]# iptables -L -n -v
- Chain INPUT (policy DROP 1784 packets, 162K bytes)
- pkts bytes target prot opt in out source destination
- 8 672 ACCEPT all -- * * 127.0.0.1 127.0.0.1
- 1107 80788 ACCEPT tcp -- * * 0.0.0.0/0 192.168.0.100 e NEW,ESTABLISHED
- 0 0 ACCEPT tcp -- * * 0.0.0.0/0 192.168.0.100 e NEW,ESTABLISHED
- Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
- pkts bytes target prot opt in out source destination
- Chain OUTPUT (policy DROP 65 packets, 4044 bytes)
- pkts bytes target prot opt in out source destination
- 8 672 ACCEPT all -- * * 127.0.0.1 127.0.0.1
- 582 62676 ACCEPT tcp -- * * 192.168.0.100 0.0.0.0/0 e ESTABLISHED
- 0 0 ACCEPT tcp -- * * 192.168.0.100 0.0.0.0/0 e ESTABLISHED
- [root@localhost ~]# iptables -I INPUT 2 -d 192.168.0.100 -p tcp -m state --state NEW,ESTABLISHED-m multiport --destination-ports 22,80 -j ACCEPT
- #这条规则把2,3 整合在一起,起到规则优化的作用
- #万能规则
- [root@localhost ~]# iptables -I OUTPUT 1 -m state --state ESTABLISHED -j ACCEPT
- [root@localhost ~]# iptables -L -n -v
此时就可以把OUTPUT链中的规则3,4 删除了,这样就减少了对规则条数的匹配,从而也对iptables起到了优化作用。
-m iprange 指定ip地址范围,
- --src-range 源地址范围
- --dst-range 目的地址范围
- [root@localhost ~]# iptables -A INPUT -m iprange --src-range 192.168.0.100-192.168.0.200 -p tcp --dport 23 -m state --state NEW -j ACCEPT
- [root@localhost ~]# iptables -L -n -v
- Chain INPUT (policy DROP 3745 packets, 340K bytes)
- pkts bytes target prot opt in out source destination
- 8 672 ACCEPT all -- * * 127.0.0.1 127.0.0.1
- 1335 87098 ACCEPT tcp -- * * 0.0.0.0/0 192.168.0.100 state NEW,ESTABLISHED multiport dports 22,80
- 1481 108K ACCEPT tcp -- * * 0.0.0.0/0 192.168.0.100 tcp dpt:22 state NEW,ESTABLISHED
- 0 0 ACCEPT tcp -- * * 0.0.0.0/0 192.168.0.100 tcp dpt:80 state NEW,ESTABLISHED
- 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 source IP range 192.168.0.100-192.168.0.200 tcp dpt:23 state NEW
- --limit rate 平均最大速率限定匹配到的数据包的个数
- --limit-burst number 平均最大数率的峰值 默认为5
- [root@localhost ~]# iptables -A INPUT -s 192.168.0.100 -p tcp --dport 80 -m state --state NEW -m limit --limit 3/minute --limit-burst 5 -j ACCAPT
- [root@localhost ~]# iptables -L -n -v
- Chain INPUT (policy DROP 3745 packets, 340K bytes)
- pkts bytes target prot opt in out source destination
- 8 672 ACCEPT all -- * * 127.0.0.1 127.0.0.1
- 1651 109K ACCEPT tcp -- * * 0.0.0.0/0 192.168.0.100 state NEW,ESTABLISHED multiport dports 22,80
- 1481 108K ACCEPT tcp -- * * 0.0.0.0/0 192.168.0.100 tcp dpt:22 state NEW,ESTABLISHED
- 0 0 ACCEPT tcp -- * * 0.0.0.0/0 192.168.0.100 tcp dpt:80 state NEW,ESTABLISHED
- 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 source IP range 192.168.0.100-192.168.0.200 tcp dpt:23 state NEW
- 0 0 ACCAPT tcp -- * * 192.168.0.100 0.0.0.0/0 tcp dpt:80 state NEW limit: avg 3/min burst 5
- # 平均没分钟三次连接,最大达到五次,超过则不予响应
- Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
- pkts bytes target prot opt in out source destination
- Chain OUTPUT (policy DROP 452 packets, 27288 bytes)
- pkts bytes target prot opt in out source destination
- 0 0 ACCEPT all -- * * 127.0.0.1 127.0.0.1
- 1521 147K ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state ESTABLISHED
- [root@localhost ~]# iptables -I OUTPUT 1 -m string --algo kmp --string "magedu" -j REJECT
- [root@localhost ~]# iptables -L -n
- Chain INPUT (policy DROP)
- target prot opt source destination
- ACCEPT all -- 127.0.0.1 127.0.0.1
- ACCEPT tcp -- 0.0.0.0/0 192.168.0.100 state NEW,ESTABLISHED multiport dports 22,80
- ACCEPT tcp -- 0.0.0.0/0 192.168.0.100 tcp dpt:22 state NEW,ESTABLISHED
- ACCEPT tcp -- 0.0.0.0/0 192.168.0.100 tcp dpt:80 state NEW,ESTABLISHED
- ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 source IP range 192.168.0.100-192.168.0.200 tcp dpt:23 state NEW
- DROP tcp -- 192.168.0.100 0.0.0.0/0 tcp dpt:80 state NEW limit: avg 3/min burst 5
- Chain FORWARD (policy ACCEPT)
- target prot opt source destination
- Chain OUTPUT (policy DROP)
- target prot opt source destination
- REJECT all -- 0.0.0.0/0 0.0.0.0/0 STRING match "magedu" ALGO name kmp TO 65535reject-with icmp-port-unreachable
- ACCEPT all -- 127.0.0.1 127.0.0.1
- ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state ESTABLISHED
- /etc/rc.d/init.d/iptables
- service iptables start: 启用保存的规则
- service iptables stop:
- /etc/sysconfig/iptables: 保存规则的文件
- /etc/sysconfig/iptables-config:向iptables脚本提供配置文件的文件
- service iptables save
- iptables-save > /etc/sysconfig/iptables.test #使用数据流重定向到到该文件
- iptables-restore < /etc/sysconfig/iptables.test #读取规则
利用iptables的recent模块来抵御DOS***
- #ssh: 远程连接
- iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 3 -j DROP
- iptables -I INPUT -p tcp --dport 22 -m state --state NEW -m recent --set --name SSH
- iptables -I INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 300 --hitcount 3 --name SSH -j DROP
- [root@localhost ~]# iptables -t -nat -A POSTROUTING -s 192.168.0.0/24 -oeth0 -j MASQUERADE
- #使用地址伪装来为内部网络snat,是内部网络能访问互联网
- [root@localhost ~]# iptables -P FORWARD DROP
- #关闭forward链,因为过滤策略将在此链上做
- [root@localhost ~]# iptables -A FORWARD -m iprange --src-range 192.168.0.10-192.168.0.20
- -m time --timestart 08:00 --timestop 20:00 -p tcp --dport 21 -j ACCEPT
- #工程部在上班时间只能访问ftp服务器,但是现在还不能访问到,需要对端口关联才可以
- [root@localhost ~]# iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
- #实现端口相关联
- [root@localhost ~]# modprobe ip_nat_ftp
- [root@localhost ~]# iptables -A FORWARD -s 192.168.0.0/24 -m time --timestart 20:01
- --timestop 07:59 -j ACCEPT
- #下班时间对各个部门无限制
关于iptables配置的心得相关推荐
- linux下IPTABLES配置详解
转载自 http://www.cnblogs.com/JemBai/archive/2009/03/19/1416364.html 如果你的IPTABLES基础知识还不了解,建议先去看看. 开始配置 ...
- 一个可以直接使用的可用iptables配置的stateless NAT实现
使用iptables配置stateless NAT?我没有搞错. 可能你根本不知道这么多NAT的实现细节,或者说根本不在乎,那么本文就当是一个"如何编写iptables模块"的练习 ...
- Android Linux自带iptables配置IP访问规则
利用Linux自带iptables配置IP访问规则,即可做到防火墙效果 初始化防火墙Chain规则 禁止指定app访问数据网络 禁止指定app访问wifi 转载于:https://www.cnblog ...
- IPTABLES简单应用说明和Linux下IPTABLES配置详解
如果你的IPTABLES基础知识还不了解,建议先去看看. 开始配置一个filter表的防火墙. (1)查看本机关于IPTABLES的设置情况 [root@tp ~]# iptables -L -n C ...
- CentOS下IPTABLES配置详解
iptables是与Linux内核集成的IP信息包过滤系统,其自带防火墙功能,我们在配置完服务器的角色功能后,需要修改iptables的配置. 配置CentOS和Ubuntu等linux服务器时需要对 ...
- iptables配置详解
-A参数是将规则写到现有链规则的最后面 -I 参数默认是将一条规则添加到现有规则链的最前面,当然也可以指定插入到第几行 行数可以用数字来指定 比如说将一条规则添加到某一条链的第三行 那么原来在 ...
- ip route / ip rule /iptables 配置策略路由
Linux 使用 ip route , ip rule , iptables 配置策略路由 要求192.168.0.100以内的使用 10.0.0.1 网关上网,其他IP使用 20.0.0.1 上网. ...
- Linux iptables 配置详解
一.配置一个filter表的防火墙 1. 查看本机关于 iptables 的设置情况 # iptables -L -n Chain INPUT (policy ACCEPT) target p ...
- Linux笔记-解决iptables配置后,本机无法访问本机,本机无法访问其他主机问题
解决两个问题: ①本机无法访问本机. ②本机无法访问其他主机. 原理:一般iptables配置的是eth设备,而本机用的是lo设备,所以lo设备也要操作. 解决本机无法访问本机: iptables - ...
- Linux操作系统下IPTables配置方法详解(转载)
为什么80%的码农都做不了架构师?>>> 配置一个filter表的防火墙 1.查看本机关于IPTABLES的设置情况 [root@tp ~]# iptables -L -n C ...
最新文章
- php 单例模式 序列化,php设计模式(二)单例模式
- 获取标签的src属性兼容性
- 天翼云从业认证(4.12) 天翼云医疗行业解决方案
- eclipse搭建多module项目的坑
- 小汤学编程之JavaEE学习day03——JSP组成结构、JSP原理、JSP生命周期、JSP九大内置对象、四大作用域、JSP的MVC模式
- [Alpha]Scrum Meeting#5
- 使用Sysmon分析宏病毒(Macros Downloader)
- 字节跳动和腾讯不正当竞争案将于深圳开庭 抖音:我们也是看新闻才知道本月24日要开庭...
- MVC中处理表单提交的方式(Ajax+Jquery)
- win32com excel转pdf
- NAND Flash批量数据烧录
- 【计算机网络】Web服务器的配置
- background系列属性
- linux内网穿透(内外网服务器端口映射)
- 大部分前端都在用的 ES6 代码简化技巧,你都在用吗?
- 学tlc和JAVA,#Java学习之路——第一部分总结
- STM32F4-DMA及相关配置
- 新一代的无线通信技术(转)
- 2019年别离职、别创业、别投资,收藏好这些热点好好干!
- 百度地图API批量地址转坐标(支持Excel上传,附案例)
热门文章
- pythonxml库_Python XML操作
- 开源java诊断工具,阿里开源Java 线上诊断工具 Arthas 实践
- 现控笔记(三):状态空间表达式的解
- Android——ImageView报错:不是可绘制对象(颜色或路径)
- L1-010 比较大小 (10 分)—团体程序设计天梯赛
- [bzoj3670] [NOI2014] [lg2375] 动物园
- C#与Javascript变量、函数之间的相互调用
- 浅谈assert()函数的用法
- priority_queue 优先队列 hdu裸题。
- Sicily 6271