If you’re running an Ubuntu server, you know that security is job one. Password-based logins for SSH are convenient, but they’re a major vulnerability. A much more secure approach is to use SSH key-based authentication.
This guide will walk you through setting it up, step by step, for Linux, macOS, and Windows clients. We’ll also cover some advanced topics like managing multiple keys and using PuTTY.
Secure Shell (SSH) is the standard way to connect to a remote Linux server’s command line. It provides an encrypted
Key Uses of SSH:
scp
and sftp
, which are built on top of SSH).Read: How to Install and Secure OpenSSH on Ubuntu 24.04: Complete Step-by-Step Guide
Traditional SSH access relies on passwords. You type your username and password, and the server verifies them. The problem is that passwords can be:
Key-based authentication uses cryptography to eliminate these risks. It relies on two keys:
When you connect, a cryptographic “handshake” happens. The server uses the public key to verify that you possess the corresponding private key – without the private key ever being transmitted over the network.
Read: Generating Secure Passwords on Linux: PWGen, Custom Scripts, and Other Tools
Several key types are available for SSH authentication:
For most users, Ed25519 is the best balance of security and convenience, with RSA as a fallback for older systems.
Here’s the overview of the process:
Read: Securing Your Data: A Practical Guide to Encryption, Integrity, and Signatures in Linux
Before you can connect to your Ubuntu server with SSH, you need to make sure the SSH server software (the sshd
daemon) is installed and running.
systemctl status sshd
If it shows “active (running)”, you’re good to go.
sshd
(if needed): Use the apt
package manager: sudo apt update # Make sure your package lists are up-to-date
sudo apt install openssh-server
sshd
: sudo systemctl start sshd
sudo systemctl enable sshd
systemctl start
starts the service immediately. systemctl enable
makes it start automatically when the system boots.
These steps are performed on your client machine (Linux or macOS).
ssh-keygen
command: ssh-keygen -t ed25519 -a 100
For older systems that don’t support Ed25519:
ssh-keygen -t rsa -b 3072
~/.ssh/id_ed25519
or ~/.ssh/id_rsa
for the private key, and corresponding .pub
file for the public key).ssh-copy-id
command: ssh-copy-id @
with your username on the remote Ubuntu server.
with the server’s hostname or IP address.ssh-copy-id
does the following:
~/.ssh
directory on the server (if it doesn’t exist).~/.ssh/authorized_keys
file on the server.Alternative method (if ssh-copy-id isn’t available):
# View your public key
cat ~/.ssh/id_ed25519.pub
# Copy the output to clipboard, then on the server:
mkdir -p ~/.ssh
echo "paste-your-public-key-here" >> ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
ssh @
You should be logged in without being asked for your account password (you will be asked for your key passphrase, if you set one).
Read: How to Secure Your Linux System with 10 Proven Firewalls
If you connect to multiple servers, you might have multiple key pairs.
-i
: ssh -i ~/.ssh/my_other_private_key @
~/.ssh/config
): This is the recommended approach for managing multiple keys and hosts. Create this file if it doesn’t exist. Here’s an example: Host work
HostName server1.example.com
User myuser
IdentityFile ~/.ssh/id_rsa_work
Host home
HostName 192.168.1.100
IdentityFile ~/.ssh/id_rsa_home
Now you can connect with ssh work
or ssh home
, and the correct keys and usernames will be used automatically.
Modern versions of Windows 10 include a built-in OpenSSH client, so you can often use the same commands as on Linux/macOS.
ssh-keygen -t ed25519 -a 100
Follow the prompts similar to Linux/macOS.
Get-Content $env:USERPROFILE.sshid_ed25519.pub
Method 1: Manually append to authorized_keys
mkdir -p ~/.sshecho "paste-your-public-key-here" >> ~/.ssh/authorized_keyschmod 700 ~/.sshchmod 600 ~/.ssh/authorized_keys
Method 2: Using scp (if installed)
scp $env:USERPROFILE.sshid_ed25519.pub username@server:~/temp.pub
Then on the server:
mkdir -p ~/.ssh
cat ~/temp.pub >> ~/.ssh/authorized_keys
rm ~/temp.pub
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Read: An introduction to Windows PowerShell for beginners
PuTTY is a popular, free SSH client for Windows. It uses its own key format (.ppk
), so you’ll need PuTTYgen.
mykey.ppk
).id_rsa
).mkdir -p ~/.sshnano ~/.ssh/authorized_keys
chmod 700 ~/.sshchmod 600 ~/.ssh/authorized_keys
Method 2: Using PSCP (comes with PuTTY)
pscp mykey.pub username@server:/tmp/
mkdir -p ~/.sshcat /tmp/mykey.pub >> ~/.ssh/authorized_keysrm /tmp/mykey.pubchmod 700 ~/.sshchmod 600 ~/.ssh/authorized_keys
.ppk
private key file.If you’re using Google Cloud Platform (GCP), you can add your public key to your project’s metadata. This makes the key available to all your Compute Engine instances.
ssh-rsa
or ssh-ed25519
part and the comment at the end). The username will be extracted and automatically associated.As a security best practice, consider rotating your SSH keys periodically (every 6-12 months):
This process helps mitigate the risk of compromised keys and follows the principle of regular credential rotation.
SSH key-based authentication is essential for securing your Ubuntu servers. This guide has covered:
ssh-keygen
and ssh-copy-id
on Linux/macOS.Disabling Password Authentication (Final, Crucial Step):
Once you’ve confirmed that key-based authentication is working, disable password authentication on the server. This forces all connections to use keys.
sudo nano /etc/ssh/sshd_config
Note: This file is sshd_config
(server config) and not ssh_config
(client config).
PasswordAuthentication no
ChallengeResponseAuthentication no
The second setting disables keyboard-interactive authentication, which is another form of password authentication.
sudo systemctl restart sshd
sudo sshd -T | grep -E 'password|challenge'
You should see both settings showing “no”.
WARNING: Before you do this, double-check that your key-based authentication is working. If you make a mistake, you could lock yourself out of your server! It’s recommended to keep a second SSH session open when making these changes.
If you’re having issues with SSH key authentication:
sudo journalctl -u sshd
This will show error messages that can help identify the problem.
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
ssh -vv username@server
This provides detailed information about the connection process.
id_rsa
and id_rsa.pub
?A: id_rsa
is your private key. Keep it secret! id_rsa.pub
is your public key. This is the one you copy to servers.~/.ssh/authorized_keys
on the server.~/.ssh
or authorized_keys
are wrong.scp
(Secure Copy) or a physically secure USB drive. Never email your private key.sshd_config
and ssh_config
?A: sshd_config
configures the SSH server. ssh_config
configures the SSH client.~/.ssh
directory and authorized_keys
file?A: Use the chmod
command: ~/.ssh
directory (700 allows read/write/execute for the owner only): chmod 700 ~/.ssh
authorized_keys
file (600 allows read/write for the owner only): chmod 600 ~/.ssh/authorized_keys
ssh-agent
) is a more advanced topic. It allows you to load your private key (and enter your passphrase) once, and then the agent handles authentication for subsequent SSH connections. This avoids having to type your passphrase repeatedly. eval "$(ssh-agent -s)" # Start the agent and set environment variables
ssh-add ~/.ssh/id_rsa # Add your private key to the agent
The post How to configure SSH-key based authentication on Ubuntu 20.10 appeared first on net2.
June 25th, 2025 – Canonical, the company behind Ubuntu, and Pure Storage, the IT pioneer…
Co-authored with Julie Muzina A year ago, during our Madrid Engineering Sprint, we challenged ourselves…
Welcome to the Ubuntu Weekly Newsletter, Issue 897 for the week of June 15 –…
This is the first optimized Ubuntu Core image available on MediaTek’s Genio 350, 510, 700,…
Data centers are expensive: automation is the solution Today, managing a data center requires striking…
Our first Ubuntu IoT Day in Southeast Asia – and our first ever event in…