iptables 命令学习

iptables,命令,学习 · 浏览次数 : 185

小编点评

**iptables 命令学习摘要** iptables 是一个内核态的网络过滤器,用于实现安全和路由等功能。 **四表五链的关系** - PREROUTING:匹配进站数据包,并将其转发到其他链。 - OUTPUT:匹配出站数据包,并将其转发到其他链。 - INPUT:匹配进站数据包,并将其处理或转发。 - FORWARD:匹配出站数据包,并将它们转发到其他链。 - OUTPUTMangle:匹配进站数据包,并将其封装为 Mangle 数据包,并将它们转发到其他链。 **常用命令** 1. **iptables -L -v:**列出所有规则。 2. **iptables -I chain rule_specification:**添加一条规则到指定的链。 3. **iptables -R chain rule_specification:**添加一条规则到指定的链,并将其设置为规则组。 4. **iptables -D chain rule_specification:**删除一条规则从指定的链。 5. **iptables -P chain target rule_specification:**将规则应用到特定的目标链。 6. **iptables -h:**显示帮助信息。 **其他选项** * **--append:**追加规则到指定的链。 * **--check:**检查规则是否存在。 * **--delete:**删除所有匹配规则的规则。 * **--delete:**删除指定的规则。 * **--replace:**替换匹配规则的规则。 * **--list:**列出所有规则。 * **--list-rules:**列出所有规则。 * **--flush:**清除所有规则。 * **--goto:**跳到指定的规则链。 * **--match:**使用匹配规则的规则。 * **--numeric:**使用数字格式表示 IP 地址和端口。 * **--out-interface:**将结果写入指定的网络接口。 * **--table:**使用指定的表进行规则匹配。

正文

iptables 命令学习


摘要

Linux 早起版本使用netfilter进行数据包过滤.
最新的版本开始改用 ebpf的方式进行内核编程式的包过滤.

netfilter 可以理解为内核态的一个处理机制
iptables 是在用户态进行管理netfilter配置的工具. 

也就可以理解为:
iptables 是管理netfilter的一个工具
便于实现安全以及路由等功能. 

四表

raw 表中的规则可以被哪些链使用:PREROUTING,OUTPUT
mangle 表中的规则可以被哪些链使用:PREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING
nat 表中的规则可以被哪些链使用:PREROUTING,OUTPUT,POSTROUTING(centos7中还有INPUT,centos6中没有)
filter 表中的规则可以被哪些链使用:INPUT,FORWARD,OUTPUT

五链

PREROUTING 的规则可以存在于:raw表,mangle表,nat表。
INPUT 的规则可以存在于:mangle表,filter表,(centos7中还有nat表,centos6中没有)。
FORWARD 的规则可以存在于:mangle表,filter表。
OUTPUT 的规则可以存在于:raw表mangle表,nat表,filter表。
POSTROUTING 的规则可以存在于:mangle表,nat表。

四表五链的关系--来自朱双印的blog

image


一些命令注意事项

关于关闭防火墙
很多blog说可以使用 iptables -F 的方式来清理防火墙的规则.
但是这一点有很多问题. 

建议需要先查看一下默认规则. 
iptables -L -v -n
注意最好是如下规则再进行-F 
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

如果是drop 后者是 reject 的话建议不要如此使用
建议可以使用如下命令进行修改
iptables -P INPUT ACCEPT

需要注意后面其实有很多注意事项: 
-L 罗列相关信息
-v 冗余模式
-P 设置链的策略 -P 链路 策略

后面会将帮助打出来一遍进行学习与查看. 

一些常用命令

