linux虛擬機器上網連接埠映射的多種方法

來源:互聯網
上載者:User

host主機系統:centos 5.3,使用升級過的2.6.23核心,安裝VirtualBox 3.0.8。host主機IP是10.60.56.90,訪問外網的網路出口為eth0。虛擬機器為vm1,ip地址192.168.56.101,網路介面為vboxnet0,使用hostonly方式。在第三台電腦10.60.56.220上進行測試連接。
兩個任務:
一:使虛擬機器的192.168.56.0/24網段能訪問到外網,即可以與10.60.56.220通訊。
二:外網能訪問內網的伺服器,即10.60.56.220可以telnet到192.168.56.101或者ssh。

任務一:直接使用iptables進行地址轉換:
[root@jcwkyl ~]# iptables --table nat --append POSTROUTING --out-interface eth0 --jump MASQUERADE

[root@jcwkyl ~]# echo "1" > /proc/sys/net/ipv4/ip_forward
測試一下:
[root@vm1 root]# ping 10.60.56.220

PING 10.60.56.220 (10.60.56.220) 56(84) bytes of data.

64 bytes from 10.60.56.220: icmp_seq=0 ttl=63 time=1.37 ms

64 bytes from 10.60.56.220: icmp_seq=1 ttl=63 time=0.925 ms

任務二:
方法一:使用nc
[root@jcwkyl ~]# nc -l -k -4 1234 < /root/telnetfifo | nc 192.168.56.101 23 > /root/telnetfifo 

-l表示監聽,-k表示一個串連結束後繼續監聽,-4表示使用ipv4。缺點是一次只能處理一個串連。
方法二:使用socat
[root@jcwkyl ~]# socat TCP4-LISTEN:1234,reuseaddr,fork TCP4:192.168.56.101:22
方法三:使用iptables
[root@jcwkyl ~]# iptables --table nat --append PREROUTING --in-interface eth0 --proto tcp --dport 1234 --jump DNAT --to-destination 192.168.56.101:22

在其它電腦上ssh user@10.60.56.90 -p 1234,就可以ssh登入到user@192.168.56.101。
在10.60.56.220上測試:
[root@server .ssh]# ssh root@10.60.56.90 -p 1234

The authenticity of host '10.60.56.90 (10.60.56.90)' can't be established.

RSA key fingerprint is 4b:d4:2d:b9:2a:66:44:c8:f8:a7:f7:c6:5a:61:48:02.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '10.60.56.90' (RSA) to the list of known hosts.

root@10.60.56.90's password: 

Last login: Tue Dec  8 14:17:53 2009 from 192.168.56.1

[root@vm1 root]#
看到登入後shell變成了root@vm1。

參考:

http://sectools.org/index.html

這個網頁上列出了100個網路安全工具。

http://www.dest-unreach.org/socat/doc/socat.html#EXAMPLES

這是socat官方文檔中給出的使用socat的例子。照著例子基本可以依葫蘆畫瓢。

http://linux.chinaunix.net/bbs/viewthread.php?tid=812400

這是chinaunix上的一篇文章,其中講到了iptables nat的配置。

http://man.chinaunix.net/network/iptables-tutorial-cn-1.1.19.html

iptables-1.1.19指南,譯者sllscn。

另外可以在google圖片搜尋中以iptables為關鍵字搜尋一下,可以看到iptables的工作圖。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.