SSH into VirtualBox Guest: Connection Refused

23

10

Setup

  • Windows 7 64-bit host OS running VirtualBox 4.2, with Ubuntu 12.04 guest OS.
  • OpenSSH server is installed and running (ssh -v localhost connects locally in the guest machine).
  • Can SSH to external servers (no outbound Windows firewall rule blocking port 22)
  • Can ping the IP of the guest (192.168.56.101)

Problem

Using PuTTY to SSH to the IP of the guest OS (192.168.56.101), PuTTY returns almost immediately with

Network error: connection refused

How can I diagnose & resolve this issue?

Eric J.

Posted 2012-09-25T00:58:43.523

Reputation: 1 449

Answers

11

Are you sure the IP is for the guest OS. I had the same problem and it turned out that the IP I had been using all along, the 192.168.56.101 was actually the Windows interface IP and the host OS IP was 192.168.56.103.

You can verify by doing ipconfig / ifconfig on both.

NOTE: I had also setup a static IP which it turns out I didn't need at all. In case you try from the start I found this tutorial to be better.

Saad Farooq

Posted 2012-09-25T00:58:43.523

Reputation: 237

+1 After 1h of Google-mess, finally! I thought the .101 was the guest not the host! – Afr – 2014-07-08T17:33:23.463

6+1, The VirtualBox default is to create a guest whose virtual network adapter is NAT'ed through VirtualBox itself. Therefore such a guest would be able to SSH out, and would be able to SSH itself, but it would not be able to receive SSH connections until Port Forwarding rules were created in VirtualBox. – Gord Thompson – 2013-03-17T12:30:29.517

7

None of the other solutions are good because:

  1. There is no point to ssh to your guest via bridge if the guest will loose access to internet
  2. All the other solutions above tell you to build a rocket to cross the road.

Scenario

  • Host Ubuntu Desktop 16.04
  • Guest Ubuntu Server 16.04 (Running on Virtual box 5)

Problem

SSH connection refused

Solution

  1. Shut your guest Ubuntu down
  2. On Virtualbox go to Setting>network>Adapter1>Advanced>Portforwarding
  3. Name=SSH Hostport=2022 (or any port but 22) Guestport(22)
  4. Reboot your guest
  5. On you host open a Putty or whatever you use for SSH
  6. ssh user@127.0.0.1 -p2022 (or any port you mentioned as hostport)
  7. Have fun you are in

Henrique Oliveira

Posted 2012-09-25T00:58:43.523

Reputation: 79

i have problem with this: On Virtualbox Setting>network>Adapter1>Advanced>Portforwarding is gray not clickable. https://imgur.com/seT3p02

– SL5net – 2019-04-02T11:30:47.060

@SL5net turn off the VM – Alon Gouldman – 2019-11-08T05:31:06.660

2

The following works for me:

Before:

vboxnet0: 192.168.56.1
eth1:     192.168.56.1

After:

vboxnet0: 192.168.56.1
eth1:     192.168.56.101

So, basically I changed part of the file { /etc/network/interfaces } as follows:

Before:

auto eth1
iface eth1 inet static
address 192.168.56.1
netmask 255.255.255.0 

After:

auto eth1
iface eth1 inet static
address 192.168.56.101
netmask 255.255.255.0 

Works: ssh username@192.168.56.101

Mause Hai

Posted 2012-09-25T00:58:43.523

Reputation: 21

Don't know why, this worked for me. Thanks @Mause – murarisumit – 2017-10-04T11:44:07.663

2

You should check that your guest OS firewall is not blocking incoming connections on port 22.

Serge

Posted 2012-09-25T00:58:43.523

Reputation: 2 585

1The guest OS firewall is disabled. That is the default, and I verified it is disabled with sudo ufw status. – Eric J. – 2012-09-25T03:22:24.797

1

Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.

Here comes the information that the package openssh-server has to be installed. After doing that on the guest and a restart of the vm you can use ssh.

Alex44

Posted 2012-09-25T00:58:43.523

Reputation: 147

I didn't restart my Ubuntu 16.04 and it still worked. Thanks :) – Oren Milman – 2018-10-12T21:51:43.473

1

I had to solve this problem by adding an ufw rule for the 2nd adapter:

ufw allow in on enp0s8 to 192.168.56.101 port 22 proto tcp

where enp0s8 is the name of my 2nd adapter (on old Ubuntu distros this was eth1).

janb

Posted 2012-09-25T00:58:43.523

Reputation: 111

1

Hey I'm just returning to the first page I found on my mini-saga of troubleshooting and research, hoping this tutorial I made can save someone else all the hassle.

Start-to-end guide on setting up a CentOS server with Oracle VM:

Part I – Virtual Machine Setup

  1. Install Oracle VM VirtualBox and the expansion pack here.
  2. Download the CentOS minimal installation here.
  3. For this tutorial, I’m using an Ubuntu client. The ISO for that can be found here.
  4. Create the CentOS server and your client virtual machines.
  5. Setup the CentOS VM. a. Open Installation Destination and press Back. b. Go to Network & Hostname if you want to change your hostname, which I recommend. c. Feel free to configure items under Localization. Everything else is handled automatically by VirtualBox. d. Press begin installation. e. Create both a root account and a normal account. Make sure the latter has administrator rights. f. Once the loading bar is full, press Reboot.
  6. Setup your client VM. a. Install VirtualBox guest additions.

