Install and Configure SSH on Ubuntu Server VM with Oracle VirtualBox

This guide shows how to install and configure SSH on an Ubuntu Server on VirtualBox running on Windows 11, though the process is similar for other host systems.

Introduction

In my previous post, Setting up an Ubuntu Server VM with Oracle VirtualBox , we looked at setting up an Ubuntu Server VM in Oracle VirtualBox. To keep that tutorial short, the steps for installing and configuring SSH were omitted. In this post, we’ll look at how to complete that step.

Tested with: VirtualBox 7.1.12, Windows 11 (24H2 at time of writing), and Ubuntu Server 26.04.1 LTS. The version of Ubuntu is newer than my previous tutorials, which used 24.04.3, but the instructions should work on either version of Ubuntu Server.

NOTE: These instructions assume installation of SSH on a virtual machine for testing or development projects only. They are NOT intended for proper configuration of SSH on a production server.

Prerequisites

  • OpenSSH installed on Windows.
  • Functioning Ubuntu Server VM running in VirtualBox.
  • Internet connection available to the server VM.
  • User with sudo access.

Steps to Install and Configure SSH on the Ubuntu Server VM

Step 1 - Install Updates and OpenSSH Server

Run sudo apt update && sudo apt install -y openssh-server.

  • The command sudo apt update refreshes the local list of available software packages.

  • The && chains the commands and will only run the second command, sudo apt install -y openssh-server, if the first command, sudo apt update, succeeds.

  • Finally, the command sudo apt install -y openssh-server installs OpenSSH. The -y tells apt to automatically answer “yes” to any confirmation prompts during the installation process.

Once the commands have completed, you should see a screen similar to the following:

Screen after Successful Installation of openssh-server

Screen after Successful Installation of openssh-server

Step 2 - Configure the SSH Server to Start Automatically

Run sudo systemctl enable --now ssh.

  • systemctl is a command line utility to manage services controlled by systemd.
  • enable tells the system to automatically launch the server during startup.
  • The --now flag tells the system to start the service immediately.
  • ssh is the target service being configured and launched.
Screen after Enabling SSH

Screen after Enabling SSH

Step 3 - Check That the Service is Running

Run sudo systemctl status ssh.

  • This command checks the status of the ssh service.

If everything is working correctly, you should see a screen like the following:

Screen after Enabling SSH

Screen after Enabling SSH

Step 4 (Oracle VirtualBox Specific) - Configure the VirtualBox Network Adapter

This is the second time I’ve walked through these instructions, and I knew I would run into possible issues at this point. I wanted to make sure to document that here to help out others who may encounter the same issues.

I got my server’s IP address (10.0.2.15) using hostname -I, but when I tried to run the command to connect to the server, I got the error “ssh: connect to host 10.0.2.15 port 22: Connection timed out”.

Connection timed out error

Connection timed out error

The first thing to check in the case of an Oracle VirtualBox VM is the network adapter setting. VirtualBox defaults to NAT for new virtual machines. I didn’t change the default when I created the server VM. Checking Machine > Settings… > Network confirms the virtual machine is configured to use NAT for the network adapter.

Network Adapter set to NAT

Network Adapter set to NAT

To get the connection working using NAT, we need to set up port forwarding.

From Machine > Settings… > Network, click the Expert tab. You should see a Port Forwarding button.

Port Forwarding Button

Port Forwarding Button

Click the Port Forwarding button and add a rule with the following settings:

  • Name: SSH
  • Protocol: TCP
  • Host IP: 127.0.0.1
  • Host Port: 2222
  • Guest IP: Blank
  • Guest Port: 22
SSH Port Forwarding Rule

SSH Port Forwarding Rule

Step #5 - Test Connection to SSH Server

With the network adapter issue out of the way, and a port forwarding rule created, the connection can be tested.

To test the connection, run ssh -p 2222 username@127.0.0.1 from the command line, where username is your server VM user.

In my case, the command is ssh -p 2222 vboxuser@127.0.0.1.

When I run this command, I am now prompted for the virtual machine user’s password:

Password Prompt

Password Prompt

I enter the password and I am successfully connected to the virtual machine through SSH:

Client Successfully Connected to Server

Client Successfully Connected to Server

You can type exit to disconnect the SSH client from the server.

IMPORTANT NOTE: Since this is just a tutorial, I’m using the vboxuser that was created in the server setup tutorial. In most cases, it is advisable to create a non-admin user and use that for SSH connections. One example might be running AI agents in sandboxed environments, where you wouldn’t want the SSH connection to have admin user access to the VM. However, for experimentation purposes only, we can use the vboxuser to ensure things are set up correctly.

Step 6 (Optional) - Set up an SSH key for Authentication

