iptables详细说明
一:前言
防火墻,其實說白了講,就是用于實現(xiàn)Linux下訪問控制的功能的,它分為硬件的或者軟件的防火墻兩種。無論是在哪個網(wǎng)絡(luò)中,防火墻工作的地方一定是在網(wǎng)絡(luò)的邊緣。而我們的任務(wù)就是需要去定義到底防火墻如何工作,這就是防火墻的策略,規(guī)則,以達到讓它對出入網(wǎng)絡(luò)的IP、數(shù)據(jù)進行檢測。
目前市面上比較常見的有3、4層的防火墻,叫網(wǎng)絡(luò)層的防火墻,還有7層的防火墻,其實是代理層的網(wǎng)關(guān)。
對于TCP/IP的七層模型來講,我們知道第三層是網(wǎng)絡(luò)層,三層的防火墻會在這層對源地址和目標地址進行檢測。但是對于七層的防火墻,不管你源端口或者目標端口,源地址或者目標地址是什么,都將對你所有的東西進行檢查。所以,對于設(shè)計原理來講,七層防火墻更加安全,但是這卻帶來了效率更低。所以市面上通常的防火墻方案,都是兩者結(jié)合的。而又由于我們都需要從防火墻所控制的這個口來訪問,所以防火墻的工作效率就成了用戶能夠訪問數(shù)據(jù)多少的一個最重要的控制,配置的不好甚至有可能成為流量的瓶頸。
?
二:iptables 的歷史以及工作原理
1.iptables的發(fā)展:
iptables的前身叫ipfirewall (內(nèi)核1.x時代),這是一個作者從freeBSD上移植過來的,能夠工作在內(nèi)核當(dāng)中的,對數(shù)據(jù)包進行檢測的一款簡易訪問控制工具。但是ipfirewall工作功能極其有限(它需要將所有的規(guī)則都放進內(nèi)核當(dāng)中,這樣規(guī)則才能夠運行起來,而放進內(nèi)核,這個做法一般是極其困難的)。當(dāng)內(nèi)核發(fā)展到2.x系列的時候,軟件更名為ipchains,它可以定義多條規(guī)則,將他們串起來,共同發(fā)揮作用,而現(xiàn)在,它叫做iptables,可以將規(guī)則組成一個列表,實現(xiàn)絕對詳細的訪問控制功能。
他們都是工作在用戶空間中,定義規(guī)則的工具,本身并不算是防火墻。它們定義的規(guī)則,可以讓在內(nèi)核空間當(dāng)中的netfilter來讀取,并且實現(xiàn)讓防火墻工作。而放入內(nèi)核的地方必須要是特定的位置,必須是tcp/ip的協(xié)議棧經(jīng)過的地方。而這個tcp/ip協(xié)議棧必須經(jīng)過的地方,可以實現(xiàn)讀取規(guī)則的地方就叫做 netfilter.(網(wǎng)絡(luò)過濾器)
作者一共在內(nèi)核空間中選擇了5個位置,
1) 內(nèi)核空間中:從一個網(wǎng)絡(luò)接口進來,到另一個網(wǎng)絡(luò)接口去的
2) 數(shù)據(jù)包從內(nèi)核流入用戶空間的
3) 數(shù)據(jù)包從用戶空間流出的
4) 進入/離開本機的外網(wǎng)接口
5) 進入/離開本機的內(nèi)網(wǎng)接口
2.iptables的工作機制
從上面的發(fā)展我們知道了作者選擇了5個位置,來作為控制的地方,但是你有沒有發(fā)現(xiàn),其實前三個位置已經(jīng)基本上能將路徑徹底封鎖了,但是為什么已經(jīng)在進出的口設(shè)置了關(guān)卡之后還要在內(nèi)部卡呢? 由于數(shù)據(jù)包尚未進行路由決策,還不知道數(shù)據(jù)要走向哪里,所以在進出口是沒辦法實現(xiàn)數(shù)據(jù)過濾的。所以要在內(nèi)核空間里設(shè)置轉(zhuǎn)發(fā)的關(guān)卡,進入用戶空間的關(guān)卡,從用戶空間出去的關(guān)卡。那么,既然他們沒什么用,那我們?yōu)槭裁催€要放置他們呢?因為我們在做NAT和DNAT的時候,目標地址轉(zhuǎn)換必須在路由之前轉(zhuǎn)換。所以我們必須在外網(wǎng)而后內(nèi)網(wǎng)的接口處進行設(shè)置關(guān)卡。?????? ?
這五個位置也被稱為五個鉤子函數(shù)(hook functions),也叫五個規(guī)則鏈。
1) PREROUTING (路由前)
2) INPUT (數(shù)據(jù)包流入口)
3) FORWARD (轉(zhuǎn)發(fā)管卡)
4) OUTPUT(數(shù)據(jù)包出口)
5) POSTROUTING(路由后)
這是NetFilter規(guī)定的五個規(guī)則鏈,任何一個數(shù)據(jù)包,只要經(jīng)過本機,必將經(jīng)過這五個鏈中的其中一個鏈。????? ?
3.防火墻的策略
防火墻策略一般分為兩種,一種叫“通”策略,一種叫“堵”策略,通策略,默認門是關(guān)著的,必須要定義誰能進。堵策略則是,大門是洞開的,但是你必須有身份認證,否則不能進。所以我們要定義,讓進來的進來,讓出去的出去,所以通,是要全通,而堵,則是要選擇。當(dāng)我們定義的策略的時候,要分別定義多條功能,其中:定義數(shù)據(jù)包中允許或者不允許的策略,filter過濾的功能,而定義地址轉(zhuǎn)換的功能的則是nat選項。為了讓這些功能交替工作,我們制定出了“表”這個定義,來定義、區(qū)分各種不同的工作功能和處理方式。
我們現(xiàn)在用的比較多個功能有3個:
1) filter 定義允許或者不允許的
2) nat 定義地址轉(zhuǎn)換的
3) mangle功能:修改報文原數(shù)據(jù)
我們修改報文原數(shù)據(jù)就是來修改TTL的。能夠?qū)崿F(xiàn)將數(shù)據(jù)包的元數(shù)據(jù)拆開,在里面做標記/修改內(nèi)容的。而防火墻標記,其實就是靠mangle來實現(xiàn)的。
小擴展:
對于filter來講一般只能做在3個鏈上:INPUT ,FORWARD ,OUTPUT
對于nat來講一般也只能做在3個鏈上:PREROUTING ,OUTPUT ,POSTROUTING
而mangle則是5個鏈都可以做:PREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING
iptables/netfilter(這款軟件)是工作在用戶空間的,它可以讓規(guī)則進行生效的,本身不是一種服務(wù),而且規(guī)則是立即生效的。
而iptables現(xiàn)在被做成了一個服務(wù),可以進行啟動,停止的。啟動,則將規(guī)則直接生效,停止,則將規(guī)則撤銷。
iptables還支持自己定義鏈。但是自己定義的鏈,必須是跟某種特定的鏈關(guān)聯(lián)起來的。在一個關(guān)卡設(shè)定,指定當(dāng)有數(shù)據(jù)的時候?qū)iT去找某個特定的鏈來處理,當(dāng)那個鏈處理完之后,再返回。接著在特定的鏈中繼續(xù)檢查。
注意:規(guī)則的次序非常關(guān)鍵,誰的規(guī)則越嚴格,應(yīng)該放的越靠前,而檢查規(guī)則的時候,是按照從上往下的方式進行檢查的。
?
三.規(guī)則的寫法:
iptables定義規(guī)則的方式比較復(fù)雜:
-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:定義如何對規(guī)則進行管理
-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:指定你接下來的規(guī)則到底是在哪個鏈上操作的,當(dāng)定義策略的時候,是可以省略的
參數(shù)
-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 :設(shè)置默認策略的(設(shè)定默認門是關(guān)著的還是開著的)
? 默認策略一般只有兩種
? iptables -P INPUT (DROP|ACCEPT)? 默認是關(guān)的/默認是開的
? 比如:
? iptables -P INPUT DROP 這就把默認規(guī)則給拒絕了。并且沒有定義哪個動作,所以關(guān)于外界連接的所有規(guī)則包括Xshell連接之類的,遠程連接都被拒絕了。
-F: FLASH,清空規(guī)則鏈的(注意每個鏈的管理權(quán)限)
? 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:清空鏈,及鏈中默認規(guī)則的計數(shù)器的(有兩個計數(shù)器,被匹配到多少個數(shù)據(jù)包,多少個字節(jié))
??? iptables -Z :清空
?
2.規(guī)則管理命令
-A:追加,在當(dāng)前鏈的最后新增一個規(guī)則
-I num : 插入,把當(dāng)前規(guī)則插入為第幾條。
? -I 3 :插入為第三條
-R num:Replays替換/修改第幾條規(guī)則
? 格式:iptables -R 3 …………
-D num:刪除,明確指定刪除第幾條規(guī)則
3.查看管理命令 “-L”
附加子命令
-n:以數(shù)字的方式顯示ip,它會將ip直接顯示出來,如果不加-n,則會將ip反向解析成主機名。
-v:顯示詳細信息
-vv
-vvv :越多越詳細
-x:在計數(shù)器上顯示精確值,不做單位換算
--line-numbers : 顯示規(guī)則的行號
-t nat:顯示所有的關(guān)卡的信息
?
五:詳解匹配標準
1.通用匹配:源地址目標地址的匹配
?? ?-s:指定作為源地址匹配,這里不能指定主機名稱,必須是IP
?? ??? ?IP | IP/MASK | 0.0.0.0/0.0.0.0
?? ??? ?而且地址可以取反,加一個“!”表示除了哪個IP之外
?? ?-d:表示匹配目標地址
?? ?-p:用于匹配協(xié)議的(這里的協(xié)議通常有3種,TCP/UDP/ICMP)
?? ?-i eth0:從這塊網(wǎng)卡流入的數(shù)據(jù)
?? ??? ?流入一般用在INPUT和PREROUTING上
?? ?-o eth0:從這塊網(wǎng)卡流出的數(shù)據(jù)
?? ??? ?流出一般在OUTPUT和POSTROUTING上
?????? ?
2.擴展匹配
2.1隱含擴展:對協(xié)議的擴展
??? -p tcp :TCP協(xié)議的擴展。一般有三種擴展
?? ?--dport XX-XX:指定目標端口,不能指定多個非連續(xù)端口,只能指定單個端口,比如
?? ?--dport 21? 或者 --dport 21-23 (此時表示21,22,23)
?? ?--sport:指定源端口
?? ?--tcp-fiags:TCP的標志位(SYN,ACK,FIN,PSH,RST,URG)
?? ???? 對于它,一般要跟兩個參數(shù):
?? ??? ?1.檢查的標志位
?? ??? ?2.必須為1的標志位
?? ??? ?--tcpflags syn,ack,fin,rst syn?? =??? --syn
?? ??? ?表示檢查這4個位,這4個位中syn必須為1,其他的必須為0。所以這個意思就是用于檢測三次握手的第一次包的。對于這種專門匹配第一包的SYN為1的包,還有一種簡寫方式,叫做--syn
??? -p udp:UDP協(xié)議的擴展
??????? --dport
??????? --sport
??? -p icmp:icmp數(shù)據(jù)報文的擴展
??????? --icmp-type:
?? ??? ?echo-request(請求回顯),一般用8 來表示
?? ??? ?所以 --icmp-type 8 匹配請求回顯數(shù)據(jù)包
?? ??? ?echo-reply (響應(yīng)的數(shù)據(jù)包)一般用0來表示
???????????????? ?
2.2顯式擴展(-m)
?? 擴展各種模塊
??? -m multiport:表示啟用多端口擴展
??? 之后我們就可以啟用比如 --dports 21,23,80
六:詳解-j ACTION
常用的ACTION:
DROP:悄悄丟棄
? 一般我們多用DROP來隱藏我們的身份,以及隱藏我們的鏈表
REJECT:明示拒絕
ACCEPT:接受
? custom_chain:轉(zhuǎn)向一個自定義的鏈
DNAT
SNAT
MASQUERADE:源地址偽裝
REDIRECT:重定向:主要用于實現(xiàn)端口重定向
MARK:打防火墻標記的
RETURN:返回
? 在自定義鏈執(zhí)行完畢后使用返回,來返回原規(guī)則鏈。
練習(xí)題:
???? 只要是來自于172.16.0.0/16網(wǎng)段的都允許訪問我本機的172.16.100.1的SSHD服務(wù)
???? 分析:首先肯定是在允許表中定義的。因為不需要做NAT地址轉(zhuǎn)換之類的,然后查看我們SSHD服務(wù),在22號端口上,處理機制是接受,對于這個表,需要有一來一回兩個規(guī)則,如果我們允許也好,拒絕也好,對于訪問本機服務(wù),我們最好是定義在INPUT鏈上,而OUTPUT再予以定義就好。(會話的初始端先定義),所以加規(guī)則就是:
定義進來的: 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
七:狀態(tài)檢測:
是一種顯式擴展,用于檢測會話之間的連接關(guān)系的,有了檢測我們可以實現(xiàn)會話間功能的擴展
什么是狀態(tài)檢測?對于整個TCP協(xié)議來講,它是一個有連接的協(xié)議,三次握手中,第一次握手,我們就叫NEW連接,而從第二次握手以后的,ack都為1,這是正常的數(shù)據(jù)傳輸,和tcp的第二次第三次握手,叫做已建立的連接(ESTABLISHED),還有一種狀態(tài),比較詭異的,比如:SYN=1 ACK=1 RST=1,對于這種我們無法識別的,我們都稱之為INVALID無法識別的。還有第四種,FTP這種古老的擁有的特征,每個端口都是獨立的,21號和20號端口都是一去一回,他們之間是有關(guān)系的,這種關(guān)系我們稱之為RELATED。
所以我們的狀態(tài)一共有四種:
NEW
ESTABLISHED
RELATED
INVALID
所以我們對于剛才的練習(xí)題,可以增加狀態(tài)檢測。比如進來的只允許狀態(tài)為NEW和ESTABLISHED的進來,出去只允許ESTABLISHED的狀態(tài)出去,這就可以將比較常見的反彈式木馬有很好的控制機制。
對于練習(xí)題的擴展:
進來的拒絕出去的允許,進來的只允許ESTABLISHED進來,出去只允許ESTABLISHED出去。默認規(guī)則都使用拒絕
iptables -L -n --line-number? :查看之前的規(guī)則位于第幾行
改寫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
練習(xí)題:
假如我們允許自己ping別人,但是別人ping自己ping不通如何實現(xiàn)呢?
分析:對于ping這個協(xié)議,進來的為8(ping),出去的為0(響應(yīng)).我們?yōu)榱诉_到目的,需要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的實現(xiàn)
由于我們現(xiàn)在IP地址十分緊俏,已經(jīng)分配完了,這就導(dǎo)致我們必須要進行地址轉(zhuǎn)換,來節(jié)約我們僅剩的一點IP資源。那么通過iptables如何實現(xiàn)NAT的地址轉(zhuǎn)換呢?
1.SNAT基于原地址的轉(zhuǎn)換
?? ?基于原地址的轉(zhuǎn)換一般用在我們的許多內(nèi)網(wǎng)用戶通過一個外網(wǎng)的口上網(wǎng)的時候,這時我們將我們內(nèi)網(wǎng)的地址轉(zhuǎn)換為一個外網(wǎng)的IP,我們就可以實現(xiàn)連接其他外網(wǎng)IP的功能。
所以我們在iptables中就要定義到底如何轉(zhuǎn)換:
定義的樣式:
?? ?比如我們現(xiàn)在要將所有192.168.10.0網(wǎng)段的IP在經(jīng)過的時候全都轉(zhuǎn)換成172.16.100.1這個假設(shè)出來的外網(wǎng)地址:
?? ?iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -j SNAT --to-source 172.16.100.1
?? ?這樣,只要是來自本地網(wǎng)絡(luò)的試圖通過網(wǎng)卡訪問網(wǎng)絡(luò)的,都會被統(tǒng)統(tǒng)轉(zhuǎn)換成172.16.100.1這個IP.
?? ?那么,如果172.16.100.1不是固定的怎么辦?
?? ?我們都知道當(dāng)我們使用聯(lián)通或者電信上網(wǎng)的時候,一般它都會在每次你開機的時候隨機生成一個外網(wǎng)的IP,意思就是外網(wǎng)地址是動態(tài)變換的。這時我們就要將外網(wǎng)地址換成 MASQUERADE(動態(tài)偽裝):它可以實現(xiàn)自動尋找到外網(wǎng)地址,而自動將其改為正確的外網(wǎng)地址。所以,我們就需要這樣設(shè)置:
?iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -j MASQUERADE
?這里要注意:地址偽裝并不適用于所有的地方。
?
2.DNAT目標地址轉(zhuǎn)換
?? ?對于目標地址轉(zhuǎn)換,數(shù)據(jù)流向是從外向內(nèi)的,外面的是客戶端,里面的是服務(wù)器端通過目標地址轉(zhuǎn)換,我們可以讓外面的ip通過我們對外的外網(wǎng)ip來訪問我們服務(wù)器不同的服務(wù)器,而我們的服務(wù)卻放在內(nèi)網(wǎng)服務(wù)器的不同的服務(wù)器上。
如何做目標地址轉(zhuǎn)換呢?:
iptables -t nat -A PREROUTING -d 192.168.10.18 -p tcp --dport 80 -j DNAT --todestination 172.16.100.2
??? 目標地址轉(zhuǎn)換要做在到達網(wǎng)卡之前進行轉(zhuǎn)換,所以要做在PREROUTING這個位置上
?
九:控制規(guī)則的存放以及開啟
注意:你所定義的所有內(nèi)容,當(dāng)你重啟的時候都會失效,要想我們能夠生效,需要使用一個命令將它保存起來
1.service iptables save 命令
? 它會保存在/etc/sysconfig/iptables這個文件中
2.iptables-save 命令
? iptables-save > /etc/sysconfig/iptables
3.iptables-restore 命令
? 開機的時候,它會自動加載/etc/sysconfig/iptabels
? 如果開機不能加載或者沒有加載,而你想讓一個自己寫的配置文件(假設(shè)為iptables.2)手動生效的話:
? iptables-restore < /etc/sysconfig/iptables.2
? 則完成了將iptables中定義的規(guī)則手動生效
?
?
十:總結(jié)
Iptables是一個非常重要的工具,它是每一個防火墻上幾乎必備的設(shè)置,也是我們在做大型網(wǎng)絡(luò)的時候,為了很多原因而必須要設(shè)置的。學(xué)好Iptables,可以讓我們對整個網(wǎng)絡(luò)的結(jié)構(gòu)有一個比較深刻的了解,同時,我們還能夠?qū)?nèi)核空間中數(shù)據(jù)的走向以及l(fā)inux的安全給掌握的非常透徹。我們在學(xué)習(xí)的時候,盡量能結(jié)合著各種各樣的項目,實驗來完成,這樣對你加深iptables的配置,以及各種技巧有非常大的幫助。
?
阿里云專有網(wǎng)絡(luò), 配置外網(wǎng)可以通過外網(wǎng)機器特定端口訪問內(nèi)網(wǎng)機器, 以及內(nèi)網(wǎng)機器可以通過外網(wǎng)機器上網(wǎng)的iptables配置, 其中192.168.1.5上綁定了外網(wǎng)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轉(zhuǎn)發(fā)功能
sed -i 's/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/g' /etc/sysctl.conf #讓ip轉(zhuǎn)發(fā)生效 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 # 這一條配合下面的規(guī)則, 將內(nèi)網(wǎng)1.1上的1521端口, 映射到了1.5的11521端口上 -A PREROUTING -p tcp -m tcp --dport 11521 -j DNAT --to-destination 192.168.1.1:1521 # 這一條用于讓內(nèi)網(wǎng)機器可以訪問外網(wǎng), 除了net.ipv4.ip_forward要為1以外, 還需要添加vpc路由, 將0.0.0.0/0指向當(dāng)前虛機-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總結(jié)
以上是生活随笔為你收集整理的iptables详细说明的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [sharepoint]根据用户名获取该
- 下一篇: 网络配置之ifconfig及Ip命令详解