Part II – Virtual Network Configuration

  1. In VirtualBox, press CTRL+W.
  2. Create a new host network. a. If you receive an error, this is likely due to your host OS causing VirtualBox problems installing its drivers. You will need to fully uninstall VirtualBox and reinstall the latest version. Make sure to run the installation as admin.
  3. Under the Adaptor tab, select “Configure Adapter Manually”
  4. For Windows, open the Command Prompt and type ipconfig.
  5. Scroll through the output until you find the name of the host-only network you just created. Use this IPv4 Address and Mask for the VirtualBox network configuration.
  6. Close the Host Network Manager.
  7. Open the settings of your client VM. Under Network, Enable Adapter 2, select Host-only Adapter and then the name is the name of your newly-created host-only network.
  8. Repeat the last step for your CentOS server VM. Additionally, go to Adapter 1’s Port Forwarding under the Advanced tab and create a new rule. Set both the host and guest ports to 22.
  9. Before closing the advanced settings, note the MAC address.

Part III – Server Configuration

  1. Launch your CentOS VM.
  2. Run yum check-update, yum upgrade, and yum clean all.
  3. Run nmtui.
  4. Hit enter over “Edit a connection.”
  5. You should see “enp03s” and a “Wired Connection 1”. a. If you have two connection options but are unsure of which is which, go to edit one. If the device value matches the MAC address seen under Adapter 1 from Part II, then it’s your NAT connection, otherwise its your host-only connection.
  6. Edit your Host-only connection: a. Set IPv4 configuration to “manual.” b. Show IPv4 configuration c. In VirtualBox, press CTRL+W. Next to your Host-only network name, you should see and IP Address/Mask combination of the form “[ip]/[mask].” On your server under IPv4 configuration, this is what you will type after pressing <Add…>. d. Go down and press OK.
  7. Make sure “Automatically connect” is checked for both your Host-only and NAT connections.
  8. Navigate out of nmtui and reboot.

Part IV – Client Configuration & Usage

  1. Boot your Client VM.
  2. (optional) Edit /etc/hosts to include the a line of the form: [serv_ip_addr] [hostname] Where serv_ip_addr is the ip address of your CentOS server (second output of hostname -I in your CentOS terminal). This will allow you to use whatever you chose for [hostname] interchangeably with the ip address of your server. I typically just set this to the hostname chosen in part II.
  3. If everything worked up to this point, you should be able to ssh [user]@[hostname] into your CentOS server. If this is not the case, you may need to install openssh-client and openssh-server on your machines.

Part V – Serving Up Websites

  1. This is an excellent guide on hosting your own websites from a setup as described above.

Frosh_VII

Posted 2012-09-25T00:58:43.523

Reputation: 11

1

Maybe it's an issue with access control. To disable it and allow full access, as root:

xhosts +

I had a similar issue with VNC viewer

Jay

Posted 2012-09-25T00:58:43.523

Reputation: 1 038

1I really doubt that, IMHO sshd has nothing to do with X -- in contrast to vnc. – mpy – 2013-03-16T20:44:58.610

1

Problem was solved by enabling the DHCP server on the VirtualBox network adapter.

VirtualBox (latest version, version 5) -> File -> Preferences -> Network -> Host-only networks (tab) -> Host-only networks details (Icon tool) -> DHCP Server

  1. Tick Enable Server
  2. Fill in the IP address
  3. OK, OK
  4. Restart the virtual machine

My settings for step 2

  1. Server Address: 192.168.56.100
  2. Server Mask: 255.255.255.0
  3. Lower Address Bound: 192.168.56.101
  4. Upper Address Bound: 192.168.56.200

thanos.a

Posted 2012-09-25T00:58:43.523

Reputation: 155

-1

In your Linux box (VirtualBox Ubuntu), open the Terminal and enter the following command: sudo service ssh start

Try connecting to it again after this

Oh my bad I missed what gronostaj pointed out

OK sometimes the solutions are really simple. recently I faced the same issue with Slackware guest on windows 7 host. All i had to do was ifconfig up eth'X' and assign IP 192.168.56.xxx sub net 255.255.255.0 (if host only adapter) This question is almost one year old, so I'm mentioning this for someone like me stumbles on this page searching for "ssh connection refused" ;-)

Deloader

Posted 2012-09-25T00:58:43.523

Reputation: 69

OP had stated that local SSH connections work, so the server is running. – gronostaj – 2013-03-16T22:02:26.683

-2

Had the same problem, solved it by adding a virtual hard drive to the VM, which had none.

Louis Garczynski

Posted 2012-09-25T00:58:43.523

Reputation: 101

That means your problem was different. If your VM didn't have a hard drive it wasn't booted to an OS, which OPs VM was. Please read questions carefully to make sure you understand them before answering. – music2myear – 2019-05-13T20:49:31.827

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

– music2myear – 2019-05-13T20:49:37.247

My VM was booted on a Linux 32-bit ISO file, with no additional virtual hard drive – Louis Garczynski – 2019-05-15T06:15:47.487

Which means you were running a "Live" Linux session, which is definitely not what OP was dealing with. We're glad you solved your problem, but your solution is not the solution, or particularly even close, to what OP was experiencing. – music2myear – 2019-05-15T15:20:25.977

You are absolutely correct, I did not realize OP's problem was so specific. That said, considering I got the same error message, I will keep the answer here, just in case someone with my problem ends up here too. – Louis Garczynski – 2019-05-22T20:02:38.913

Actually, it would be better if you created your own question and then posted your own answer to it. Most people find answers by finding the question first, and because the question above doesn't say anything about a Live session, a question that DOES mention live sessions and this error would be more helpful. – music2myear – 2019-05-22T21:29:51.293