zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

iptables详细说明

2023-03-07 09:42:36 时间

iptables工作原理

netfilter/iptables IP 信息包过滤系统是一种功能强大的工具,可用于添加、编辑和除去规则,这些规则是在做信息包过滤决定时,防火墙所遵循和组成的规则。这些规则存储在专用的信息包过滤表中,而这些表集成在 Linux 内核中。在信息包过滤表中,规则被分组放在所谓的链(chain)中。
虽然 netfilter/iptables IP 信息包过滤系统被称为单个实体,但它实际上由两个组件 netfilter 和 iptables 组成。

  1. netfilter 处于内核空间, 是内核的一部分, 由一些信息包过滤表组成, 这些表包含内核用来控制信息包过滤处理的规则集。
  2. iptables 组件是一种工具, 处于用户空间, 它使插入、修改和除去信息包过滤表中的规则变得容易。

iptables包含4个表, 5个链, 其中表是按照对数据包的操作区分的,链是按照不同的Hook点来区分的,表和链实际上是netfilter的两个维度.

Iptables的4个表

4个表filter,nat,mangle,raw. 默认表是filter, 没有指定表的时候就是filter表. 表的处理优先级: raw > mangle > nat > filter

  • filter:一般的过滤功能
  • nat:用于nat功能(端口映射,地址映射等)
  • mangle:用于对特定数据包的修改
  • raw:有限级最高,设置raw时一般是为了不再让iptables做数据包的链接跟踪处理,提高性能

RAW 表只使用在PREROUTING链和OUTPUT链上,因为优先级最高,从而可以对收到的数据包在连接跟踪前进行处理。一但用户使用了RAW表,在某个链 上,RAW表处理完后,将跳过NAT表和 ip_conntrack处理,即不再做地址转换和数据包的链接跟踪处理了. RAW表可以应用在那些不需要做nat的情况下,以提高性能。如大量访问的web服务器,可以让80端口不再让iptables做数据包的链接跟踪处理,以提高用户的访问速度。

Iptables的5个链

  • PREROUTING: 数据包进入路由表之前
  • INPUT:通过路由表后目的地为本机
  • FORWARD:通过路由表后,目的地不为本机
  • OUTPUT:由本机产生,向外转发
  • POSTROUTIONG:发送到网卡接口之前

iptables的工作机制

从上面的发展我们知道了作者选择了5个位置,来作为控制的地方,但是你有没有发现,其实前三个位置已经基本上能将路径彻底封锁了,但是为什么已经在进出的口设置了关卡之后还要在内部卡呢? 由于数据包尚未进行路由决策,还不知道数据要走向哪里,所以在进出口是没办法实现数据过滤的。所以要在内核空间里设置转发的关卡,进入用户空间的关卡,从用户空间出去的关卡。那么,既然他们没什么用,那我们为什么还要放置他们呢?因为我们在做NAT和DNAT的时候,目标地址转换必须在路由之前转换。所以我们必须在外网而后内网的接口处进行设置关卡。

这五个位置也被称为五个钩子函数(hook functions),也叫五个规则链。

  1. PREROUTING (路由前)
  2. INPUT (数据包流入口)
  3. FORWARD (转发管卡)
  4. OUTPUT(数据包出口)
  5. POSTROUTING(路由后)
    这是NetFilter规定的五个规则链,任何一个数据包,只要经过本机,必将经过这五个链中的其中一个链。

防火墙的策略

防火墙策略一般分为两种,一种叫“通”策略,一种叫“堵”策略,通策略,默认门是关着的,必须要定义谁能进。堵策略则是,大门是洞开的,但是你必须有身份认证,否则不能进。所以我们要定义,让进来的进来,让出去的出去,所以通,是要全通,而堵,则是要选择。当我们定义的策略的时候,要分别定义多条功能,其中:定义数据包中允许或者不允许的策略,filter过滤的功能,而定义地址转换的功能的则是nat选项。为了让这些功能交替工作,我们制定出了“表”这个定义,来定义、区分各种不同的工作功能和处理方式。