1. 查看, 注意可以通过 -t 查看特定的filter
iptables -L -n -v
2. 屏蔽IP地址
iptables -A INPUT -s xxx.xxx.xxx.xxx -j DROP
3. 屏蔽指定的ip指定的协议
iptables -A INPUT -p tcp -s xxx.xxx.xxx.xxx -j DROP
4. 解封某个地址
iptables -D INPUT -s xxx.xxx.xxx.xxx -j DROP
5. 阻止特定的传出连接
iptables -A OUTPUT -p tcp --dport xxx -j DROP
6. 阻止特定的传入连接
iptables -A INPUT -p tcp --dport xxx -j ACCEPT
7. 一次性处理多个端口
iptables -A INPUT  -p tcp -m multiport --dports 22,80,443 -j ACCEPT
iptables -A OUTPUT -p tcp -m multiport --sports 22,80,443 -j ACCEPT
8. 使用IP地址范围
iptables -A OUTPUT -p tcp -d 192.168.100.0/24 --dport 22 -j ACCEPT
9. 设置端口转发
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 25 -j REDIRECT --to-port 2525
10. 放置flood攻击
iptables -A INPUT -p tcp --dport 80 -m limit --limit 100/minute --limit-burst 200 -j ACCEPT
11. 屏蔽ping
iptables -A INPUT -p icmp -i eth0 -j DROP
12. 开放loopback
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
13. 屏蔽指定mac地址
iptables -A INPUT -m mac --mac-source 00:00:00:00:00:00 -j DROP
14. 限制并发连接数
iptables -A INPUT -p tcp --syn --dport 22 -m connlimit --connlimit-above 3 -j REJECT
15. 清空规则, 可以指定链表进行清除.
iptables -t nat –F
iptables -f
16. 保存和缓存iptables规则
iptables-save > ~/iptables.rules
iptables-restore < ~/iptables.rules
17. 允许建立相关连接
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m conntrack --ctstate ESTABLISHED -j ACCEPT
18. 丢弃无效数据包
iptables -A INPUT -m conntrack --ctstate INVALID -j DROP
19. 组织发送邮件
iptables -A OUTPUT -p tcp --dports 25,465,587 -j REJECT
20. 组织连接到某块网卡
iptables -A INPUT -i eth0 -s xxx.xxx.xxx.xxx -j DROP

注意以上命令来源:csdn. 感谢原作者. 


关于防火墙的功能

iptables 不仅可以实现防火墙的策略
还可以实现 路由转发(软路由)
K8S比较早的版本网络栈都是采用的iptables
但是因为如果K8S的集群扩大之后会导致严重的性能衰退
iptables 管理起来非常复杂. 后来直接使用ipvs进行网络栈处理
所以iptables的功能是非常强大的. 

附录: 命令帮助

iptables -h
iptables v1.8.4

Usage: iptables -[ACD] chain rule-specification [options]
        iptables -I chain [rulenum] rule-specification [options]
        iptables -R chain rulenum rule-specification [options]
        iptables -D chain rulenum [options]
        iptables -[LS] [chain [rulenum]] [options]
        iptables -[FZ] [chain] [options]
        iptables -[NX] chain
        iptables -E old-chain-name new-chain-name
        iptables -P chain target [options]
        iptables -h (print this help information)

Commands:
Either long or short options are allowed.
  --append  -A chain            Append to chain
  --check   -C chain            Check for the existence of a rule
  --delete  -D chain            Delete matching rule from chain
  --delete  -D chain rulenum
                                Delete rule rulenum (1 = first) from chain
  --insert  -I chain [rulenum]
                                Insert in chain as rulenum (default 1=first)
  --replace -R chain rulenum
                                Replace rule rulenum (1 = first) in chain
  --list    -L [chain [rulenum]]
                                List the rules in a chain or all chains
  --list-rules -S [chain [rulenum]]
                                Print the rules in a chain or all chains
  --flush   -F [chain]          Delete all rules in  chain or all chains
  --zero    -Z [chain [rulenum]]
                                Zero counters in chain or all chains
  --new     -N chain            Create a new user-defined chain
  --delete-chain
             -X [chain]         Delete a user-defined chain
  --policy  -P chain target
                                Change policy on chain to target
  --rename-chain
             -E old-chain new-chain
                                Change chain name, (moving any references)
Options:
    --ipv4      -4              Nothing (line is ignored by ip6tables-restore)
    --ipv6      -6              Error (line is ignored by iptables-restore)
