By Jordan


2011-05-06 03:24:03 8 Comments

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 comments

@DarckBlezzer 2018-10-14 07:22:24

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

@Kashyap Kopparam 2014-05-05 04:33:09

You can also use a Bridged Network (or "Bridge Adapter", in newer versions) in network settings. This will put your VM in a VLAN with your machine. So you can just ssh into the VM like this.

ssh [email protected]_OF_VM

@user2233706 2018-06-11 01:39:13

With this solution does the SSH connection go across the LAN?

@vkostromin 2012-05-10 10:39:54

The best way to login to a guest Linux VirtualBox VM is port forwarding. By default, you should have one interface already which is using NAT. Then go to the Network settings and click the Port Forwarding button. Add a new Rule. As the rule name, insert "ssh". As "Host port", insert 3022. As "Guest port", insert 22. Everything else of the rule can be left blank.

or from the command line

VBoxManage modifyvm myserver --natpf1 "ssh,tcp,,3022,,22"

where 'myserver' is the name of the created VM. Check the added rules:

VBoxManage showvminfo myserver | grep 'Rule'

That's all! Please be sure you don't forget to install an SSH server in the VM:

sudo apt-get install openssh-server

To SSH into the guest VM, write:

ssh -p 3022 [email protected]

Where user is your username within the VM.

@Keyslinger 2012-12-03 13:14:33

What do I use as a host name or IP address?

@vkostromin 2012-12-03 14:39:12

Keyslinger, just left blank, you don't need fill them

@Victor P. 2012-12-11 05:47:34

Note that you need to enable VirtualBox firewall access for both public and private networks.

@D. Woods 2012-12-21 04:50:49

@Keyslinger, is this what you were looking for? ssh -p 3022 [email protected] That was the one step that I was missing and just went to find on another site.

@Kim Stacks 2013-01-03 07:21:38

just want to give props to you guys. THis answer helped me with my own problems with the vm. which i now convert into my own solution at superuser superuser.com/a/527462/8184

@sfarbota 2013-01-04 20:31:37

This helped me get a bit further but still having issues. If anyone could look at my issue here sqa.stackexchange.com/q/5399/4556 it would be very much appreciated!!

@Eduardo Russo 2013-02-28 09:20:06

I also added the port redirect from 2020 to 80, so I can easily access Apache from the browser using 192.0.0.1:2020. VBoxManage modifyvm myserver --natpf1 "ssh,tcp,,2020,,80"

@John Simon 2013-04-07 18:11:26

@AngeloR. first see if your guestVM can ssh into itself. I'm setting up a minimal dev environment, and as it turns out, minimal can mean not even an SSH server.

@amphibient 2013-08-19 19:30:29

I did this, restarted the VM, captured the IP address of the VM using ifconfig, tried ssh <GUEST_IP>, still couldn't connect

@amphibient 2013-08-19 19:51:34

However, when I moved the Network Adapter to the Bridged mode, it worked.

@Ruslans Uralovs 2013-10-10 20:20:44

See VirtualBox networking docs: virtualbox.org/manual/ch06.html#network_nat

@Martin Kapfhammer 2013-10-27 14:26:18

Works great with ssh -p 3022 [email protected], but how can I connect from another machine somewhere in the internet to my vbox via windows? I used the IP-address of windows and tried ssh -p 3022 [email protected], but got a connection timeout. If I use the same command on my host machine, it works.

@shoover 2013-12-12 17:47:55

Thanks! Works great with the Cloudera Quickstart VM, too.

@Darren Smith 2014-01-04 17:38:52

This worked for me, with guest OS SUSE 9.3 However I did have to modify the guest-OS firewall to allow SSH service (or, just disable the guest-OS firewall). These firewall changes are made using whatever firewall config tool the guest-OS provides (eg, YAST).

@GabLeRoux 2014-02-10 03:35:43

Make sure you're not using sudo VBoxManage since it won't be your current user and make sure that the vm is stopped ;)

