π 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 π»π