182

I have set up a VM of Ubuntu server, have installed OpenSSH, and am now trying to connect to it using Putty. Within Putty, under "Host name", I put "Ubuntu", given this is what I thought it was called when I set up the VM. However, I just get the error: "Connection Timed Out".

I also tried putting "127.0.0.1" into the host name within Putty and just get "Connection Refused". Note that I have done the port forwarding for SSH and HTTP within Oracle VM, so I am at a loss as to how to get it running.

Jamal
  • 115
  • 6
wickywills
  • 2,093
  • 1
    FWIW bridged/host only didn't work for me, but I was able to work around it by, in essence, only SSH'ing "out" (scp can go both ways, after all) so I didn't need SSH access within the guest. – rogerdpack Dec 06 '16 at 21:09

6 Answers6

221

VirtualBox will create a private network (10.0.2.x) which will be connected to your host network using NAT. (Unless configured otherwise.)

This means that you cannot directly access any host of the private network from the host network. To do so, you need some port forwarding. In the network preferences of your VM you can, for example, configure VirtualBox to open port 22 on 127.0.1.1 (a loopback address of your host) and forward any traffic to port 22 of 10.0.2.1 (the internal address of your VM)

This way, you can point putty to Port 22 of 127.0.1.1 and VirtualBox will redirect this connection to your VM where its ssh daemon will answer it, allowing you to log in.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
michas
  • 21,510
  • 25
    Thanks, this worked for me. From cygwin I can now ssh username@127.0.1.1 and I'm in. The panel you need is Settings->Network->Port Forwarding, and mine now looks as shown at: http://imgur.com/BjGh3N2 – user7543 May 01 '15 at 15:26
  • 51
    This will NOT work if you already have ssh running on your host because the ports overlap. Redirect port 127.0.1.1:2222 to 10.0.2.X:22 and then run 'ssh 127.0.1.1 -p 2222' from your host' – dzlatkov May 29 '15 at 07:10
  • 11
    Also, make sure you have openssh-server installed on the client OS. sudo apt-get install openssh-server – Illuminati Sep 03 '16 at 13:29
  • 19
    Just had this issue myself - thanks for the explaination. I basically followed what you said. I went to Settings and my connection showed 'NAT'. I clicked the 'port forwarding' option at the bottom of the screen. I gave it a name 'PuTTY-port-fwd', protocol TCP, host IP 127.0.0.1 host port 2222 guest IP 10.0.2.15 guest port 22. I then launched PuTTY and set it to 127.0.0.1 port 2222 and it connected to my OpenSUSE vm. Thanks guys, hope this helps anyone in future ;-) – BubbleMonster Dec 03 '16 at 17:00
  • @BubbleMonster Your solution worked for me. Thank you. – Quazi Irfan Jan 16 '17 at 06:18
  • 4
    This worked for me, although I had to tell VirtualBox to open port 2222 on 127.0.0.1. Then I was able to log in using ssh -p 2222 user@127.0.0.1 – m81 Jan 25 '17 at 15:24
  • 1
    I had same problem I was Working with bridged connection. Actual problem was the openssh was not installed. Make sure you have it. – Anwar Shaikh Feb 12 '17 at 10:55
  • do the version of openssh on windows and that on linux on the virtual machine have to match? I have tried all combinations (127.0.1.1/127.0.0.1, port: 22/2222), updated the openssh on linux etc etc.. nothing worked..however my openssh version on windows and linux dont match windows has version 6.6 while linux has 7.2... – alpha_989 Jul 22 '17 at 21:03
  • 1
    important to note that you shouldnt be changing this under File>Preferences>Network (http://imgur.com/Wogy4Ok). If you do it here.. it wont work. You have to do it under Setting (http://imgur.com/5IMZE8I)>Port Forwarding Rules (http://imgur.com/G7agMje). – alpha_989 Jul 22 '17 at 22:31
  • 2
    Also, in my case, i didnt need to set the host ip or guest ip. As per https://www.howtogeek.com/122641/how-to-forward-ports-to-a-virtual-machine-and-use-it-as-a-server/, you dont need to do that, and I am able to connect to the host machine by ssh. I pretty much spent 5 hrs before I figured out this difference and tried pretty much everything. Thought I would post the screenshots.. so somebody else doesnt waste 5 hrs of their life. :) – alpha_989 Jul 22 '17 at 22:33
  • In my case I can't forward any port below 1024. – Qback May 21 '18 at 16:28
  • In my case host ip needs to be 127.0.0.1, 127.0.1.1 doesn't work. – feilong Dec 13 '18 at 04:05
  • 1
    NOTE that 127.0.1.1 will work on Linux but not on MacOS. Would recommend using 127.0.0.1 to be safe. – sixty4bit Aug 31 '19 at 15:47
  • Tried port-forwarding from localhost:2222->guest:22. Not working. It connects, as shown by trying telnet, but ssh just hangs. $ telnet 127.0.0.1 2222
    Trying 127.0.0.1...
    Connected to 127.0.0.1.
    Escape character is '^]'. SSH-2.0-OpenSSH_8.0p1 Ubuntu-6build1

    Connection closed by foreign host.

    This just hangs indefinitely

    $ ssh joe@127.0.0.1 -p 2222 Pseudo-terminal will not be allocated because stdin is not a terminal.

    – Jack Mar 14 '20 at 22:10
61

I wanted to use putty to connect to my ubuntu on virtual box (comfort reasons, the VB is just weird. I can't work unless it is on a proper terminal). Anyway,

  1. Make sure ssh client is installed on your Linux. If not, install it sudo apt install ssh.
  2. Power off the OS.
  3. Now on your VB go to Settings -> Network -> on Adapter 1 choose Host-only adapter->click OK.
  4. Now start your OS. Run ifconfig; now the inet address is your IP.
  5. Use this and run it on your putty. Login with your credentials.

The only disadvantage of using host-only adapter is that your guest OS won't have access to the wider network (eg the Internet).

If you also need your VM to have internet access, leave Adapter 1 as NAT and enable Adapter 2, configured as a Host-Only adapter. This will allow your VM to connect to the internet using NAT as well as make a local connection to your Host using Host-Only.

Saikat
  • 105
  • 6
    "bridged" will put the ip on your local interface and bridge access to the greater internet – Duke Oct 11 '16 at 02:37
  • 3
    The advantage of using two interfaces, as described here, over bridged, is that you don't rely on the local dhcp server accepting your requests. Most dhcp servers in public places (ariport, airplanes, hotels, and even most company networks) won't offer a second ip through the same wifi adapter, so a bridged VM will not work in those environment.

    Here's a really good step-by-step on how to set this particular config up:

    http://christophermaier.name/2010/09/01/host-only-networking-with-virtualbox/

    – Yves Dorfsman Jun 30 '17 at 13:37
  • Work like a charm, but you should put Adapter 2 on the 3rd option so that people leave the NAT alone – user3502626 Jan 13 '19 at 05:20
  • thanks very much. but when I change the Ubuntu Network setting from NAT to host only adapter it can't connect to internet anymore, any solution? – Luke May 15 '19 at 11:23
  • This is perfect. Most of the answers I found suggests that the SSH client is installed in the guest machine. In my case it was not. – Twister1002 May 13 '20 at 15:23
  • did everything right.. except install ssh on my vm.. been stuck for hours.. thanks man or woman.. – Mubashar Abbas Jan 06 '21 at 13:50
27

First you need to decide if your VM connected to your host machine via a bridge connection or via a NAT, but ether way you'll need to put the VM IP address in putty to be able to connect to ip, in the VM terminal run this command to show you the machine IP address (and no 127.0.0.1 is not the machine IP address)

VM # ip addr show

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:d9:16:b3 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.1 scope global eth0
       valid_lft forever preferred_lft forever

in this case my IP address will by 10.0.2.15,

First try to make sure you can communicate on a basic level with VM, open a terminal window on your host, and try to ping the VM

HOST # ping 10.0.2.15

PING 10.0.2.15 (10.0.2.15) 56(84) bytes of data.
64 bytes from 10.0.2.15: icmp_seq=1 ttl=64 time=0.045 ms
64 bytes from 10.0.2.15: icmp_seq=2 ttl=64 time=0.110 ms
64 bytes from 10.0.2.15: icmp_seq=3 ttl=64 time=0.099 ms

If you get ant result, then make sure you have a ssh service running on the VM, in the terminal on your VM type as root,

VM # netstat -lnpt | grep 22
tcp   0  0  0.0.0.0:22  0.0.0.0:*  LISTEN  2361/sshd

This tell as we have a service/process with PID(2361) called sshd (OpenSSH daemon) listening to port 22.

You can test if the service work correctly by trying to ssh to it from the VM it self,

VM # ssh 127.0.0.1

Next you neet to make sure that you are not blocking port 22 in your firewall/iptables, I can not believe so, but check it out anyway. In the VM type this command to show you the iptables,

VM # iptables -nvL INPUT

in the output you should have line like this one:

0  0  ACCEPT  tcp  --  *  *  0.0.0.0/0  0.0.0.0/0  tcp  dpt:22  ctstate  NEW
Rabin
  • 3,883
  • 1
  • 22
  • 23
18

My resolution was similar to Roman T's however I needed to add a few extra steps. In my case I had Ubuntu Server 14 VM running on a Windows 8 Desktop in Windows 2008 domain. If I tried NAT or Bridge I could access the Internet but couldn't connect via SSH.

If I tried Host Only Adapter then that would allow me to SSH to machine but couldn't access the Internet.

I tried Port forwarding as well and no joy. Opened up Wireshark and it just wasn't finding VM.

So my solution was to add a second network adapter.

Method

With VM powered down

  1. Click Settings > Network Click Adapter 1 and choose Bridged Adapter
  2. Click Adapter 2 and choose Host Only Adapter
  3. Click File > Preferences > Networks Under NAT Networks if you don't see a NAT Network click on + icon to add NAT Network.
  4. Click Host-Only Networks if you don't see a host only network click + icon to add one

Start up VM

  1. In order to see network adapter you need to type

    ifconfig -a
    
  2. You may see the network adapter is added with a mac address but not an IP?

  3. If so then you need to edit /etc/network/interfaces in order to configure DHCP. Example below using VI/VIM but you can use editor of your choice

    sudo vi /etc/network/interfaces
    
  4. add the lines

    auto eth1
    iface eth1 inet dhcp
    
  5. save and exit file.
  6. Then try restarting network service using below command

    sudo service networking restart
    
  7. Or if that fails then restart VM.
  8. Once restarted type below to see if you eth1 has been allocated an IP address

    ifconfig -a
    
  9. if so then see if you can SSH on to the VM
  • I had tried your solution, but no IP has been assigned to new interface. What is more I have DHCP enabled in my VBox. – Athlan Feb 21 '16 at 22:21
15

The following instructions worked with Ubuntu 14.04 and Oracle VirtualBox 4.3.30.

Do this in VirtualBox:

  1. Right-click your virtual machine, select "Settings", and then select "Network".

  2. Next to "Attached to", select "Host-only Adapter". As a side note, "Bridged Adapter" will also work, check the VirtualBox documentation for more details about each option.

Do this inside your Virtual Machine:

  1. Find your network IP address by opening a terminal and typing ifconfig. Observe the IP address displayed next to eth0, under "inet addr". You can also see your IP address if you click the Network icon in the upper-right corner of your desktop, and then select "Connection Information".

  2. Install openssh-server by typing the following at the terminal:

    sudo apt-get install openssh-server
    
  3. Just in case, restart the virtual machine.

Now you can connect from PuTTY using the IP address from step 1 above and port 22.

Roman T
  • 159
6

For Ubuntu 18.04 and VirtualBox 5.2:

  1. Create a Host Network Interface

    i. On Virtualbox, click File/Host Network Manager.

    ii. If you don't already have a Host Network adapter (default vboxnet0), click Create.

  2. Enable Host-only Adapter settings for VM

    i. On Virtualbox, right click on your VM and select Settings.

    ii. Click Network and select Adapter 2.

    iii. Click Enable Network Adapter.

    iv. Under Attached to: select Host-only Adapter. The name of the adapter you created in step 1 should appear (default vboxnet0).

  3. Configure network settings in VM

    i. Start your VM and check which interface was added: ip a. Look for the interface that doesn't have an inet address. On mine, it was enp0s8.

    ii. Edit /etc/netplan/01-netcfg.yaml. e.g. sudo vi /etc/netplan/01-netcfg.yaml.

    iii. Under the settings for the original adapter, add configuration details:

    enp0s8:
        addresses: [192.168.56.2/24]
        gateway4: 192.168.56.1
        dhcp4: no
    

    This assigns a static ip address for your VM for SSH convenience. If you want dhcp to handle the addressing instead, leave out the address and gateway config and set dhcp4 to yes.

    iv. Reload the configuration file: sudo netplan apply.

  4. SSH into your VM

    i. If you have not installed ssh in the VM: sudo apt install ssh.

    ii. From your host machine, SSH into the VM: ssh <username>@192.168.56.2.

ooknosi
  • 416