@Ray Hunter 2014-02-23 00:16:33

I used this along with starting the VM in headless mode: VBoxManage startvm "vmname" --type headless

@vkostromin 2014-02-27 19:19:58

thebigdog, you can headless directly: VBoxHeadless -s 'vmname'

@Ateev Chopra 2014-07-07 12:58:36

How can I add the box's ip into the /etc/hosts file to create a link alias ?

@Sebastian Höffner 2014-07-30 17:36:14

It should be "localhost" anyway. Otherwise add a line with "127.0.0.1 localhost" or similar.

@York 2014-12-21 02:33:57

This works. However, the problems are: 1. The connection is extremely slow. It takes a few seconds for the "enter password" prompt to show up. 2. The password is not buffered, i.e., you have to enter the password of the ssh key each time you ssh to the VM.

@York 2014-12-21 02:51:59

I've figured out the reason for not being able to buffer the ssh password. That was because I didn't add the public key to my VM. However, the login is still extremely slow and takes at least five seconds.

@Seraphim 2015-07-22 11:27:50

Why are we using port 3022 and not the default 22?

@Matt Kneiser 2015-08-10 06:14:48

The comments weren't that clear - I logged into the guest OS (Ubuntu 14.04 in my case) and ran sudo apt-get update and sudo apt-get install openssh-server. After that I ran the VM headless on my Mac and was able to ssh in :)

@lenhhoxung 2015-12-17 21:16:00

Thank you, it's an amazing answer

@Frerich Raabe 2016-01-29 09:40:52

@York I use an expect script to automate an SSH session, including the password input.

@AbdulBasit 2016-03-20 14:12:33

The ssh is not connecting saying Connection closed by 10.0.2.15

@enthusiasticgeek 2016-04-07 15:56:53

Just as a helpful tip to list all virtual machines before you start this task, use VBoxManage list vms

@vancexu 2016-04-12 06:07:53

To make life easier, open ~/.ssh/config, add the following Host mybox HostName 127.0.0.1 User myuser Port 3022 to that file (add indent yourself), then you only need to ssh mybox each time :)

@not2qubit 2016-11-01 12:28:33

If you do route -n in your linux VM, you'll find the IP to ssh into on the default line. On my windows it is: 10.0.2.2. (When adapter is NAT and with the port forwarding as above.)

@user3943543 2017-03-07 22:22:09

thank you for taking the time to write this!!!

@Ben Creasy 2017-07-22 23:51:52

I prefer stackoverflow.com/a/10410527/4200039 as it avoids messing around with port forwarding.

@Klemenko 2018-03-01 08:49:06

damn, tnx... I tried everything then simple port fordwarding works

@raddaqii 2018-04-27 13:10:19

