Ubuntu

How to Set Up SSH Key Authentication on Ubuntu 24.04

If you're tired of typing your password every time you connect to your server β€” or you're aiming for stronger security β€” using SSH keys is the way to go. This guide will walk you through generating an SSH key and setting it up on an Ubuntu 24.04 server.

By Hamad
2 min read
174 views
How to Set Up SSH Key Authentication on Ubuntu 24.04

πŸ” How to Set Up SSH Key Authentication on Ubuntu 24.04

If you're tired of typing your password every time you connect to your server β€” or you're aiming for stronger security β€” using SSH keys is the way to go. This guide will walk you through generating an SSH key and setting it up on an Ubuntu 24.04 server.

βœ… Step 1: Generate SSH Key Pair (on your local machine)

Open your terminal and run:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
 -t rsa: specifies RSA encryption.
 
 -b 4096: sets the key length to 4096 bits.
 
 -C: adds a comment (usually your email).

When prompted:

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

Enter a passphrase for extra protection (optional).

This will generate two files:

~/.ssh/id_rsa β†’ your private key (keep it safe and never share it).
 
~/.ssh/id_rsa.pub β†’ your public key (you'll upload this to your server).

βœ… Step 2: Copy the Public Key to Your Ubuntu Server

Connect to your server (temporarily using a password):

ssh username@your-server-ip Now, on the server:

Create the .ssh directory (if it doesn't exist):

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

Add your public key to the authorized_keys file:

You can paste it manually:

nano ~/.ssh/authorized_keys

Paste the contents of id_rsa.pub, save and exit (Ctrl + O, Enter, then Ctrl + X).

Then set proper permissions:

chmod 600 ~/.ssh/authorized_keys

βœ… Step 3: (Optional) Disable Password Authentication

To enhance security, you can disable password logins:

sudo nano /etc/ssh/sshd_config

Ensure these lines are uncommented:

PubkeyAuthentication yes
PasswordAuthentication no

Then restart the SSH service:

sudo systemctl restart ssh

βœ… Step 4: Log In with SSH Key

From your local machine, connect again:

ssh username@your-server-ip

You should be logged in without being asked for a password.

πŸŽ‰ Done! You’ve successfully set up SSH key authentication on Ubuntu 24.04. It’s faster, safer, and one step closer to being a true Linux pro πŸ’»πŸ”

H

Hamad

Published on July 25, 2025

Related Posts

Zsh Cheat Sheet [Free PDF Download]
Ubuntu

Zsh Cheat Sheet [Free PDF Download]

Zsh is a powerful command-line shell for operating systems based on Unix that is renowned for its customization options and wide range of features. This cheat sheet is a must-read if you’re new to Zsh or want to brush up on your knowledge. To help you work in the terminal more effectively and efficiently, I’ll go over some of the most useful Zsh commands and shortcuts in this article.