一:前言

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

目前市面上比较常见的有3、4层的防火墙,叫网络层的防火墙,还有7层的防火墙,其实是代理层的网关。

对于TCP/IP的七层模型来讲,我们知道第三层是网络层,三层的防火墙会在这层对源地址和目标地址进行检测。但是对于七层的防火墙,不管你源端口或者目标端口,源地址或者目标地址是什么,都将对你所有的东西进行检查。所以,对于设计原理来讲,七层防火墙更加安全,但是这却带来了效率更低。所以市面上通常的防火墙方案,都是两者结合的。而又由于我们都需要从防火墙所控制的这个口来访问,所以防火墙的工作效率就成了用户能够访问数据多少的一个最重要的控制,配置的不好甚至有可能成为流量的瓶颈。
 
二:iptables 的历史以及工作原理

1.iptables的发展:

iptables的前身叫ipfirewall (内核1.x时代),这是一个作者从freeBSD上移植过来的,能够工作在内核当中的,对数据包进行检测的一款简易访问控制工具。但是ipfirewall工作功能极其有限(它需要将所有的规则都放进内核当中,这样规则才能够运行起来,而放进内核,这个做法一般是极其困难的)。当内核发展到2.x系列的时候,软件更名为ipchains,它可以定义多条规则,将他们串起来,共同发挥作用,而现在,它叫做iptables,可以将规则组成一个列表,实现绝对详细的访问控制功能。

他们都是工作在用户空间中,定义规则的工具,本身并不算是防火墙。它们定义的规则,可以让在内核空间当中的netfilter来读取,并且实现让防火墙工作。而放入内核的地方必须要是特定的位置,必须是tcp/ip的协议栈经过的地方。而这个tcp/ip协议栈必须经过的地方,可以实现读取规则的地方就叫做 netfilter.(网络过滤器)

作者一共在内核空间中选择了5个位置,
1) 内核空间中:从一个网络接口进来,到另一个网络接口去的
2) 数据包从内核流入用户空间的
3) 数据包从用户空间流出的
4) 进入/离开本机的外网接口
5) 进入/离开本机的内网接口

2.iptables的工作机制

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

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

3.防火墙的策略

防火墙策略一般分为两种,一种叫“通”策略,一种叫“堵”策略,通策略,默认门是关着的,必须要定义谁能进。堵策略则是,大门是洞开的,但是你必须有身份认证,否则不能进。所以我们要定义,让进来的进来,让出去的出去,所以通,是要全通,而堵,则是要选择。当我们定义的策略的时候,要分别定义多条功能,其中:定义数据包中允许或者不允许的策略,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

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-specificationAppend 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-specificationCheck 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 rulenumDelete 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-specificationInsert 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-specificationReplace 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 byiptables -t nat -n -LPlease 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 useiptables -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 chainCreate 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 targetSet 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-chainRename the user specified chain to the user supplied name. This is cosmetic, and has no effect on the structure of the table.
-hHelp. Give a (currently very brief) description of the command syntax. 

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

参数

-4, --ipv4This option has no effect in iptables and iptables-restore.
-6, --ipv6If 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 protocolThe 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 matchSpecifies 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 targetThis 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 chainThis 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 nameName 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 nameName 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, --fragmentThis 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 bytesThis 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:

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

-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 :清空
 
2.规则管理命令

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

3.查看管理命令 “-L”

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

1.通用匹配:源地址目标地址的匹配
    -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上
        
2.扩展匹配

2.1隐含扩展:对协议的扩展
    -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来表示
                  
