iptables详细说明
一:前言
防火墻,其實說白了講,就是用于實現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定義規則的方式比較復雜:
-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 :指定如何進行處理
比如:
?
四:詳解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详细说明的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [sharepoint]根据用户名获取该
- 下一篇: 网络配置之ifconfig及Ip命令详解