@Seraphim, we are using Port 3022 because possibly the machine hosting the VM may already have an SSH daemon listening on port 22. This extends the answer beyond Windows machines running VirtualBox, e.g. to a linux host environment. Point here is a) one port per service (can't listen on 22 if a service already uses it) and b) we need to use a non-privileged port higher than 1024.

@smoe 2018-10-21 11:55:00

I found this to be of enormous help for me to access a VM that did not allow to login graphically any more but that had an SSH server installed. Many thanks!

@Yuci 2018-02-10 20:17:41

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

@Piyush Raj 2017-11-30 04:50:29

Follow below steps to login to your ubuntu VM running in virtual box from the host machine using putty (Without port forwarding):

  1. On Virtualbox manager select the vm, click on settings icon. Then go Networks and enable two adaptors as below:

    • Adaptor 1 (For internet access): Attached to -> NAT, Advanced -> Check the cable connected.
    • Adaptor 2: Attached to -> Host only adaptor, Advanced -> Check the cable connected and Promiscuous mode -> Allow all.
  2. Start the ubuntu vm.

  3. Login to the VM as root.
  4. Edit the file '/etc/network/interfaces' as below and save it:

    auto lo
    iface lo inet loopback
    
    auto eth0
    iface eth0 inet dhcp
    
    auto eth1
    iface eth1 inet dhcp
    
  5. Restart the VM.

  6. Login to the VM and run below command to check the IP allocated to eth1:

    ifconfig
    
  7. Use this IP to open putty session for the VM.

@rofrol 2014-11-26 14:52:31

How to do host-only network (better than bridged) for Solaris 10 and Ubuntu 16.04

Add Host-only interface

  1. Virtualbox > File > Preferences > Network > Host-only Networks > Add
  2. Shutdown vm.
  3. VM's Settings > Network. First adapter should be Nat, second Host-only.
  4. Start cmd.exe and run ipconfig /all. You should see lines:

    Ethernet adapter VirtualBox Host-Only Network:
       ...
       IPv4 Address. . . . . . . . . . . : 192.168.59.1
    

    Second adapter in guest should also be in 192.168.59.*.

  5. Start VM.

Solaris 10

  1. Check settings ifconfig -a. You should see e1000g0 and e1000g1. We are interested in e1000g1.
  2. ifconfig e1000g down
  3. ifconfig e1000g 192.168.56.10 netmask 255.255.255.0 up
  4. Check from host if this interface is reachable: ping 192.168.56.10

Preserve those settings upon reboot

# vi /etc/hostname.e1000g1
192.168.56.10 netmask 255.255.255.0
# reboot

Configure ssh service (administering) to login as root (not adviced)

Check if ssh is enabled

# svcs -a | grep ssh
online         15:29:57 svc:/network/ssh:default

Modify /etc/ssh/sshd_config so there is

PermitRootLogin yes

Restart ssh service

svcadm restart ssh

From host check it

ssh [email protected]

Ubuntu 16.04

List interfaces:

ip addr

You should see three interfaces like lo, enp0s3, enp0s8. We will use the third.

Edit /etc/network/interfaces

auto enp0s8
iface enp0s8 inet static
    address 192.168.56.10
    netmask 255.255.255.0

Then sudo ifup enp0s8. Check if enp0s8 got correct address. You should see your ip:

 $ ip addr show enp0s8
 ...
    inet 192.168.56.10/24 brd 192.168.56.255 scope global secondary enp0s8

If not, you may run sudo ifdown enp0s8 && sudo ifup enp0s8

https://superuser.com/questions/424083/virtualbox-host-ssh-to-guest/424115#424115

@Quolonel Questions 2017-09-10 11:15:20

This doesn't work if your host-only network is on 192.168.59.1 and you specify 192.168.56.10 for the static guest IP. The entire class C subnet address must match exactly, so you would have to use something like 192.168.59.10 inside the guest instead.

@Alexandre Mélard 2013-12-02 21:27:19

For Windows host, you can :

  1. In virtualbox manager:
    1. select ctrl+G in your virtualbox manager,
    2. then go to network pannel
    3. add a private network
      1. make sure that activate DHCP is NOT selected
  2. In network management (windows)
    1. Select the newly created virtualbox host only adapter and the physical network card
    2. Right-Click and select "Make bridge"
  3. Enjoy

@anunaki 2016-07-18 10:44:56

Use NAT network adapter and Add port forward. Mention actual host ip.Do not use 127.0.0.1 or localhost.

@NoobNet14 2015-11-06 22:47:15

SSH Back to Your Home / Office VirtualBox Guest Machine From The INTERNET

The answers provided by other users here : How to SSH to a VirtualBox guest externally through a host?

... helped me to accomplish the task of connecting from out on the internet to my home computer's guest machine. You should be able to connect using computers, tablets, and smart phones (android, IPhone,etc). I add a few more step in case it might be helpful to someone else:

Here is a quick diagram of my setup:

  • Remote device ---> INTERNET --> MODEM --> ROUTER --> HOST MACHINE --> GUEST VM

  • Remote device (ssh client) ---> PASS THRU DEVICES ---> GUEST VM (ssh server)

  • Remote device (leave ssh port 3022) ---> INTERNET --> MODEM --> ROUTER (FWD frm:p3022 to:p3022)--> HOST MACHINE (FWD frm:p3022 to:p22) --> GUEST VM (arrive ssh port 22)

The key for me was to realize that ALL connections was PASSING-THROUGH intermediary devices to get from my remote PC to my guest virtual-machine at home --Hence port forwarding!

Notes: * Need ssh client to request a secure connection and a running ssh server to process the secure connection.

  • I will forward the port 3022 as used in the chosen answer from above.

  • Enter your IPs where needed (home modem/router, host IP, guest IP,etc.), Names chosen are just examples-use or change.

1.Create ssh tunnel to port 3022 on your modem's IP / router's external IP address.

ssh client/device possible commands: ssh -p 3022 [email protected]_external_IP

2.Port forward = we are passing thru the connection from router to host machine

  • Also make sure firewall /IPtable rules on router is allowing ports to be forward (open if needed)

  • Router's Pfwd SCREEN required entries: AppName:SSH_Fwd, Port_from: 3022, Protocol:both (UDP/TCP), IP_address:hostIP_address, Port_to:3022, everything else can be blank

DD-WRT router software resources / Info:

3.Host Machine Firewall: open port 3022 #so forwarded port can pass thru to guest machine

  • Host Machine: Install VirtualBox, guest additions, and guest machine if not done already

  • Configure guest machine and then follow the Network section below

  • I used VirtualBox GUI to setup guest's network- easier than CLI

  • If you want to use other methods refer to : VirtualBox/manual/ch06.html#natforward

4.Some suggest using Network Bridge adapter for guest = access to LAN and other machines on your LAN. This also pose an increase security risk, because now your guest machine is now exposed to LAN machines and possibly the INTERNET hackers if firewall not setup properly. So I selected Network adapter attached to NAT for less exposure to bridged security risks.

On the guest machine do the following:

  • Guest Machine VirtualBox Network settings: Adapter 1: Attached to NAT
  • Guest Machine VirtualBox Port Forwarding Rule: Name:External_SSH, Protocol:TCP, Host Port: 3022, Guest Port 22, Host&guest IPs:leave blank
  • click on advance in Network section then click on Port forwarding to enter rules
  • Guest Machine Firewall: open port 22 #so ssh connection can enter
  • Guest Machine: Make sure that ssh server is installed, configured properly, and running
  • LINUX test to see if ssh server running w/command: sudo service ssh status
  • Can check netstat to see if connection made to port 22 on the guest machine

Also there are different ssh servers and clients depending on platform using.

  • wikipedia/Secure_Shell
  • wikipedia/Comparison_of_SSH_servers
  • wikipedia/Comparison_of_SSH_clients

For Ubuntu Users:

  • ubuntu community: SSHOpenSSH/Configuring
  • ubuntu/community: OpenSSH/Keys

That should be it. If I made a mistake or want to add anything -feel free to do so-- I am still a noob.

Hope this helps someone. Good luck!

@Andrew 2015-02-24 19:00:42

You can also initiate a port forward TO your HOST, OR ANY OTHER SERVER, from your Guest. This is especially useful if your Guest is 'locked' or can't otherwise complete the ModifyVM option (e.g. no permission to VBoxManage).

Three minor requirements are 1) you are/can log into the VirtualBox Guest (via 'console' GUI, another Guest, etc), 2) you have an account on the VirtualBox HOST (or other Server), and 3) SSH and TCP forwarding is not blocked.