我们现在用的比较多个功能有3个:

  1. filter 定义允许或者不允许的
  2. nat 定义地址转换的
  3. mangle功能:修改报文原数据

我们修改报文原数据就是来修改TTL的。能够实现将数据包的元数据拆开,在里面做标记/修改内容的。而防火墙标记,其实就是靠mangle来实现的。

小扩展:
对于filter来讲一般只能做在3个链上:INPUT ,FORWARD ,OUTPUT
对于nat来讲一般也只能做在3个链上:PREROUTING ,OUTPUT ,POSTROUTING
而mangle则是5个链都可以做:PREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING

iptables/netfilter(这款软件)是工作在用户空间的,它可以让规则进行生效的,本身不是一种服务,而且规则是立即生效的。

而iptables现在被做成了一个服务,可以进行启动,停止的。启动,则将规则直接生效,停止,则将规则撤销。

iptables还支持自己定义链。但是自己定义的链,必须是跟某种特定的链关联起来的。在一个关卡设定,指定当有数据的时候专门去找某个特定的链来处理,当那个链处理完之后,再返回。接着在特定的链中继续检查。

注意:规则的次序非常关键,谁的规则越严格,应该放的越靠前,而检查规则的时候,是按照从上往下的方式进行检查的。

规则的写法:

iptables定义规则的方式比较复杂:

iptables [-t table] {-A|-C|-D} chain rule-specification
iptables [-t table] -I chain [rulenum] rule-specification
iptables [-t table] -R chain rulenum rule-specification
iptables [-t table] -D chain rulenum
iptables [-t table] -S [chain [rulenum]]
iptables [-t table] {-F|-L|-Z} [chain [rulenum]] [options...]
iptables [-t table] -N chain
iptables [-t table] -X [chain]
iptables [-t table] -P chain target
iptables [-t table] -E old-chain-name new-chain-name
rule-specification = [matches...] [target]
match = -m matchname [per-match-options]
target = -j targetname [per-target-options]   

-t table 对应的table类型

filter:
        This is the default table (if no -t option is passed). It contains the built-in chains INPUT (for packets destined to local sockets), FORWARD (for packets being routed through the box), and OUTPUT (for locally-generated packets). 
nat:
        This table is consulted when a packet that creates a new connection is encountered. It consists of three built-ins: PREROUTING (for altering packets as soon as they come in), OUTPUT (for altering locally-generated packets before routing), and POSTROUTING (for altering packets as they are about to go out). 
mangle:
        This table is used for specialized packet alteration. Until kernel 2.4.17 it had two built-in chains: PREROUTING (for altering incoming packets before routing) and OUTPUT (for altering locally-generated packets before routing). Since kernel 2.4.18, three other built-in chains are also supported: INPUT (for packets coming into the box itself), FORWARD (for altering packets being routed through the box), and POSTROUTING (for altering packets as they are about to go out). 
raw:
        This table is used mainly for configuring exemptions from connection tracking in combination with the NOTRACK target. It registers at the netfilter hooks with higher priority and is thus called before ip_conntrack, or any other IP tables. It provides the following built-in chains: PREROUTING (for packets arriving via any network interface) OUTPUT (for packets generated by local processes) 
security:
        This table is used for Mandatory Access Control (MAC) networking rules, such as those enabled by the SECMARK and CONNSECMARK targets. Mandatory Access Control is implemented by Linux Security Modules such as SELinux. The security table is called after the filter table, allowing any Discretionary Access Control (DAC) rules in the filter table to take effect before MAC rules. This table provides the following built-in chains: INPUT (for packets coming into the box itself), OUTPUT (for altering locally-generated packets before routing), and FORWARD (for altering packets being routed through the box). 

COMMAND:定义如何对规则进行管理

-A, --append chain rule-specification
    Append one or more rules to the end of the selected chain. When the source and/or destination names resolve to more than one address, a rule will be added for each possible address combination. 
-C, --check chain rule-specification
    Check whether a rule matching the specification does exist in the selected chain. This command uses the same logic as -D to find a matching entry, but does not alter the existing iptables configuration and uses its exit code to indicate success or failure. 
