SSH into Ubuntu VM remotely

27,511

Solution 1

By default VirtualBox works in "NAT" mode, meaning that it builds a "virtual" network and translates network accesses from the VM so that to the outside world, they appear to come from your actual computer.

VirtualBox makes the VM think it's connected to a network, but in reality VirtualBox is providing network services, including a mini DHCP server. Then, VirtualBox does network accesses "on behalf" of the VM, making it appear as just another application running on your PC.

However this means that the outside world doesn't really know about the VM and can't access it directly.

I think the port redirection you did was from your home router to your PC, not to the VM. You may be missing some configuration on the VirtualBox side. Apologies if you already did this, but your question was not clear about this.

Basically you have to change the VM's network configuration and set it to "Bridged". What this does is, it creates a virtual interface on your PC and makes the VM use that to access the outside world; anything coming into or going to that interface gets piped to the VM. Thus, the VM will be visible as just another machine in whichever network your PC is connected to (it even has its own MAC address!).

Then, depending on your network's configuration, the VM may get a DHCP address like any other PC connected to your router, or you may need to configure a static IP address for it. This depends on how your network is set up.

Once the VM has a "real" address, you can, on your router, follow the instructions you found to route port 22 to the VM's IP address. This should work as you expect it to.

If it doesn't:

  • Look at your VM's firewall configuration (iptables -L -n). Is it blocking anything?
  • Set up another computer on your local network, and try to SSH to the VM's IP address. If you can, then you need to double-check the forwarding configuration in your router. If you can't, you need to recheck the VM's SSH and firewalling configuration.

Solution 2

from what I can gather, your problem lies in your server computer network, because once you setup vm network to bridge mode, you are good to go (check with ssh <your.vm.ip.number> on your server command line - not in vm).

your server is behind a router, just like a vm using NAT in your machine.

You need to contact your server network administrator so that he/she can put forward your vm's local ip address to a port/range of ports (make sure your vm have a static ip address).

You can do it yourself if you had the authority on your server gateway to do so.

a bit of visualization:

Internet
     '---ROUTER (SERVER1 and SERVER2 use NAT on this router)
            |   (vm1, vm2, vm3, vm4, vm5 need to be forwarded on this router)
            | 
            '--- SERVER1 (vm1, vm2, vm3 use Bridge)
            |       '---vm1
            |       '---vm2
            |       '---vm3
            '--- SERVER1 (vm4, vm5 use Bridge)
            |       '---vm4
            |       '---vm5
Share:
27,511

Related videos on Youtube

user1354275
Author by

user1354275

Updated on September 18, 2022

Comments

  • user1354275
    user1354275 over 1 year

    I dont have much experience on networking or Linux, but I'm trying to ssh into an Ubuntu virtual machine from outside my home network. The Ubuntu virtual machine (VirtualBox) is running on a Debian desktop. I did some research and found I had to forward port 22 from the router to the virtual machine.

    • I changed the network setting on the VM to bridge.
    • I'm forwarding the port to the VM.
    • I checked (http://www.yougetsignal.com/) to show the port as open.

    But when I try to connect it is still not working. ssh username@ -- connection refused

    Is there anything I have to do inside the virtual machine to allow incoming connections? Or forward any port?

    Is there anything I'm doing wrong, any help would be greatly appreciated!!

    • bitsmax
      bitsmax almost 12 years
      How is the VM networked with host. If it behind a NAT (default on virtual box). It wouldn't work but a bridged connection should work provided you forwarded the ports correctly.
    • cprofitt
      cprofitt almost 12 years
      You are using ssh username@router-ip-address right?
    • user1354275
      user1354275 almost 12 years
      yes username@public-ip-address is that right?
    • david6
      david6 almost 12 years
      Ubuntu out-of-the-box includes OpenSSH client, which allows you to connect to another computer running SSH server. To connect 'to' Ubuntu, you need to load OpenSSH server (sudo apt-get install openssh-server) on the target machine.