Presuming you can meet the 3 requirements, these are the steps:

  1. 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).
  2. 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.
  3. 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

@tensai 2013-10-02 12:35:18

On secure networks setting your network to bridge might not work. Administrators could only allow one mac address per port or even worse block the port should the switches detect multiple macs on one port.

The best solution in my opinion is to set up additional network interfaces to handle additional services you would like to run on your machines. So I have a bridge interface to allow for bridging when I take my laptop home and can SSH into it from other devices on my network as well as a host only adapter when I would like to SSH into my VM from my laptop when I am connected to the eduroam wifi network on campus.

@boulder_ruby 2013-09-10 01:15:21

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.

@boulder_ruby 2014-07-18 00:21:51

This information I'm guessing is partially or even entirely inaccurate in some general sense, I have no idea. downvoter didn't bother to say.

@boulder_ruby 2014-07-18 00:24:57

To wit, "for some, it does not work."

@rodrunner 2013-03-10 11:55:11

A good explanation about how to configure port forwarding with NAT is found in the VirtualBox documents: http://www.virtualbox.org/manual/ch06.html#natforward

@JohnD 2011-05-06 03:37:00

Change the adapter type in VirtualBox to bridged, and set the guest to use DHCP or set a static IP address outside of the bounds of DHCP. This will cause the Virtual Machine to act like a normal guest on your home network. You can then port forward.

