Skip to content
Go back

Connect to Your Linux VM via SSH- Guide to Secure Remote Access and Key-Based Authentication

Published:

learn how to connect to your Linux Virtual Machine (VM) terminal from an external via SSH.

output

🧩 Understand What SSH Is

SSH (Secure SHell) is a protocol used to securely log into another computer over a network. It encrypts all communication between two systems.

So if you’re connecting to your VM via SSH, you’re basically logging into it remotely.


🧪 Check If SSH Server Is Installed on Linux VM

Most Linux distributions do not install the SSH server by default for security reasons.

To check if openssh-server is installed:

sudo systemctl status ssh

If you get something like “active (running)”, then SSH is already installed and running.

If not:

🟢 Install OpenSSH Server

For Ubuntu/Debian-based distros:

sudo apt update
sudo apt install ssh

After installing, start the SSH service:

sudo systemctl start ssh

Enable it to start automatically on boot:

sudo systemctl enable ssh

Network Configuration (Host Machine and Router)

This is the most critical part for external access and depends heavily on your setup.

Option 1: Bridged Networking (Simplest for external access on the same LAN)

Option 2: NAT (Network Address Translation) with Port Forwarding (Most common for a VM behind a host)

Option 3: Port Forwarding on Your Main Router (For truly external access over the internet)

Option 4: Host-Only Networking (Only for Host-to-VM, not external)

Option 5: Cloud VMs (e.g., AWS EC2, Google Cloud, Azure)

Important Security Considerations:

Configure VM Firewall (UFW/firewalld):

Ensure the VM’s internal firewall allows incoming SSH connections (default port 22).

sudo apt install ufw
sudo ufw allow ssh  # This allows port 22/tcp OR sudo ufw allow 22/tcp 
sudo ufw enable         # If not already enabled
sudo ufw status verbose # helps to provide a richer and more detailed description of their operation.

📍 Find the IP Address of the Linux VM

You need to know the IP address of your Linux VM so you can connect to it.

Run this command in the VM terminal:

ip a
# OR
ifconfig # if net-tools is installed

Look for something like:

inet 192.168.x.x

This is your local network IP address. Note it down.

Example output:

2: enp0s3: <BROADCAST,MULTICAST,UP> mtu 1500...
    inet 192.168.1.100/24 brd 192.168.1.255 scope global dynamic enp0s3

In this case, the IP is: 192.168.1.100


🖥️ Connect to the VM Using SSH From Host Machine

Now open a terminal on your host machine (your main OS, not the VM), and type:

ssh username@vm-ip-address

Replace:

Example:

ssh [email protected] -p 22

Then press Enter and type the password when prompted.

If successful, you’ll now be inside the Linux VM’s terminal — but connected via SSH!


Set up SSH key-based authentication.

This method is more secure than using passwords and allows you to log in without typing a password every time.


🔐 What Is SSH Key-Based Authentication?

SSH keys are a pair of cryptographic keys:

When you connect via SSH, the system checks if your private key matches the public key on the server.


🧰 Step-by-Step Guide to Set Up SSH Keys

We’ll do this from your host machine (your main computer) and then copy the public key to your Linux VM.


✅ Step 1: Generate an SSH Key Pair on Your Host Machine

Open a terminal on your host machine (not the VM).

Type:

ssh-keygen -t rsa -b 4096

This command generates a strong RSA key pair with 4096 bits.

You’ll see something like:

Generating public/private rsa key pair.
Enter file in which to save the key (/home/youruser/.ssh/id_rsa):

Just press Enter to accept the default location (~/.ssh/id_rsa).

Then it asks:

Enter passphrase (empty for no passphrase):

You can leave it empty or set a passphrase for extra security.

✅ Done! Your keys are now created:


✅ Step 2: Copy the Public Key to Your Linux VM

There are two common ways to do this:

Run this on your host machine:

sudo ssh-copy-id -i ~/.ssh/id_rsa.pub username@vm-ip-address

Replace:

It will ask for your password, then copy the public key to the VM.

If successful, you’ll see:

Number of key(s) added: 1

🔹 Option B: Manually Copy the Key (if ssh-copy-id not available)

On your host machine, view your public key:

cat ~/.ssh/id_rsa.pub

Copy the entire line (it starts with ssh-rsa AAAAB3NzaC1yc2...).

Now, log in to your Linux VM normally via SSH:

ssh username@vm-ip-address

Once inside, create the .ssh directory and edit the authorized_keys file:

mkdir -p ~/.ssh
chmod 700 ~/.ssh
nano ~/.ssh/authorized_keys

Paste the public key into the file and save it (Ctrl+O, Enter, Ctrl+X in nano).

Set correct permissions:

chmod 600 ~/.ssh/authorized_keys

✅ Step 3: Test SSH Without Password

Now try logging in again:

ssh username@vm-ip-address

If everything went well, you should be logged in without being asked for a password!


🔒 Optional: Disable Password Login (for extra security)

Once you’ve confirmed key-based login works, you can disable password-based SSH logins.

⚠️ Only do this if you’re sure your SSH key setup works.

Edit the SSH config file on your Linux VM:

sudo nano /etc/ssh/sshd_config

Find these lines and change them to:

PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM no

Save and exit.

Then restart the SSH service:

sudo systemctl restart ssh

Now only users with the correct private key can log in.


🧠 Summary of Important Terms

TermMeaning
~/.ssh/id_rsaYour private SSH key (never share this!)
~/.ssh/id_rsa.pubYour public SSH key (can be shared safely)
~/.ssh/authorized_keysFile on the server that contains trusted public keys
ssh-keygenTool used to generate SSH key pairs
ssh-copy-idTool to copy your public key to a remote server

💡 Tips


Learn More:


Suggest Changes

Previous Post
First Database Project - Setting Up & Managing MySQL/MariaDB on Linux, Database Dump, Backup and Restore
Next Post
🐧💻 Linux Directories & Config Files Demystified - What Every DevOps Should Know