While this tutorial is focused on development and testing, disabling password authentication and using an SSH key pair instead is generally considered good practice, especially in production environments. I’m including the steps here for those users that want to understand how to apply this extra security step. A few things to keep in mind:

  • Never share your private key file with anyone and keep it only on client machines that need it.
  • In production environments, it is good practice to protect your private key with a passphrase.

Run ssh-keygen -t ed25519 on your local machine to generate a key pair.

The command will prompt you as to where to save the key. (I accepted the default, and the key was saved under my user’s home directory with the names id_ed25519 for the private key and id_ed25519.pub for the public key.)

Enter an optional passphrase if you want one to protect the key.

Copy the public key to the server. Since I’m running on a Windows host, I can’t use ssh-copy-id, as that command isn’t included with Windows OpenSSH.

I found a solution documented in this superuser.com question: Alternative to ssh-copy-id on windows .

That post recommends the command type "%USERPROFILE%\.ssh\id_rsa.pub" | ssh user@host "cat >> .ssh/authorized_keys". This will need to be updated for your specific public key file name, user and host name.

For this tutorial, the command is type "%USERPROFILE%\.ssh\id_ed25519.pub" | ssh -p 2222 vboxuser@127.0.0.1 "cat >> .ssh/authorized_keys". The syntax is specific to the Windows Command Prompt, so make sure to run it there, rather than in PowerShell.

Password Prompt Copying Public Key to Server

Password Prompt Copying Public Key to Server

I enter the password and am returned to the command prompt as if nothing happened. However, if all worked correctly, the key was copied to the server.

I can now run the command ssh -p 2222 vboxuser@127.0.0.1 again. This time, it immediately connects to the server without prompting me for the password:

Key-Based Connection Successful

Key-Based Connection Successful

Step 7 (Optional) - Disable Password Authentication on the Server VM

Open the SSH configuration file from the server by typing sudo nano /etc/ssh/sshd_config.

NOTE: Ubuntu uses a convention where config file settings can be placed in files in /etc/ssh/sshd_config.d/. These will override the default ssh config file. In my case, there is nothing in this directory. Using config snippets is out of scope for this tutorial, and I’m just going to change the default config file.

The config file should open in nano, as shown in the screenshot below:

SSH Config File Open in Nano

SSH Config File Open in Nano

The entire file likely won’t be visible in the nano editor. You use the Page Up and Page Down keys or the up and down arrow keys to scroll the contents of the file.

In my case, there is already a commented out section related to password authentication, so that’s where I will update the entries. Here is the relevant section before my changes:

Server SSH Config File Before Changes

Server SSH Config File Before Changes

The PasswordAuthentication setting was already there, so I uncommented it by removing the # at the beginning and changed it to no.

I also added PubkeyAuthentication yes and KbdInteractiveAuthentication no to the file.

Here is a screenshot of the config file after the changes:

Server SSH Config File After Changes

Server SSH Config File After Changes

Press Ctrl + X, answer Y to the “Save modified buffer?” prompt, and press Enter on the “Write to File: /etc/ssh/sshd_config” prompt to save the changes. You should be back at the command prompt.

At this point, it doesn’t hurt to either cat the config file to the terminal or re-open the file again in nano to confirm the changes were actually saved.

To validate the config file, type sudo sshd -t. If the config file is valid, then there should be no output from the command. If the command reports an issue, correct it before continuing.

Restart the ssh service by typing sudo systemctl restart ssh. If there are no issues, there will be no output from this command either.

With changes saved to the config file and the service restarted, I can try connecting with my ssh client again. The connection is successful and I am not prompted for a password.

SSH Connection After Disabling Password Authentication on the Server

SSH Connection After Disabling Password Authentication on the Server

To test that password-based connections are now being blocked, run the command ssh -p 2222 -o "PreferredAuthentications=password" -o "PubkeyAuthentication=no" username@127.0.0.1. Again, make sure to change username to your server user name.

Password Authentication Denied

Password Authentication Denied

Next Steps

Now that you (hopefully) have your SSH connection working, here are a few things that you can try experimenting with.

  • Create a non-admin user to use for SSH connections.
  • Set up public key authentication for the new user.

Conclusion

This completes the instructions for installing and configuring SSH on an Ubuntu Server VM running in Oracle VirtualBox, and connecting to the server VM with an SSH client running on Windows 11.


The postings on this site are my own and do not necessarily reflect the views of my employer.

The content on this blog is for informational and educational purposes only and represents my personal opinions and experience. While I strive to provide accurate and up-to-date information, I make no guarantees regarding the completeness, reliability, or accuracy of the information provided.

By using this website, you acknowledge that any actions you take based on the information provided here are at your own risk. I am not liable for any losses, damages, or issues arising from the use or misuse of the content on this blog.

Please consult a qualified professional or conduct your own research before implementing any solutions or advice mentioned here.