一、认识iptables


二、Iptables命令
2.1、语法:iptables -t table 命令 chain rules -j target
table:有filter、nat、mangle,默认是filter
命令:
-L 或 --list 查看iptables规则列表
[root@appex ~]# iptables -t filter -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
-v 显示更多设置,-n 以数字形式显示IP地址和端口
[root@appex ~]#iptables -L FORWARD -nv
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 ACCEPT all -- 172.16.2.0/24 172.16.3.0/24 limit: avg 3/hour burst 1000
0 0 ACCEPT all -- 172.16.2.0/24 172.16.3.0/24 limit: avg 303/sec burst 5
0 0 DROP all -- 172.16.2.0/24 172.16.3.0/24 limit: avg 303/sec burst 5
-P 或 --policy 定义默认策略
[root@appex ~]# iptables -t filter -P FORWARD DROP
[root@appex ~]#iptables -t filter -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy DROP)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@appex ~]# iptables -t filter -P FORWARD ACCEPT
[root@appex ~]#iptables -t filter -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
-A 或--append 在规则列表的最后增加一条规则
[root@appex ~]#iptables -t filter -A FORWARD -p icmp -j DROP
[root@appex ~]#iptables -t filter -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy DROP)
target prot opt source destination
DROP icmp -- anywhere anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
-I或--insert 在规则列表的最前面插入一条规则
[root@appex ~]# iptables -t filter -I FORWARD 2 -p icmp -j ACCEPT
[root@appex ~]# iptables -t filter -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy DROP)
target prot opt source destination
DROP icmp -- anywhere anywhere
ACCEPT icmp -- anywhere anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
-R或--replace 替换规则列表中的某条规则
[root@appex ~]#iptables -t filter -R FORWARD 2 -p icmp -j DROP
[root@appex ~]#iptables -t filter -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy DROP)
target prot opt source destination
DROP icmp -- anywhere anywhere
DROP icmp -- anywhere anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
-D或--delete 从规则列表中删除一条规则
[root@appex ~]#iptables -t filter -D FORWARD 2
[root@appex ~]#iptables -t filter -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy DROP)
target prot opt source destination
DROP icmp -- anywhere anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
-F或--flush 删除表中所有的规则
[root@appex ~]#iptables -t filter -F
[root@appex ~]#iptables -t filter -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy DROP)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
2.2、Iptables匹配选项
-i或--in-interface 指定数据包从哪块网络接口进入,如eth0、eth1等
-o或--out-interface 指定数据包从哪块网络接口输出,如eth0、eth1等
[root@appex ~]# iptables -t filter -I FORWARD -i eth0 -j DROP
[root@appex ~]# iptables -t filter -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
DROP all -- anywhere anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
-p或--protocol 指定数据包匹配的协议,如TCP、UDP、ICMP等
-s或--source 指定数据包匹配的源地址
-d或--destination 指定数据包匹配的目的地址
--sport 指定数据包匹配的源端口号,可以使用”起始端口号:结束端口号”的格式指定一个范围
--dport 指定数据包匹配的目标端口号,可以使用”起始端口号:结束端口号”的格式指定一个范围
[root@appex ~]# iptables -t filter -I FORWARD -p tcp -s 10.0.0.90/32 -d 10.0.0.80/32 --dport 3389 -j DROP
[root@appex ~]# iptables -t filter -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
DROP tcp -- 10.0.0.90 10.0.0.80 tcp dpt:ms-wbt-server
DROP all -- anywhere anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@appex ~]#iptables -t filter -I FORWARD -p tcp -s 10.0.0.0/24 -d 10.10.10.0/24 --dport 3389 -j DROP
[root@appex ~]# iptables -t filter -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
DROP tcp -- 10.0.0.0/24 10.0.10.0/24 tcp dpt:ms-wbt-server
DROP tcp -- 10.0.0.90 10.0.0.80 tcp dpt:ms-wbt-server
DROP all -- anywhere anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
2.3、Iptables使用扩展选项
限制网速:-m limit --limit
控制瞬间爆发流量:-m limit --limit-burst
[root@appex ~]# iptables -t filter -F
[root@appex ~]#iptables -t filter -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@appex ~]# iptables -t filter -I FORWARD -s 172.16.2.0/24 -d 172.16.3.0/24 -m limit --limit 300/second -j ACCEPT
[root@appex ~]#iptables -t filter -A FORWARD -s 172.16.2.0/24 -d 172.16.3.0/24 -m limit --limit 300/second -j DROP //超过的就drop
[root@appex ~]#iptables -t filter -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 172.16.2.0/24 172.16.3.0/24 limit: avg 303/sec burst 5
DROP all -- 172.16.2.0/24 172.16.3.0/24 limit: avg 303/sec burst 5
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@appex ~]#iptables -t filter -I FORWARD -s 172.16.2.0/24 -d 172.16.3.0/24 -m limit --limit-burst 1000 -j ACCEPT
[root@appex ~]#iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 172.16.2.0/24 172.16.3.0/24 limit: avg 3/hour burst 1000
ACCEPT all -- 172.16.2.0/24 172.16.3.0/24 limit: avg 303/sec burst 5
DROP all -- 172.16.2.0/24 172.16.3.0/24 limit: avg 303/sec burst 5
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
2.4、处理动作
-j 参数用来指定要进行的处理动作,常用的处理动作包括:ACCEPT、REJECT、DROP、REDIRCT、MASQUERADE、LOG、DNAT、SNAT、MIRROR、QUEUE、RETURN、MARK
Filter表能使用的主要动作:
ACCEPT:将封包放行,进行完此处理动作后,将不再匹配其他规则,直接跳往下一个规则链
REJECT:拦截该封包,并传送封包通知对方,进行完此处理动作后,将不再匹配其他规则,直接中断过滤程序
DROP:丢弃封包不予处理,进行完此处理动作后,将不再匹配其他规则,直接中断过滤程序。
三、保存和还原iptables设置
3.1、保存修改的iptables到配置文件中
[root@appex ~]# /etc/rc.d/init.d/iptables save
3.2、查看iptables的配置文件
[root@appex ~]# cat /etc/sysconfig/iptables
3.3、保存修改的iptables到一个文件中及从文件中导入到iptables中
[root@appex ~]# iptables-save >iptables.conf1
[root@appex ~]# iptables-restore< iptables.conf1
四、配置NAT实现网络地址转换
[root@appex ~]# ifconfig eth0:0 10.0.0.81 netmask 255.255.255.0
[root@appex ~]#ip addr show eth0:0
2: eth0: < BROADCAST,MULTICAST,UP,LOWER_UP > mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:21:85:0e brd ff:ff:ff:ff:ff:ff
inet 10.0.0.80/24 brd 10.0.0.255 scope global eth0
inet 10.0.0.81/24 brd 10.0.0.255 scope global secondary eth0:0
inet6 fe80::20c:29ff:fe21:850e/64 scope link
valid_lft forever preferred_lft forever
[root@appex ~]# iptables -t nat -L POSTROUTING
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
[root@appex ~]# iptables -t nat -A POSTROUTING -s 10.0.10.0/24 -o eth0 -j SNAT --to-source 10.0.0.80-10.0.0.81
[root@appex ~]#iptables -t nat -L POSTROUTING -nv
Chain POSTROUTING (policy ACCEPT 3 packets, 205 bytes)
pkts bytes target prot opt in out source destination
0 0 SNAT all -- - eth0 10.0.10.0/24 0.0.0.0/0 to:10.0.0.80-10.0.0.81
五、mangle表的应用
--ttl-inc 1
--ttl-dec 2
--ttl-set 40
[root@appex ~]#iptables -t mangle -L
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
Chain INPUT (policy ACCEPT)
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
[root@appex ~]# iptables -t mangle -I PREROUTING -i eth0 -j TTL --ttl-inc 1
[root@appex ~]#iptables -t mangle -I PREROUTING -i eth0 -j TTL --ttl-dec 2
[root@appex ~]# iptables -t mangle -A PREROUTING -i eth0 -j TTL --ttl-set 40
[root@appex ~]#iptables -t mangle -L PREROUTING
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
TTL all -- anywhere anywhere TTL decrement by 2
TTL all -- anywhere anywhere TTL increment by 1
TTL all -- anywhere anywhere TTL set to 40

