为VirtualBox虚拟的Ubuntu加上远程SSH功能

来源:互联网 发布:商场销售数据报表 编辑:程序博客网 时间:2024/05/22 10:40

在我使用VirtualBox虚拟Ubuntu的时候,有需要远程SSH到虚拟Ubuntu客户机的需求,但是对VirtualBox来说,其默认是使用NAT(Network Address Translation)做为客户机和主机之间的连接的。这使得客户机可以很容易连接到网络,但是如果想从主机连接到客户机,却会是一件很困难的事情。如果想解决此问题,需要通过如下思路来解决:

  1.通过Virtualbox主机的网络来连接。
  2.用VirtualBox来转发所有接收的数据包到主机的一个特定端口。

下面我们就来说说具体的实现方法:

一、安装SSH-Server:
首先要在Ubuntu里把SSH-Server装上。

dev@karmic-dev:~$ sudo apt-get install openssh-server

配置openssh:
dev@karmic-dev:~$ sudo vi /etc/ssh/sshd_config
将:
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
的注释去掉,以加快远程SSH进入的速度。
重启SSH:
dev@karmic-dev:~$ sudo /etc/init.d/ssh restart

二、配置VirtualBox监听端口:

然后我们回到Windows下,打开一个DOS端口,进入到VirtualBox的安装目录,敲入如下命令:
VBoxManage.exe setextradata KarmicDev32 "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort" 2222
VBoxManage.exe setextradata KarmicDev32 "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort" 22
VBoxManage.exe setextradata KarmicDev32 "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol" TCP

此时通过以下命令:
VBoxManage.exe getextradata KarmicDev32 enumerate
可以查看到相关配置信息:
C:\Program Files\Sun\VirtualBox>VBoxManage.exe getextradata KarmicDev32 enumerate
VirtualBox Command Line Management Interface Version 3.0.10
(C) 2005-2009 Sun Microsystems, Inc.
All rights reserved.

Key: GUI/LastWindowPostion, Value: 405,35,720,441
Key: GUI/Fullscreen, Value: off
Key: GUI/Seamless, Value: off
Key: GUI/AutoresizeGuest, Value: on
Key: GUI/MiniToolBarAutoHide, Value: on
Key: GUI/SaveMountedAtRuntime, Value: yes
Key: GUI/ShowMiniToolBar, Value: yes
Key: GUI/MiniToolBarAlignment, Value: bottom
Key: VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort, Value: 2222
Key: VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort, Value: 22
Key: VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol, Value: TCP

重新启动Ubuntu,此时通过Putty来连接localhost的2222端口,会发现已经可以连上VirtualBox虚拟机的Ubuntu了。
另外,如果要取消VirtualBox的端口数据转发,可以通过将这个值设置为空来删除。比如我们要取消HostPort,可以输入:
VBoxManage.exe setextradata KarmicDev32 "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort"

参考资料:http://mydebian.blogdns.org/?p=148,基本上完全是参考的这篇文章,但亦非完全是翻译,呵呵。