2.2显式扩展(-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的地址转换呢?

1.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
 这里要注意:地址伪装并不适用于所有的地方。
 
2.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这个位置上
 
九:控制规则的存放以及开启

注意:你所定义的所有内容,当你重启的时候都会失效,要想我们能够生效,需要使用一个命令将它保存起来
1.service iptables save 命令
  它会保存在/etc/sysconfig/iptables这个文件中

2.iptables-save 命令
  iptables-save > /etc/sysconfig/iptables

3.iptables-restore 命令
  开机的时候,它会自动加载/etc/sysconfig/iptabels
  如果开机不能加载或者没有加载,而你想让一个自己写的配置文件(假设为iptables.2)手动生效的话:
  iptables-restore < /etc/sysconfig/iptables.2
  则完成了将iptables中定义的规则手动生效
 
 
十:总结

Iptables是一个非常重要的工具,它是每一个防火墙上几乎必备的设置,也是我们在做大型网络的时候,为了很多原因而必须要设置的。学好Iptables,可以让我们对整个网络的结构有一个比较深刻的了解,同时,我们还能够将内核空间中数据的走向以及linux的安全给掌握的非常透彻。我们在学习的时候,尽量能结合着各种各样的项目,实验来完成,这样对你加深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.134  iptables -t nat -I POSTROUTING -p tcp --dport 52001 -j MASQUERADE35  iptables -t nat -I PREROUTING -p tcp --dport 52002 -j DNAT --to 192.168.1.236  iptables -t nat -I POSTROUTING -p tcp --dport 52002 -j MASQUERADE37  iptables -t nat -I PREROUTING -p tcp --dport 52003 -j DNAT --to 192.168.1.338  iptables -t nat -I POSTROUTING -p tcp --dport 52003 -j MASQUERADE39  iptables -t nat -I PREROUTING -p tcp --dport 52004 -j DNAT --to 192.168.1.440  iptables -t nat -I POSTROUTING -p tcp --dport 52004 -j MASQUERADE41  iptables -L -t nat42  service iptables save

iptables详细说明相关推荐

  1. linux防火墙规则配置教程,linux防火墙iptables详细教程

    2.1 框架图 -->PREROUTING-->[ROUTE]-->FORWARD-->POSTROUTING--> mangle     |     mangle    ...

  2. [转载]iptables 详细教程

    产品测试需要对各个linux节点之间的网络开关进行控制,由于物理插拔实在麻烦, iptables这个linux自带防火墙自然就成为了解决问题的首选, 看了鸟哥的书熟悉了大致规则也就基本能够上手了.下文 ...

  3. Linux iptables 防火墙常用规则

    2019独角兽企业重金招聘Python工程师标准>>> 米扑博客 总结了 Linux iptables 防火墙常用规则,分享出来. iptables 安装 yum install i ...

  4. 防火墙 iptables

    第1章 防火墙iptables 网络防火墙就是一个位于计算机和它所连接的网络之间的防火墙.该计算机流入流出的所有网络通信均要经过此防火墙.防火墙对流经它的网络通信进行扫描,这样能够过滤掉一些***,以 ...

  5. iptables详解 redhat5

    Iptables原理 现在防火墙主要分以下三种类型: 包过滤.应用代理.状态检测 包过滤防火墙:现在静态包过滤防火墙市面上已经看不到了,取而代之的是动态包过滤技术的防火墙哈~ 代理防火墙:因一些特殊的 ...

  6. IPTABLES学习摘抄

    为什么80%的码农都做不了架构师?>>>    //----------------------------------------------------------------- ...

  7. Iptables命令大全

    1./etc/init.d/iptables stop        #关闭iptables命令 2.iptables -nL                        #查看iptables详细 ...

  8. 带着问题学 Kubernetes 抽象对象 Service 服务间调用

    带着问题学 Kubernetes 抽象对象 Service https://github.com/jasonGeng88/blog/blob/master/201707/k8s-service.md ...

  9. k8s-------(| 二 |)资源对象Namespace,Service

    文章目录 一. Namespace(命名空间) Pod跨namespace访问Service服务 二 . Service kube-proxy和service的关系: 一.Service 代理方式 1 ...

最新文章

  1. ListView 异步更新出现问题的解决(Handler)
  2. 深度:应用安全是信息安全防护的短板
  3. 元宇宙iwemeta: 韩国政府力挺元宇宙, 打造元宇宙城市 出台五年规划
  4. 华为发布《大交通时代》:开启未来数字交通宏图
  5. 系统分析师资料_软考 系统分析师考试通过总结
  6. Controller层返回字符串
  7. 01-Quartz2D
  8. Doris SQL执行计划
  9. ubuntu命令chmod755
  10. 计算机报 论文,计算机学院毕业设计(论文)题目上报.doc
  11. PostgreSQL 如何实现数据透视表
  12. isalpha() / isupper() / islower()函数
  13. java递归分苹果_递归应用示例(放苹果)[较难 选听]
  14. 人脸识别经典开源项目
  15. PHP 7.3 新特性介绍
  16. python 文件缓存
  17. 基于单片机定时闹钟设计
  18. React基础-JSX语法介绍
  19. 如何判断是不是一个网段
  20. AI将带我们去何方?(上-前言篇)

热门文章

  1. excel根据rgb自动填充颜色_Excel一键定位空值与自动填充
  2. iphone导出照片到电脑_如何把 iPhone 中的照片快速传到电脑上?
  3. 计算机软考知识点总结,历年计算机软考《系统分析师》复习知识点总结(8)
  4. 增加数据_太原二手房七月数据出炉,挂牌量增加800余套,万柏林区涨幅大
  5. 使用ansible批量部署开机启动时为字符界面
  6. vue 怎么样不重复往数组里插入数据_Vue.js在数组中插入重复数据的实现代码分享...
  7. java自定义注解为空值_java自定义注解
  8. windows下安装各个版本memcache扩展
  9. input标签内容改变的触发事件
  10. C# IOThread