-D, --delete chain rule-specification
-D, --delete chain rulenum
    Delete one or more rules from the selected chain. There are two versions of this command: the rule can be specified as a number in the chain (starting at 1 for the first rule) or a rule to match. 
-I, --insert chain [rulenum] rule-specification
    Insert one or more rules in the selected chain as the given rule number. So, if the rule number is 1, the rule or rules are inserted at the head of the chain. This is also the default if no rule number is specified. 
-R, --replace chain rulenum rule-specification
    Replace a rule in the selected chain. If the source and/or destination names resolve to multiple addresses, the command will fail. Rules are numbered starting at 1. 
-L, --list [chain]
    List all rules in the selected chain. If no chain is selected, all chains are listed. Like every other iptables command, it applies to the specified table (filter is the default), so NAT rules get listed by

     iptables -t nat -n -L

    Please note that it is often used with the -n option, in order to avoid long reverse DNS lookups. It is legal to specify the -Z (zero) option as well, in which case the chain(s) will be atomically listed and zeroed. The exact output is affected by the other arguments given. The exact rules are suppressed until you use

     iptables -L -v

-S, --list-rules [chain]
    Print all rules in the selected chain. If no chain is selected, all chains are printed like iptables-save. Like every other iptables command, it applies to the specified table (filter is the default). 
-F, --flush [chain]
    Flush the selected chain (all the chains in the table if none is given). This is equivalent to deleting all the rules one by one. 
-Z, --zero [chain [rulenum]]
    Zero the packet and byte counters in all chains, or only the given chain, or only the given rule in a chain. It is legal to specify the -L, --list (list) option as well, to see the counters immediately before they are cleared. (See above.) 
-N, --new-chain chain
    Create a new user-defined chain by the given name. There must be no target of that name already. 
-X, --delete-chain [chain]
    Delete the optional user-defined chain specified. There must be no references to the chain. If there are, you must delete or replace the referring rules before the chain can be deleted. The chain must be empty, i.e. not contain any rules. If no argument is given, it will attempt to delete every non-builtin chain in the table. 
-P, --policy chain target
    Set the policy for the chain to the given target. See the section TARGETS for the legal targets. Only built-in (non-user-defined) chains can have policies, and neither built-in nor user-defined chains can be policy targets. 
-E, --rename-chain old-chain new-chain
    Rename the user specified chain to the user supplied name. This is cosmetic, and has no effect on the structure of the table. 
-h
    Help. Give a (currently very brief) description of the command syntax. 

chain:指定你接下来的规则到底是在哪个链上操作的,当定义策略的时候,是可以省略的

参数

-4, --ipv4
    This option has no effect in iptables and iptables-restore. 
-6, --ipv6
    If a rule using the -6 option is inserted with (and only with) iptables-restore, it will be silently ignored. Any other uses will throw an error. This option allows to put both IPv4 and IPv6 rules in a single rule file for use with both iptables-restore and ip6tables-restore. 
[!] -p, --protocol protocol
    The protocol of the rule or of the packet to check. The specified protocol can be one of tcp, udp, udplite, icmp, esp, ah, sctp or the special keyword "all", or it can be a numeric value, representing one of these protocols or a different one. A protocol name from /etc/protocols is also allowed. A "!" argument before the protocol inverts the test. The number zero is equivalent to all. "all" will match with all protocols and is taken as default when this option is omitted. 
[!] -s, --source address[/mask][,...]
    Source specification. Address can be either a network name, a hostname, a network IP address (with /mask), or a plain IP address. Hostnames will be resolved once only, before the rule is submitted to the kernel. Please note that specifying any name to be resolved with a remote query such as DNS is a really bad idea. The mask can be either a network mask or a plain number, specifying the number of 1's at the left side of the network mask. Thus, a mask of 24 is equivalent to 255.255.255.0. A "!" argument before the address specification inverts the sense of the address. The flag --src is an alias for this option. Multiple addresses can be specified, but this will expand to multiple rules (when adding with -A), or will cause multiple rules to be deleted (with -D). 