[!] --proto     -p proto        protocol: by number or name, eg. `tcp'
[!] --source    -s address[/mask][...]
                                source specification
[!] --destination -d address[/mask][...]
                                destination specification
[!] --in-interface -i input name[+]
                                network interface name ([+] for wildcard)
 --jump -j target
                                target for rule (may load target extension)
  --goto      -g chain
                               jump to chain with no return
  --match       -m match
                                extended match (may load extension)
  --numeric     -n              numeric output of addresses and ports
[!] --out-interface -o output name[+]
                                network interface name ([+] for wildcard)
  --table       -t table        table to manipulate (default: `filter')
  --verbose     -v              verbose mode
  --wait        -w [seconds]    maximum wait to acquire xtables lock before give up
  --wait-interval -W [usecs]    wait time to try to acquire xtables lock
                                default is 1 second
  --line-numbers                print line numbers when listing
  --exact       -x              expand numbers (display exact values)
[!] --fragment  -f              match second or further fragments only
  --modprobe=<command>          try to insert modules using this command
  --set-counters PKTS BYTES     set the counter during insert/append
[!] --version   -V              print package version.

与iptables 命令学习相似的内容:

iptables 命令学习

iptables 命令学习 摘要 Linux 早起版本使用netfilter进行数据包过滤. 最新的版本开始改用 ebpf的方式进行内核编程式的包过滤. netfilter 可以理解为内核态的一个处理机制 iptables 是在用户态进行管理netfilter配置的工具. 也就可以理解为: ipta

[转帖]iptables命令详解和举例(完整版)

1、防火墙概述 防火墙,其实说白了讲,就是用于实现Linux下访问控制的功能的,它分为硬件的或者软件的防火墙两种。无论是在哪个网络中,防火墙工作的地方一定是在网络的边缘。而我们的任务就是需要去定义到底防火墙如何工作,这就是防火墙的策略,规则,以达到让它对出入网络的IP、数据进行检测。 目前市面上比较

[转帖]安全技术和iptables防火墙

目录 安全技术Netfilter防火墙工具介绍iptablesfirewalldnftables iptables的组成概述netfilter与iptables关系 iptables的四表五链结构介绍iptables安装iptables的命令格式数据包的常见控制类型iptables 命令常用管理选项

[转帖]iptables 执行清除命令 iptables -F 要非常小心的

使用 /sbin/iptables -F 要小心,搞不好,你就马上同服务器断开连接了 以下是来自 http://wiki.ubuntu.org.cn/IptablesHowTo 上的说明 可以通过/sbin/iptables -F清除所有规则来暂时停止防火墙: (警告:这只适合在没有配置防火墙的环境

[转帖]ipset命令介绍与基本使用

一. 介绍 ipset命令是用于管理内核中IP sets模块的,如iptables之于netfilter。ipset字面意思是一些IP地址组成一个集合(set)。但是ipset用于用于存储IP地址,整个子网,端口号(TCP/UDP),MAC地址,网络接口名或者上述这些的组合。ipset主要是由ipt

[转帖]1.IPtable基础命令总结

https://www.cnblogs.com/kcxg/p/10350870.html 规则查询 #查看对应表中的所有规则 iptables -t 表名 -L #查看表的指定链中的规则 iptables -t 表名 -L 链名 #查看对应表中的所有规则, -v显示跟详细的信息 iptables -

[转帖]ubuntu下配置iptables、ufw端口转发

iptables 端口转发(CentOS) 注意:一来一去 在中转服务器操作 iptables -t nat -A PREROUTING -p tcp --dport [端口号] -j DNAT --to-destination [目标IP]iptables -t nat -A POSTROUTIN

[转帖]iptables开放指定端口

https://www.jianshu.com/p/5b44dd20484c 由于业务的需要, MySQL,Redis,mongodb等应用的端口需要我们手动操作开启 下面以 MySQL 为例,开启 3306 端口的远程访问,其他端口类似。 方式一:通过修改iptables文件方式 编辑配置文件 #

[转帖]iptables的四表五链与NAT工作原理

本文主要介绍了iptables的基本工作原理和四表五链等基本概念以及NAT的工作原理。 1、iptables简介 我们先来看一下netfilter官网对iptables的描述: iptables is the userspace command line program used to config

[转帖]iptables ip_conntrack_max 调整

https://www.diewufeiyang.com/post/583.html 一、概念 -允许的最大跟踪连接条目:CONNTRACK_MAX(默认值是 2^16=65536 ) -存储跟踪连接条目列表的哈西表的大小:HASHSIZE -每个哈西表的条目(叫一个bucket),包含了一个链接起