HOW TO SSH TO LINUX ON VIRTUALBOX

Published: July 18, 2023 (Updated: Jul 18, 2023)

Enjoying this content? Subscribe to the Channel!

Solved: How to SSH from Windows Host to CentOS (or Any Linux) Guest in VirtualBox


Welcome back to Darren’s Tech Tutorials!

If you’re using VirtualBox to run Linux—maybe CentOS, Ubuntu, Debian, or Red Hat—you know how crucial it is to manage that system remotely. Working directly in the VirtualBox window is fine, but sometimes you just need the power and convenience of your host machine’s dedicated terminal or SSH client (like PuTTY or PowerShell).

The problem? You likely tried to SSH into your guest machine and were met with the dreaded “Connection Timed Out” error.

This isn’t a bug; it’s just how VirtualBox’s default networking (NAT) handles things. Good news: The fix is incredibly simple, requires only a quick change, and will open up seamless communication between your Windows host and your Linux guest.

In this tutorial, we’ll walk through exactly why the connection fails and provide the step-by-step instructions to configure VirtualBox Port Forwarding, allowing you to SSH flawlessly!


The Root Cause: Why Default VirtualBox Networking Blocks SSH

When you first create a virtual machine in VirtualBox, the default network setting is usually Network Address Translation (NAT).

NAT allows your virtual machine (the guest) to access the internet using your host machine’s IP address. It’s like your VM is hiding behind your Windows machine. While this is great for allowing outbound traffic (browsing the web from the VM), it prevents unsolicited inbound traffic.

Since SSH requires an inbound connection from your Windows host to the Linux guest, the NAT boundary stops the connection cold.

To solve this, we don’t need to change the network type entirely (like switching to Bridged mode, which can be overly complicated). Instead, we will use NAT’s built-in Port Forwarding feature to create a secure tunnel directly to the SSH server on your guest OS.

Prerequisites: Prepare Your Linux Guest

Before making changes in VirtualBox, quickly confirm that your Linux guest machine has the SSH server installed and running. If you are using a minimal install of CentOS or other distributions, it might not be there by default.

  1. Start your Linux VM.
  2. Open the terminal.
  3. Verify the SSH service:
    sudo systemctl status sshd
    
  4. If the service is not active, install it:
    • For CentOS/Red Hat:
      sudo yum install openssh-server
      sudo systemctl enable sshd
      sudo systemctl start sshd
      
    • For Ubuntu/Debian:
      sudo apt update
      sudo apt install openssh-server
      sudo systemctl enable ssh
      sudo systemctl start ssh
      

Once you confirm the service is running, shut down your VirtualBox machine. Do not just pause it; shut down the machine completely.

The Fix: Configuring VirtualBox Port Forwarding

The key to seamless connectivity is setting up a Port Forwarding rule within your VM’s network settings. This rule tells VirtualBox: “When traffic comes into Port 2222 on the Host machine, automatically route it to Port 22 on the Guest machine.”

Here’s how to do it:

Step 1: Access VM Network Settings

  1. Open the main VirtualBox Manager application on your Windows host.
  2. Select your CentOS (or other Linux) VM from the list (do not start it).
  3. Click the Settings icon.
  4. In the Settings window, navigate to Network.
  5. Ensure that Adapter 1 is enabled and set to Attached to: NAT.

Step 2: Create the Port Forwarding Rule

  1. While still in the Network settings, click the Port Forwarding button at the bottom of the window.
  2. Click the green ‘Add new rule’ icon (a plus sign).
  3. Configure the rule exactly as follows:
Setting Value Description
Name SSH Rule A simple descriptive name.
Protocol TCP SSH uses TCP.
Host IP Leave Blank (Or use 127.0.0.1)
Host Port 2222 We use 2222 because Port 22 on the host might be reserved.
Guest IP Leave Blank Let VirtualBox handle the routing.
Guest Port 22 The standard port for SSH on Linux.
  1. Click OK to save the Port Forwarding rule.
  2. Click OK again to close the VirtualBox Settings window.

Connecting via SSH from Your Windows Host

Now that the port is open and the tunnel is configured, you can treat the forwarded port (2222) on your Windows loopback address (127.0.0.1) as the entrance to your VirtualBox guest.

Step 3: Start the VM and Connect

  1. Start your Linux VirtualBox VM normally. Let it boot fully.
  2. Open your Windows terminal (PowerShell, Command Prompt, or your favorite SSH client like PuTTY).
  3. Use the following command syntax to connect, replacing username with the user account on your CentOS machine:
ssh [email protected] -p 2222
  1. When prompted, enter the password for your Linux user account.

Success! You should now be logged into your CentOS system via SSH, running directly from your Windows host terminal. You can now use all the power of SSH, including SCP (secure copy) for transferring files between the host and guest without setting up shared folders.


Summary and Next Steps

By setting up a simple NAT Port Forwarding rule, we bypassed the networking limitations of VirtualBox and established a robust, reliable, and secure SSH connection. This process works for virtually any Linux guest operating system, including Ubuntu, Red Hat, Debian, and of course, our CentOS system!

Give this a try today and take control of your virtual development environment!

If this tutorial helped you crack the code on VirtualBox networking, please smash that like button and subscribe to Darren’s Tech Tutorials for more practical, straightforward guides. Happy coding!