[!] -d, --destination address[/mask][,...]
    Destination specification. See the description of the -s (source) flag for a detailed description of the syntax. The flag --dst is an alias for this option. 
-m, --match match
    Specifies a match to use, that is, an extension module that tests for a specific property. The set of matches make up the condition under which a target is invoked. Matches are evaluated first to last as specified on the command line and work in short-circuit fashion, i.e. if one extension yields false, evaluation will stop. 
-j, --jump target
    This specifies the target of the rule; i.e., what to do if the packet matches it. The target can be a user-defined chain (other than the one this rule is in), one of the special builtin targets which decide the fate of the packet immediately, or an extension (see EXTENSIONS below). If this option is omitted in a rule (and -g is not used), then matching the rule will have no effect on the packet's fate, but the counters on the rule will be incremented. 
-g, --goto chain
    This specifies that the processing should continue in a user specified chain. Unlike the --jump option return will not continue processing in this chain but instead in the chain that called us via --jump. 
[!] -i, --in-interface name
    Name of an interface via which a packet was received (only for packets entering the INPUT, FORWARD and PREROUTING chains). When the "!" argument is used before the interface name, the sense is inverted. If the interface name ends in a "+", then any interface which begins with this name will match. If this option is omitted, any interface name will match. 
[!] -o, --out-interface name
    Name of an interface via which a packet is going to be sent (for packets entering the FORWARD, OUTPUT and POSTROUTING chains). When the "!" argument is used before the interface name, the sense is inverted. If the interface name ends in a "+", then any interface which begins with this name will match. If this option is omitted, any interface name will match. 
[!] -f, --fragment
    This means that the rule only refers to second and further fragments of fragmented packets. Since there is no way to tell the source or destination ports of such a packet (or ICMP type), such a packet will not match any rules which specify them. When the "!" argument precedes the "-f" flag, the rule will only match head fragments, or unfragmented packets. 
-c, --set-counters packets bytes
    This enables the administrator to initialize the packet and byte counters of a rule (during INSERT, APPEND, REPLACE operations). 

CRETIRIA:指定匹配标准
-j ACTION :指定如何进行处理, 比如:

#不允许172.16.0.0/24的进行访问。
iptables -t filter -A INPUT -s 172.16.0.0/16 -p udp --dport 53 -j DROP
#如果想拒绝的更彻底:
iptables -t filter -R INPUT 1 -s 172.16.0.0/16 -p udp --dport 53 -j REJECT
#查看定义规则的详细信息
iptables -L -n
iptables -t nat -L -n

详解COMMAND:

链管理命令(这都是立即生效的)

-P :设置默认策略的(设定默认门是关着的还是开着的)
  默认策略一般只有两种
  iptables -P INPUT (DROP|ACCEPT)  默认是关的/默认是开的
  比如:
  iptables -P INPUT DROP 这就把默认规则给拒绝了。并且没有定义哪个动作,所以关于外界连接的所有规则包括Xshell连接之类的,远程连接都被拒绝了。
-F: FLASH,清空规则链的(注意每个链的管理权限)
  iptables -t nat -F PREROUTING
  iptables -t nat -F 清空nat表的所有链
-N:NEW 支持用户新建一个链
    iptables -N inbound_tcp_web 表示附在tcp表上用于检查web的。
-X: 用于删除用户自定义的空链
    使用方法跟-N相同,但是在删除之前必须要将里面的链给清空昂了
-E:用来Rename chain主要是用来给用户自定义的链重命名
    -E oldname newname
-Z:清空链,及链中默认规则的计数器的(有两个计数器,被匹配到多少个数据包,多少个字节)
    iptables -Z :清空

规则管理命令

-A:追加,在当前链的最后新增一个规则
-I num : 插入,把当前规则插入为第几条。
  -I 3 :插入为第三条
-R num:Replays替换/修改第几条规则
  格式:iptables -R 3 …………
-D num:删除,明确指定删除第几条规则

查看管理命令 “-L”

附加子命令

