iptables nat 測試

網絡環境:

node01:  eth0: 10.11.1.197/22
node02:  ​eth0: 10.11.1.198/22,  eth1: 192.168.1.198/24
node03:  eth0:  192.168.1.199/24

SNAT 測試

node02始終作爲NAT的節點,因爲它可以同時訪問到node01和 node03

模擬場景, 從node01 ping node03 的ip (192.168.1.199)經過node02的時候source ip通過SNAT轉換爲node02的ip(192.168.1.199)
當前網絡環境 node01 不能ping通 192.168.1.198 和 192.168.1.199 因爲node01還沒建立192.168.1.x的路由。

1.  在node02建立相應的SNAT規則(SNAT規則一般建在POSTROUTING上):
$ iptables -t nat -A POSTROUTING -s 10.11.1.197 -j DNAT --to-destination 192.168.1.198

2. 在node01上建立相應的路由規則:
$ route add -net 192.168.1.0/24 gw 10.11.1.198

3. 從node01上ping 192.168.1.199
$ ping 192.168.1.199
PING 192.168.1.199 (192.168.1.199) 56(84) bytes of data.
64 bytes from 192.168.1.199: icmp_seq=1 ttl=64 time=2.83 ms
64 bytes from 192.168.1.199: icmp_seq=2 ttl=64 time=1.10 ms

4.  從node03上抓icmp的包, node03查看的是來自192.168.1.198(node02)的包:
$ tcpdump -nei eth0 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
01:47:46.394650 00:50:56:b0:74:08 > 00:50:56:b0:52:d7, ethertype IPv4 (0x0800), length 98: 192.168.1.198 > 192.168.1.199: ICMP echo request, id 32146, seq 5, length 64
01:47:46.394716 00:50:56:b0:52:d7 > 00:50:56:b0:74:08, ethertype IPv4 (0x0800), length 98: 192.168.1.199 > 192.168.1.198: ICMP echo reply, id 32146, seq 5, length 64

DNAT 測試

爲了更好的測試,在測試之前最好清空SNAT測試的規則,路由等。
模擬場景: 在node02 eth0上增加一個secondary ip, node01訪問這個ip的時候,其實真正訪問的是node03

1.  往node02上增加一個secondary ip
$ ip addr add 10.11.1.199/22 dev eth0

2. 建立DNAT規則, (DNAT規則一般建立在PREROUTING上), 將訪問10.11.1.199的包轉換爲訪問192.168.1.199的包。
$ iptables -t nat -A PREROUTING -d 10.11.1.199 -j DNAT --to-destination 192.168.1.199

3. 從node01上ping 10.11.1.199
$ ping 10.11.1.199
PING 10.11.1.199 (10.11.1.199) 56(84) bytes of data.
64 bytes from 10.11.1.199: icmp_seq=1 ttl=64 time=1.42 ms
64 bytes from 10.11.1.199: icmp_seq=2 ttl=64 time=0.905 ms

4. 從node03上抓icmp包, node03上同樣抓的是來自192.168.1.198(node02)的包。
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
01:54:24.446025 00:50:56:b0:74:08 > 00:50:56:b0:52:d7, ethertype IPv4 (0x0800), length 98: 192.168.1.198 > 192.168.1.199: ICMP echo request, id 32156, seq 1, length 64
01:54:24.446266 00:50:56:b0:52:d7 > 00:50:56:b0:74:08, ethertype IPv4 (0x0800), length 98: 192.168.1.199 > 192.168.1.198: ICMP echo reply, id 32156, seq 1, length 64

5. 爲了讓node01能ssh到node03, 在node02上打開FORWARD chain的ssh端口:
$ iptables -A FORWARD -p tcp --dport 22 -j ACCEPT

6. 從node01上ssh 10.11.1.199, 雖然10.11.1.199綁定在node02上, 真正訪問的節點其實是 node03
$ ssh [email protected]
[email protected]'s password:
Last login: Thu Sep 10 01:37:10 2015 from 192.168.1.198
[root@node03~]#
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章