@CantGetANick 2012-03-14 11:47:37

Any Idea how 2 adapter configuration will work? One on NAT and other on Host Only?

@Victor P. 2012-12-10 23:17:28

In my opinion exposing the VM on the local network is not the best option (for instance in an office network), the solution proposed by @vkostromin keeps the encapsulation of the VM within the host.

@mdo123 2017-08-16 18:02:55

@VictorP. can you please explain why it's not the best option in an office network? Is it due to security or?

@James 2018-03-16 20:49:02

Even knowing that is not the best option, in my current configuration set the adapter to bridged is the only solution that works.

@orip 2012-05-02 08:53:55

Keeping the NAT adapter and adding a second host-only adapter works amazing, and is crucial for laptops (where the external network always changes).

http://muffinresearch.co.uk/archives/2010/02/08/howto-ssh-into-virtualbox-3-linux-guests/

Remember to create a host-only network in virtualbox itself (GUI -> settings -> network), otherwise you can't create the host-only interface on the guest.

@brokenfoot 2014-04-09 23:19:59

Only this answer worked for me, nothing else did. Also, I just added additional adapter and it worked, didn't need to edit the /etc/network/interfaces, Thanks!

@Kirk Woll 2016-06-01 20:10:30

Agreed this is the best answer. Otherwise, you're jumping through hoops every time you want to access a common service (ssh, sftp, etc.) since you'd have to go through a non-standard port.

@Ben Creasy 2017-07-22 23:53:52

@mdo123 2017-08-16 17:53:01

another good source on setting this up 2buntu.com/articles/1513/…

@Sparkler 2018-02-21 15:19:09

ping from the host to the guest works but ssh returns port 22: Connection refused.

@orip 2018-02-21 19:56:25

@Sparkler is sshd running? Is there a firewall closing ports?

@ascendants 2019-11-18 21:32:50

Note that if the host-only adapter name (should be vboxnet0) is not shown, you have to create it first via: File – Host Network Manager – Create – [use defaults].

Related Questions

Sponsored Content

16 Answered Questions

[SOLVED] How to use SSH to run a shell script on a remote machine?

20 Answered Questions

16 Answered Questions

[SOLVED] How to resize a VirtualBox vmdk file

  • 2012-07-25 21:36:20
  • Brian
  • 364327 View
  • 522 Score
  • 16 Answer
  • Tags:   virtualbox

3 Answered Questions

[SOLVED] How to ping ubuntu guest on VirtualBox

1 Answered Questions

Vagrant - provision guest in same virtualbox as host?

3 Answered Questions

[SOLVED] Port forwarding from Host port 80 to VirtualBox port 80 doesn't work

1 Answered Questions

[SOLVED] Netbeans can't add remote host running on Virtualbox

1 Answered Questions

1 Answered Questions

[SOLVED] How to login ssh from windows host to Linux guest in Virtualbox?

  • 2014-04-11 07:19:48
  • user2737980
  • 2668 View
  • 0 Score
  • 1 Answer
  • Tags:   virtualbox

3 Answered Questions

Port Forward Directly to a Guest OS with VirtualBox

Sponsored Content