-n:以数字的方式显示ip,它会将ip直接显示出来,如果不加-n,则会将ip反向解析成主机名。
-v:显示详细信息
-vv
-vvv :越多越详细
-x:在计数器上显示精确值,不做单位换算
--line-numbers : 显示规则的行号
-t nat:显示所有的关卡的信息

详解匹配标准

通用匹配:源地址目标地址的匹配

    -s:指定作为源地址匹配,这里不能指定主机名称,必须是IP
        IP | IP/MASK | 0.0.0.0/0.0.0.0
        而且地址可以取反,加一个“!”表示除了哪个IP之外
    -d:表示匹配目标地址
    -p:用于匹配协议的(这里的协议通常有3种,TCP/UDP/ICMP)
    -i eth0:从这块网卡流入的数据
        流入一般用在INPUT和PREROUTING上
    -o eth0:从这块网卡流出的数据
        流出一般在OUTPUT和POSTROUTING上

扩展匹配

隐含扩展:对协议的扩展

    -p tcp :TCP协议的扩展。一般有三种扩展
    --dport XX-XX:指定目标端口,不能指定多个非连续端口,只能指定单个端口,比如
    --dport 21  或者 --dport 21-23 (此时表示21,22,23)
    --sport:指定源端口
    --tcp-fiags:TCP的标志位(SYN,ACK,FIN,PSH,RST,URG)
        对于它,一般要跟两个参数:
        1.检查的标志位
        2.必须为1的标志位
        --tcpflags syn,ack,fin,rst syn   =    --syn
        表示检查这4个位,这4个位中syn必须为1,其他的必须为0。所以这个意思就是用于检测三次握手的第一次包的。对于这种专门匹配第一包的SYN为1的包,还有一种简写方式,叫做--syn
    -p udp:UDP协议的扩展
        --dport
        --sport
    -p icmp:icmp数据报文的扩展
        --icmp-type:
        echo-request(请求回显),一般用8 来表示
        所以 --icmp-type 8 匹配请求回显数据包
        echo-reply (响应的数据包)一般用0来表示

显式扩展(-m)

扩展各种模块

    -m multiport:表示启用多端口扩展
    之后我们就可以启用比如 --dports 21,23,80

详解-j ACTION

常用的ACTION

DROP:悄悄丢弃, 一般我们多用DROP来隐藏我们的身份,以及隐藏我们的链表
REJECT:明示拒绝
ACCEPT:接受 custom_chain:转向一个自定义的链
DNAT
SNAT
MASQUERADE:源地址伪装
REDIRECT:重定向:主要用于实现端口重定向
MARK:打防火墙标记的
RETURN:返回 在自定义链执行完毕后使用返回,来返回原规则链。

例如 只要是来自于172.16.0.0/16网段的都允许访问我本机的172.16.100.1的SSHD服务
分析:首先肯定是在允许表中定义的。因为不需要做NAT地址转换之类的,然后查看我们SSHD服务,在22号端口上,处理机制是接受,对于这个表,需要有一来一回两个规则,如果我们允许也好,拒绝也好,对于访问本机服务,我们最好是定义在INPUT链上,而OUTPUT再予以定义就好。(会话的初始端先定义),所以加规则就是:
定义进来的: iptables -t filter -A INPUT -s 172.16.0.0/16 -d 172.16.100.1 -p tcp --dport 22 -j ACCEPT
定义出去的: iptables -t filter -A OUTPUT -s 172.16.100.1 -d 172.16.0.0/16 -p tcp --dport 22 -j ACCEPT
将默认策略改成DROP:

   iptables -P INPUT DROP
   iptables -P OUTPUT DROP
   iptables -P FORWARD DROP

状态检测:

是一种显式扩展,用于检测会话之间的连接关系的,有了检测我们可以实现会话间功能的扩展

