linux虚拟机上网端口映射的多种方法

               

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 [email protected] -p 1234,就可以ssh登录到[email protected]
在10.60.56.220上测试:
[root@server .ssh]# ssh [email protected] -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.
[email protected]'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的工作图。

           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

猜你喜欢

转载自blog.csdn.net/qq_43668159/article/details/87866468
今日推荐