How to SSH to a VirtualBox guest externally throug

2019-01-12 12:52发布

I have a Ubuntu VM running on my Windows 7 machine. How do I set it up so that I can access the webserver externally through SSH?

I found steps (Setup SSH access between VirtualBox Host and Guest VMs) to be able to ssh to my guest from my host, but that still leaves me with the problem of accessing it through my router.

I suppose that I could install an SSH server on my Windows machine and then tunnel a few times (though I'm not 100% sure what to use in terms of local, dynamic, etc. or how to set up multiple tunnels?), but is there a way to make the VM directly accessible to my router so I could directly port forward to it?

15条回答
The star\"
2楼-- · 2019-01-12 13:43

In order to ssh to a Ubuntu VM running in VirtualBox from your host machine, you need to set up two network adapters for the VM.

First of all, stop the VM if not yet.

Then select the VM and click the Settings menu in the VirtualBox toolbar:

enter image description here

Set up Adapter 1

enter image description here

Set up Adapter 2

enter image description here

(Note: you don't need to set up any port forwarding.)

That's it. Once set up, you can start your VM. In your VM, the network configuration will look like below and you'll have Internet access too:

enter image description here

Also in your host machine, you can ssh to your VM:

enter image description here

Be sure that the SSH server has been installed and up running in the VM.

$ ps aux | grep sshd
root 864 0.1 0.5 65512 5392 ? Ss 22:10 0:00 /usr/sbin/sshd -D

If not, install it:

$ sudo apt-get install openssh-server

Also for your information:

  • My VirtualBox version: 5.2.6 r120293 (Qt5.6.2), 2018
  • My Ubuntu version: Ubuntu 16.04.3 LTS
  • My host machine: Windows 10
查看更多
ゆ 、 Hurt°
3楼-- · 2019-01-12 13:49

Ubuntu 18.04 LTS

Configuration with bridged to see the server ip, and connect without "port forwarding"

VirtualBox > right click in server > settings > Network > enable adapter 2 > select "bridged" > Promiscuous mode: allow all > Check the cable connected > start server

On ubuntu server, edit sudo nano /etc/netplan/*init.yaml file,

My sample file:

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

Commands that will help you

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

Configure Static IP Addresses On Ubuntu 18.04 LTS Server - with NetPlan

查看更多
姐就是有狂的资本
4楼-- · 2019-01-12 13:50

Simply setting the Network Setting to bridged did the trick for me.

Your IP will change when you do this. However, in my case it didn't change immediately. ifconfig returned the same ip. I rebooted the vm and boom, the ip set itself to one start with 192.* and I was immediately allowed ssh access.

查看更多
登录 后发表回答