我有一个Ubuntu虚拟机运行在我的Windows 7机器上。我如何设置它,以便我可以通过SSH从外部访问web服务器?

我发现步骤(在VirtualBox主机和客户虚拟机之间设置SSH访问)能够从我的主机SSH到我的客户,但这仍然留给我通过路由器访问它的问题。

我想我可以在我的Windows机器上安装一个SSH服务器,然后隧道几次(虽然我不是100%确定在本地,动态等方面使用什么,或者如何设置多个隧道?),但是有没有一种方法可以让虚拟机直接访问我的路由器,这样我就可以直接端口转发到它?


当前回答

您还可以在网络设置中使用桥接网络(或“桥接适配器”,在较新的版本中)。这将使您的VM与您的机器处于一个VLAN中。你可以像这样ssh进入VM。

ssh user@IP_OF_VM

其他回答

使用NAT网卡和添加端口转发。注明实际主机ip。不要使用127.0.0.1或localhost。

优麒麟 18.04 LTS

配置桥接以查看服务器ip,并且连接时没有“端口转发”

VirtualBox >右键单击服务器>设置> Network > enable适配器2 > select " bridging " >混杂模式:允许所有>检查>启动服务器连接的线缆

在ubuntu服务器上编辑sudo nano /etc/netplan/*init.yaml文件,

我的示例文件:

network:
    ethernets:
        enp0s3:
            addresses: []
            dhcp4: true
        enp0s8:
            addresses: [192.168.0.200/24]
            dhcp4: no
            dhcp6: no
            nameservers:
               addresses: [8.8.8.8, 8.8.4.4]
    version: 2

这些命令将帮助你

nano /etc/netplan/file.yaml     # file to specify the rules of network
reboot now                      # restart ubuntu server right now
netplan apply                   # do after edited *.yaml, to apply changes
ifconfig -a                     # show interfaces with ip, netmask, broadcast, etc...
ping google.com                 # to see if there is internet

在Ubuntu 18.04 LTS服务器上配置静态IP地址-使用NetPlan

将VirtualBox中的适配器类型更改为桥接,并将客户机设置为使用DHCP或在DHCP范围之外设置静态IP地址。这将使虚拟机像家庭网络上的普通客户一样工作。然后,您可以移植向前。

VirtualBox文档中有一个关于如何配置NAT端口转发的很好的解释: http://www.virtualbox.org/manual/ch06.html#natforward

您也可以启动一个端口转发到您的主机,或任何其他服务器,从您的客户。这是特别有用的,如果你的客户是“锁定”或不能以其他方式完成ModifyVM选项(例如没有权限VBoxManage)。

三个次要要求是1)您是/可以登录到VirtualBox Guest(通过“控制台”GUI,另一个Guest等),2)您在VirtualBox主机(或其他服务器)上有一个帐户,3)SSH和TCP转发不被阻止。

假设你能满足这3个要求,以下是步骤:

On the Guest, run netstat -rn and find the Gateway address to the default route destination 0.0.0.0. Let's say it's "10.0.2.2". This 'Gateway' address is (one of) the VirtualBox Host virtual IP(s). On the Guest, run ssh -R 2222:localhost:22 10.0.2.2 where "10.0.2.2" is the VirtualBox server's IP address -OR- any other server IP you wish to port forward to. On the Host, run ssh 10.0.2.2 -p2222 where 10.0.2.2 is the default gateway/VBHost virtual IP found in step 1. If it is NOT the VirtualBox host you are port forwarding to, then the command is ssh localhost -p2222