DEV Community

Cover image for Easy way to SSH into VirtualBox machine | Any OS
Tharun Shiv
Tharun Shiv

Posted on • Updated on

Easy way to SSH into VirtualBox machine | Any OS

Hey there!
This is Tharun here. Most of us would have struggled to SSH into a VirtualBox Machine. In this article, I introduce to you what Virtual Box is and how you can SSH into it from your local machine.

What is VirtualBox?

The VirtualBox is a software that can be used to run multiple operating systems on your own operating system and effortlessly switch between them, create snapshots, delete them, restore them on the fly.

We all have wondered if we had an easier way to access our VirtualBox Machines. In this tutorial, we will look at how you can SSH into your VirtualBox machine. This will prevent you to have to open your VirtualBox GUI to do everything. (duh!)

Did you know you could do this??

How to SSH into it?

Open VirtualBox

VirtualBox

Click on Settings and go to Network

You can either click on the Settings icon, or you can right-click on a Virtual Machine and select the Settings option

network-virtualbox

Choose an Adapter

Based on which Adapter is free on your virtual machine. Enable the Network Adapter. Select the NAT network.

NAT-virtualbox

Select the Port Forwarding

Open the Advanced options and click on Port Forwarding. Leave other settings as default.

Port-virtualbox

Add a new entry

Click on the + icon on the top-right corner to add a new rule.

add-virtualbox

The rule

Name: ssh
Protocol: TCP
Host Port: 3022
Guest Port: 22
Leave others empty

What we're telling here is, basically forward all connections to the port 3022 on host machine to the port 22 on the guest machine. If you didn't know, SSH uses port 2

entry-virtualbox

Done! Now let's connect

Open your terminal and type

~$ ssh -p 3022 <username>@127.0.0.1
Enter fullscreen mode Exit fullscreen mode

Here,

~$ ssh -p 3022 ubuntu-1@127.0.0.1
Enter fullscreen mode Exit fullscreen mode

command-virtualbox

Enter the Password of your that you are logging into and we're done

Done-virtualbox

Hope this article helped. Give this article a like, unicorn, and save it for later. Share it with your friends and let them know this useful trick.

Next article on how you can run a VirtualBox without running it's GUI. So combining SSH access and no-GUI, VirtualBox made convenient.

My Blog: https://www.tharunshiv.com

Written by,

Thank you for reading, This is Tharun Shiv a.k.a Developer Tharun

Tharun Shiv

Top comments (14)

Collapse
 
redcricket profile image
redcricket • Edited

% ssh -p 3022 127.0.0.1
kex_exchange_identification: Connection closed by remote host
Connection closed by 127.0.0.1 port 3022

trying to ssh to ubuntu vm

Ah. I didn't install openssh-server on the ubuntu vm like so ...
sudo apt-get install openssh-server

Collapse
 
longtth profile image
Long Nguyễn Xuân

thanks for comment this.

Collapse
 
justman10000 profile image
Justman10000

Yes, it's nasty! You want to use software and forget to install it!

Collapse
 
nandum21 profile image
nandum21

try:
ssh -p 3022 username@127.0.0.1
can change port to 7777 like that .... as ur wish

Collapse
 
balukrish111 profile image
balu

Thanks a lot man :)

Collapse
 
developertharun profile image
Tharun Shiv

Glad it helped. :)

Collapse
 
igormarques profile image
igor-marques

had trouble using

$ ssh -p 3022 imarques@127.0.0.1
Connecting to 127.0.0.1:22...
Could not connect to '127.0.0.1' (port 22): Connection failed.

changed to

$ ssh imarques@127.0.0.1:3022
Connecting to 127.0.0.1:3022...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Welcome to Ubuntu 22.04.3 LTS (GNU/Linux 5.15.0-84-generic x86_64)

many thanks!

Collapse
 
vi_thali profile image
Vitaly Natalievich

Thanx a lot!

Collapse
 
developertharun profile image
Tharun Shiv

Awesome!

Collapse
 
justman10000 profile image
Justman10000

Spent hours looking for this, thanks to this post it finally works...

Collapse
 
nandum21 profile image
nandum21

This Works ... Thanks :)

Collapse
 
kaizen profile image
Thanh Nguyen

awesome bro. it saved me :D

Collapse
 
phylloxera profile image
Phylloxera

Operation timed out