转载于:https://blog.51cto.com/13162375/2095290

LINUX防火墙iptables基本命令相关推荐

  1. Linux防火墙iptables学习

    http://blog.chinaunix.net/uid-9950859-id-98277.html 要在网上传输的数据会被分成许多小的数据包,我们一旦接通了网络,会有很多数据包进入,离开,或者经过 ...

  2. linux 防火墙iptables简明教程

    前几天微魔部落再次遭受到个别别有用心的攻击者的攻击,顺便给自己充个电,复习了一下linux下常见的防火墙iptables的一些内容,但是无奈网上的很多教程都较为繁琐,本着简明化学习的目的,微魔为大家剔 ...

  3. 共创Linux防火墙,Linux防火墙iptables简明教程

    前几天微魔部落再次遭受到个别别有用心的攻击者的攻击,顺便给自己充个电,复习了一下linux下常见的防火墙iptables的一些内容,但是无奈网上的很多教程都较为繁琐,本着简明化学习的目的,微魔为大家剔 ...

  4. linux防火墙策略文件夹,Linux防火墙iptables的策略

    iptables策略 iptables -L #查看现有防火墙所有策略 iptables -F #清除现有防火墙策略 只允许特定流量通过,禁用其他流量 1.允许SSH流量(重要) iptables - ...

  5. linux防火墙--iptables(三)

    七.SNAT源地址转换 ·Source Network Address Translation ·修改数据包的源地址 ·仅用于nat表的POSTROUTING链 Example:局域网共享公网IP上网 ...

  6. linux 防火墙 iptables的简单使用

    linux 防火墙 iptables的简单使用 [root@web web]# uname -a Linux web 2.6.9-67.ELsmp #1 SMP Wed Nov 7 13:58:04 ...

  7. linux防火墙ip黑名单,【转】Linux防火墙(iptables)之黑名单

    iptables删除规则 So if you would like to delete second rule : iptables -D INPUT 2 ---------------------- ...

  8. linux防火墙常用控制协议,Linux防火墙iptables的基础

    一.网络访问控制 1.Linux一般都是作为服务器系统使用,对外提供一些基于网络的服务 2.通常我们都需要对服务器进行一些网络访问控制,类似防火墙的功能 3.常见的访问控制包括:哪些IP可以访问服务器 ...

  9. linux防火墙iptables

    目录 一.防火墙iptables的概述 1.防火墙的作用 2.netfilter和iptables的关系 3.四表五链 4.常用的控制类型 二.配置基础iptables 1.配置格式 2.查看配置列表 ...

