Friday, 9 May 2025

How to Set Up SSH in Ubuntu VM

How to Set Up SSH in Ubuntu VM

SSH (Secure Shell) is a powerful tool that allows you to remotely access and manage your Ubuntu system. This guide explains how to install and configure SSH, and also how to fix common connection issues—especially when accessing a guest VM (like Ubuntu in VirtualBox) from a host machine.

Step 1: Install SSH Server on Ubuntu

sudo apt update
sudo apt install openssh-server

Check if it's running:

sudo systemctl status ssh

If it's not active, start it:

sudo systemctl start ssh

Enable it at boot:

sudo systemctl enable ssh

Step 2: Find Ubuntu’s IP Address

hostname -I

or

ip a

Step 3: Connect via SSH

From another machine on the same network:

ssh username@your_ubuntu_ip

Step 4: Configure SSH Settings (Optional)

Edit the config file:

sudo nano /etc/ssh/sshd_config

Then restart SSH:

sudo systemctl restart ssh

Step 5: Set Up SSH Key Authentication (Optional)

On the client machine:

ssh-keygen
ssh-copy-id username@your_ubuntu_ip

Troubleshooting: “Connection Refused” in VirtualBox

If you're trying to SSH from host to guest and get “connection refused,” check the following:

1. Is SSH Running?

sudo systemctl status ssh

2. Check VirtualBox Network Settings

  • Bridged Adapter: Recommended for easiest access from host to guest.
  • NAT: Needs port forwarding to work with SSH.

3. Port Forwarding for NAT

Go to Settings → Network → Advanced → Port Forwarding and add:

FieldValue
ProtocolTCP
Host IP127.0.0.1
Host Port2222
Guest IP(leave blank or use 10.0.2.15)
Guest Port22

Then connect using:

ssh username@127.0.0.1 -p 2222

4. Firewall Blocking SSH

sudo ufw status

If active:

sudo ufw allow ssh
sudo ufw reload

Quick Checklist

StepCommand/Setting
Install SSHsudo apt install openssh-server
Start SSHsudo systemctl start ssh
Network TypeUse Bridged or set up NAT Port Forwarding
Firewallsudo ufw allow ssh
Connectssh user@ip or ssh user@127.0.0.1 -p port

Conclusion

Setting up SSH in Ubuntu is straightforward, but getting it to work in a virtual environment requires special attention to networking. Use bridged mode for simplicity, or configure port forwarding in NAT mode. Ensure your firewall and SSH service are both active, and you’ll have a smooth remote access setup in no time.

No comments:

Post a Comment