什么是状态检测?对于整个TCP协议来讲,它是一个有连接的协议,三次握手中,第一次握手,我们就叫NEW连接,而从第二次握手以后的,ack都为1,这是正常的数据传输,和tcp的第二次第三次握手,叫做已建立的连接(ESTABLISHED),还有一种状态,比较诡异的,比如:SYN=1 ACK=1 RST=1,对于这种我们无法识别的,我们都称之为INVALID无法识别的。还有第四种,FTP这种古老的拥有的特征,每个端口都是独立的,21号和20号端口都是一去一回,他们之间是有关系的,这种关系我们称之为RELATED。

所以我们的状态一共有四种:

  • NEW
  • ESTABLISHED
  • RELATED
  • INVALID

所以我们对于刚才的问题,可以增加状态检测。比如进来的只允许状态为NEW和ESTABLISHED的进来,出去只允许ESTABLISHED的状态出去,这就可以将比较常见的反弹式木马有很好的控制机制。进来的拒绝出去的允许,进来的只允许ESTABLISHED进来,出去只允许ESTABLISHED出去。默认规则都使用拒绝

iptables -L -n --line-number  :查看之前的规则位于第几行
# 改写INPUT
iptables -R INPUT 2 -s 172.16.0.0/16 -d 172.16.100.1 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -R OUTPUT 1 -m state --state ESTABLISHED -j ACCEPT
# 此时如果想再放行一个80端口如何放行呢?
iptables -A INPUT -d 172.16.100.1 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -R INPUT 1 -d 172.16.100.1 -p udp --dport 53 -j ACCEPT

假如我们允许自己ping别人,但是别人ping自己ping不通如何实现呢?
分析:对于ping这个协议,进来的为8(ping),出去的为0(响应).我们为了达到目的,需要8出去,允许0进来

#在出去的端口上
iptables -A OUTPUT -p icmp --icmp-type 8 -j ACCEPT
#在进来的端口上
iptables -A INPUT -p icmp --icmp-type 0 -j ACCEPT
# 小扩展:对于127.0.0.1比较特殊,我们需要明确定义它
iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
iptables -A OUTPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT

SNAT和DNAT的实现

由于我们现在IP地址十分紧俏,已经分配完了,这就导致我们必须要进行地址转换,来节约我们仅剩的一点IP资源。那么通过iptables如何实现NAT的地址转换呢?

SNAT基于原地址的转换

基于原地址的转换一般用在我们的许多内网用户通过一个外网的口上网的时候,这时我们将我们内网的地址转换为一个外网的IP,我们就可以实现连接其他外网IP的功能。
所以我们在iptables中就要定义到底如何转换. 定义的样式. 比如我们现在要将所有192.168.10.0网段的IP在经过的时候全都转换成172.16.100.1这个假设出来的外网地址:

iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -j SNAT --to-source 172.16.100.1

这样,只要是来自本地网络的试图通过网卡访问网络的,都会被统统转换成172.16.100.1这个IP.
那么,如果172.16.100.1不是固定的怎么办?我们都知道当我们使用联通或者电信上网的时候,一般它都会在每次你开机的时候随机生成一个外网的IP,意思就是外网地址是动态变换的。这时我们就要将外网地址换成 MASQUERADE(动态伪装):它可以实现自动寻找到外网地址,而自动将其改为正确的外网地址。所以,我们就需要这样设置:

iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -j MASQUERADE

这里要注意:地址伪装并不适用于所有的地方。

DNAT目标地址转换

对于目标地址转换,数据流向是从外向内的,外面的是客户端,里面的是服务器端通过目标地址转换,我们可以让外面的ip通过我们对外的外网ip来访问我们服务器不同的服务器,而我们的服务却放在内网服务器的不同的服务器上。
如何做目标地址转换呢?:

iptables -t nat -A PREROUTING -d 192.168.10.18 -p tcp --dport 80 -j DNAT --todestination 172.16.100.2

目标地址转换要做在到达网卡之前进行转换,所以要做在PREROUTING这个位置上

控制规则的存放以及开启

注意:你所定义的所有内容,当你重启的时候都会失效,要想我们能够生效,需要使用一个命令将它保存起来

service iptables save

它会保存在/etc/sysconfig/iptables这个文件中

iptables-save
iptables-save > /etc/sysconfig/iptables
iptables-restore