最新文章

  1. 正则化技巧:标签平滑(Label Smoothing)以及在 PyTorch 中的实现
  2. 文件的特殊权限:suid sgid sticky
  3. AWS — AWS EKS
  4. JavaScript数据结构与算法——列表详解(上)
  5. SPT20 协议_协议离婚协议书模板锦集6篇
  6. GNU make manual 翻译( 一百六十)
  7. linux共享软件_为什么 linux 要用 tar.gz,很少用 7z 或 zip?
  8. oracle 函数 abs,Oracle 函数(八)
  9. Linux CTRL+ALT+DELETE
  10. fast neural style transfer图像风格迁移基于tensorflow实现
  11. JAVA之stream汪文君_Java8新特性之Stream API
  12. 使用nssm管理Windows服务
  13. 如何测试聊天机器人的 10 个最佳实践
  14. IDEA右键菜单管理--懒人专用
  15. android 触摸屏干扰,如何解决电容触摸屏的抗干扰问题?
  16. 技术VS管理,哪个更重要?
  17. Gephi画无向图和有向图(显示节点和边序号)
  18. 一款小巧精致的编辑和编译工具--TextPad
  19. UML交流群2月14日讨论内容!
  20. JAVA Web实现注册登录系统

热门文章

  1. 行业风向标|2023中国(深圳)国际马拉松运动展览会
  2. 【云原生 • Kubernetes】认识 k8s、k8s 架构、核心概念点介绍
  3. opencv 双边滤波算法(Bilateral Filters)原理及实现
  4. 估值超10亿 同行者牵手腾讯 加速车联网布局
  5. 新浪微博数据库是如何设计的
  6. mac安装pyinstaller和py2app打包python程序生成app应用程序
  7. 不正经 之 性感美女教你如何打领带
  8. android 高通分区表,高通智能机分区表详细解析
  9. Python 在Online Judge上自动挂题脚本
  10. 德国最大的光纤项目开建