This guide shows you how to generate and install an SSH key pair on Ubuntu so you can log into remote servers without a password. You will create a private and public key, copy the public key to your server, and verify that passwordless login works. The process takes about five minutes and works on any modern Ubuntu version.
Why Use SSH Keys Instead of Passwords
SSH keys are a pair of cryptographic files that replace the need to type a password every time you connect. The private key stays on your local machine, and the public key is placed on any server you want to access. When you connect, the server checks that your private key matches the public key it holds. This is far more secure than a password because the private key is never sent over the network, and it is practically impossible to guess. Passwords can be brute-forced, especially if your server is exposed to the internet. Keys are also more convenient once set up—you can script transfers, use version control over SSH, and manage multiple servers without typing credentials each time.
Prerequisites
- An Ubuntu machine (your local computer) with a terminal.
- A remote server running Linux with SSH enabled (any distribution works).
- Your username and IP address or hostname for the remote server.
- Optional but recommended: a passphrase for your private key, which adds an extra layer of protection if someone steals the file.
You do not need root access on your local machine to generate keys, but you will need the ability to log in to the remote server with a password initially.
Step-by-Step: Generate the Key Pair
- Open a terminal on your Ubuntu machine.
- Run
ssh-keygen -t ed25519 -C "your_email@example.com". The-tflag selects the algorithm (ed25519 is modern and fast), and-Cadds a comment so you can identify the key later. - When prompted, press Enter to accept the default file location (
~/.ssh/id_ed25519). If you already have a key there, choose a different name like~/.ssh/mykey. - Enter a passphrase when asked. You can leave it empty, but a passphrase protects your private key if it is ever copied. You will need to enter it each time you use the key unless you add it to an SSH agent.
- After the key is generated, you will see a randomart image and the key fingerprint. This confirms the key was created successfully.
If you need to support older servers that do not accept ed25519, use ssh-keygen -t rsa -b 4096 -C "your_email@example.com" instead. RSA with 4096 bits is still widely compatible, though ed25519 is preferred for new setups.
Copy the Public Key to Your Server
Now you need to place the public key (the file ending in .pub) into the ~/.ssh/authorized_keys file on the remote server. The easiest way is to use the ssh-copy-id utility, which is included with OpenSSH on Ubuntu.
- Run
ssh-copy-id username@server_ip. Replaceusernamewith your remote username andserver_ipwith the actual IP or hostname. - You will be prompted for your remote password. Enter it once.
- The utility appends your public key to the server’s
authorized_keysfile and closes the connection.
If ssh-copy-id is not available, you can do it manually. First, display your public key with cat ~/.ssh/id_ed25519.pub. Copy the entire line. Then connect to the server with ssh username@server_ip, create the .ssh directory if it does not exist (mkdir -p ~/.ssh), and append the key with echo "paste_your_key_here" >> ~/.ssh/authorized_keys. Make sure the permissions are correct: chmod 700 ~/.ssh and chmod 600 ~/.ssh/authorized_keys.
Test the Login and Disable Password Authentication
From your local machine, run ssh username@server_ip. If everything is set up correctly, you will be logged in without a password prompt (or you will be asked for your key passphrase if you set one). If it works, you can optionally disable password authentication on the server for extra security. Edit the SSH daemon configuration file with sudo nano /etc/ssh/sshd_config and set PasswordAuthentication no. Then restart the service with sudo systemctl restart sshd. Be careful: only do this after you have confirmed your key works, otherwise you could lock yourself out.
Managing Multiple Keys and the SSH Agent
If you have multiple servers or use different keys for different services, you can create a config file at ~/.ssh/config to simplify connections. For example:
Host myserver
HostName 192.168.1.100
User ubuntu
IdentityFile ~/.ssh/mykey
Then you can just type ssh myserver. If you set a passphrase on your key, you can add it to the SSH agent so you do not have to type it repeatedly. Run eval "$(ssh-agent -s)" and then ssh-add ~/.ssh/id_ed25519. The agent will hold your key in memory for the duration of your session.
Common Problems and Fixes
- Permission denied (publickey): Check that the
authorized_keysfile on the server has the correct permissions (600) and that the.sshdirectory is 700. Also verify you are using the correct key file withssh -i ~/.ssh/id_ed25519 username@server_ip. - Key not being used: If you have multiple keys, SSH may try the default key first. Use the
-iflag or setIdentityFilein your config file. - Passphrase forgotten: There is no way to recover a passphrase. You will need to generate a new key pair and replace the public key on the server.
- Server still asks for password: Make sure you copied the public key, not the private key. The public key ends in
.pub. - Connection timed out: Check that the server’s firewall allows SSH (port 22 by default) and that the SSH service is running.
Security Best Practices
- Never share your private key. It is like a password—anyone who has it can access your servers.
- Use a passphrase on your private key. If someone steals the file, they still cannot use it without the passphrase.
- Regularly rotate your keys. If you suspect a key has been compromised, remove it from the server’s
authorized_keysand generate a new one. - Disable root login over SSH if you do not need it. Set
PermitRootLogin noinsshd_configand use a regular user with sudo privileges. - Consider using a hardware security key or a YubiKey for storing your SSH keys, though that is more advanced.
Frequently Asked Questions
What is the difference between ed25519 and RSA keys?
Ed25519 is a modern elliptic-curve algorithm that is faster, more secure, and produces shorter keys. RSA is older and more widely supported, but requires longer key lengths (2048 or 4096 bits) for equivalent security. For new setups, ed25519 is recommended unless you need compatibility with very old systems.
Can I use the same SSH key for multiple servers?
Yes, you can copy the same public key to as many servers as you like. Each server will have a copy of your public key, and your private key will unlock them all. This is convenient but means that if your private key is compromised, all servers are at risk. For high-security environments, use separate keys per server.
What if I lose my private key?
You cannot recover it. You will need to generate a new key pair and replace the public key on all servers that had the old key. This is why it is wise to keep a backup of your private key in a secure location, like an encrypted USB drive.
How do I change the passphrase on an existing key?
Run ssh-keygen -p -f ~/.ssh/id_ed25519. It will prompt you for the current passphrase and then let you set a new one. This does not change the key itself, only the encryption of the private key file.
Is it safe to disable password authentication?
Yes, if you have a working key and you have tested it. Disabling password authentication eliminates the risk of brute-force password attacks. Just make sure you have a backup method (like console access) in case you lose your key.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Hiberfil.sys: What It Is & How to Delete It
- How to Make a Mask on Snapchat: Step-by-Step
- How to Factory Reset iPad If You Forgot Password
- Ubuntu in Windows Store: Which Editions Work?
- Secure Guest Account on Windows 11: Step-by-Step
- Stop Outlook From Changing Hyperlink Font