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?

share|improve this question
3  
There's a much easier way to do this, using Vagrant (which creates ready-to-go VMs (ssh-accessable, ip-accessable) from a config file or GUI). I don't know your use-case, but I've written a large tutorial on How to create an ssh-accessable Ubuntu server VM with Vagrant, useful when you want to setup a LAMP-based development machine. – Sliq Sep 15 '13 at 16:30
1  
Vagrant is a great tool, but it isn't compatible with the more recent versions of virtualbox – LizH Mar 12 '16 at 14:16
2  
Best answer is further down: stackoverflow.com/a/10410527/2214806 – Michael Jul 26 '16 at 21:44
    
You can check this tutorial wiki.workassis.com/virtualbox-ssh-between-host-and-guest – Bikesh M Annur Nov 29 '16 at 6:45

13 Answers 13

up vote 1074 down vote accepted

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:

Host port 3022, guest port 22, name ssh, other left blank.

or from 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:

sudo apt-get install openssh-server

To SSH into the guest VM, write:

ssh -p 3022 user@127.0.0.1

Where user is your username within the VM.

share|improve this answer
113  
All day I've been thinking I did something wrong. Then I installed the server :( – Angelo R. May 14 '12 at 3:57
24  
@Keyslinger, is this what you were looking for? ssh -p 3022 user@127.0.0.1 That was the one step that I was missing and just went to find on another site. – D. Woods Dec 21 '12 at 4:50
9  
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" – Eduardo Russo Feb 28 '13 at 9:20
3  
thebigdog, you can headless directly: VBoxHeadless -s 'vmname' – vkostromin Feb 27 '14 at 19:19
2  
Why are we using port 3022 and not the default 22? – Seraphim Jul 22 '15 at 11:27

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.

share|improve this answer
1  
Any Idea how 2 adapter configuration will work? One on NAT and other on Host Only? – CantGetANick Mar 14 '12 at 11:47
15  
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. – Victor P. Dec 10 '12 at 23:17
1  
@VictorP. can you please explain why it's not the best option in an office network? Is it due to security or? – mdo123 Aug 16 '17 at 18:02

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.

share|improve this answer
1  
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! – brokenfoot Apr 9 '14 at 23:19
2  
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. – Kirk Woll Jun 1 '16 at 20:10
1  
1  
another good source on setting this up 2buntu.com/articles/1513/… – mdo123 Aug 16 '17 at 17:53

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 root@192.168.56.10

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

share|improve this answer
2  
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. – Quolonel Questions Sep 10 '17 at 11:15

You can also use a Bridged Network 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 user@IP_OF_VM

share|improve this answer
    
I always do like that, in my opinion the best solution – Lorenzo Barbagli Mar 3 '17 at 16:41
1  
Simplest answer. In newest version, it is called 'Bridged adapter.' – user2233706 Sep 23 '17 at 1:42
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 user-name@home_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!

share|improve this answer

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
share|improve this answer

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
share|improve this answer

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

share|improve this answer

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.

share|improve this answer
    
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 Jul 18 '14 at 0:21
    
To wit, "for some, it does not work." – boulder_ruby Jul 18 '14 at 0:24

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.

share|improve this answer

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

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.