开机的时候,它会自动加载/etc/sysconfig/iptabels
如果开机不能加载或者没有加载,而你想让一个自己写的配置文件(假设为iptables.2)手动生效的话:

iptables-restore < /etc/sysconfig/iptables.2

则完成了将iptables中定义的规则手动生效

其他

阿里云专有网络, 配置外网可以通过外网机器特定端口访问内网机器, 以及内网机器可以通过外网机器上网的iptables配置, 其中192.168.1.5上绑定了外网IP
https://help.aliyun.com/knowledge_detail/6704687.html?spm=5176.7618386.5.13.5BquC7
https://help.aliyun.com/knowledge_detail/6704727.html?spm=5176.7618386.5.1.oGBxpz

开启这台ECS的ip转发功能

sed -i 's/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/g' /etc/sysctl.conf
#让ip转发生效
sysctl –p
[root@bogon ~]# more /etc/sysconfig/iptables                             
# Generated by iptables-save v1.4.7 on Sat Dec 26 15:58:09 2015
*nat
:PREROUTING ACCEPT [21:1603]
:POSTROUTING ACCEPT [0:0]
:OUTPUT ACCEPT [2:146]
-A PREROUTING -p tcp -m tcp --dport 52004 -j DNAT --to-destination 192.168.1.4 
-A PREROUTING -p tcp -m tcp --dport 52003 -j DNAT --to-destination 192.168.1.3 
-A PREROUTING -p tcp -m tcp --dport 52002 -j DNAT --to-destination 192.168.1.2 
-A PREROUTING -p tcp -m tcp --dport 52001 -j DNAT --to-destination 192.168.1.1 
# 这一条配合下面的规则, 将内网1.1上的1521端口, 映射到了1.5的11521端口上
-A PREROUTING -p tcp -m tcp --dport 11521 -j DNAT --to-destination 192.168.1.1:1521
# 这一条用于让内网机器可以访问外网, 除了net.ipv4.ip_forward要为1以外, 还需要添加vpc路由, 将0.0.0.0/0指向当前虚机
-A POSTROUTING -s 192.168.1.0/24 -j SNAT --to-source 192.168.1.5 
-A POSTROUTING -p tcp -m tcp --dport 52004 -j MASQUERADE 
-A POSTROUTING -p tcp -m tcp --dport 52003 -j MASQUERADE 
-A POSTROUTING -p tcp -m tcp --dport 52002 -j MASQUERADE 
-A POSTROUTING -p tcp -m tcp --dport 52001 -j MASQUERADE 
-A POSTROUTING -p tcp -m tcp --dport 1521 -j MASQUERADE
COMMIT
# Completed on Sat Dec 26 15:58:09 2015

命令历史如下

   33  iptables -t nat -I PREROUTING -p tcp --dport 52001 -j DNAT --to 192.168.1.1
   34  iptables -t nat -I POSTROUTING -p tcp --dport 52001 -j MASQUERADE
   35  iptables -t nat -I PREROUTING -p tcp --dport 52002 -j DNAT --to 192.168.1.2
   36  iptables -t nat -I POSTROUTING -p tcp --dport 52002 -j MASQUERADE
   37  iptables -t nat -I PREROUTING -p tcp --dport 52003 -j DNAT --to 192.168.1.3
   38  iptables -t nat -I POSTROUTING -p tcp --dport 52003 -j MASQUERADE
   39  iptables -t nat -I PREROUTING -p tcp --dport 52004 -j DNAT --to 192.168.1.4
   40  iptables -t nat -I POSTROUTING -p tcp --dport 52004 -j MASQUERADE
   41  iptables -L -t nat
   42  service iptables save

清理iptables规则,还原默认设置

iptables -P INPUT ACCEPT -w
iptables -P OUTPUT ACCEPT -w
iptables -P FORWARD ACCEPT -w
iptables -F -w
iptables -X -w

其中 -P 是设置对chain的策略, -w 是等待,不带参数表示无期限等待; -F是清除所有规则, -X是清除所有用户规则