Introduction
Learning how to ssh with private key is a fundamental skill for anyone who works with remote servers, cloud instances, or version‑controlled code repositories. By using a private key instead of a password, you gain stronger security, eliminate the risk of brute‑force attacks, and streamline the login process with a single command. This article walks you through every step required to set up and use SSH keys, explains the underlying technology, and answers the most common questions you may encounter along the way.
Prerequisites
Before you begin, make sure you have the following:
- A local machine (Linux, macOS, or Windows with PowerShell or the Windows Subsystem for Linux).
- SSH client installed. On most Linux distributions and macOS, the client is provided by OpenSSH by default. Windows users can enable the OpenSSH client via Settings → Apps → Optional Features.
- Access to a remote server where you have permission to add your public key to the
~/.ssh/authorized_keysfile.
If any of these items are missing, the process cannot proceed smoothly, so verify them first.
Generating a Private Key
Choose the Key Type
Modern SSH implementations support several algorithms, but the two most recommended are:
- Ed25519 – offers fast performance and strong security with a short key size.
- RSA – widely compatible, especially with older servers; a 4096‑bit RSA key is a good balance between security and compatibility.
Create the Key Pair
Open a terminal and run the following command for an Ed25519 key:
ssh-keygen -t ed25519 -C "your_email@example.com"
For an RSA key, use:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
What the options mean
-tspecifies the key type.-Cadds a comment (usually your email) to identify the key later.-b 4096(RSA only) sets the key length.
You will be prompted to choose a file in which to save the key and optionally set a passphrase Not complicated — just consistent. Took long enough..
- File name – the default (
~/.ssh/id_ed25519or~/.ssh/id_rsa) is fine for most users. - Passphrase – highly recommended for added protection; it encrypts the private key at rest.
After you confirm, the command generates two files:
- Private key (
id_ed25519orid_rsa) – keep this file secret. - Public key (
id_ed25519.puborid_rsa.pub) – this is what you share with the server.
Protect the Private Key
Set restrictive permissions so that only your user can read the private key:
chmod 600 ~/.ssh/id_ed25519
If the permissions are too open, SSH will refuse to use the key, and you’ll see a warning in the client logs.
Configuring the SSH Client
Add the Public Key to the Remote Server
Copy the contents of the public key file to the server:
cat ~/.ssh/id_ed25519.pub | ssh user@remote_server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
Alternatively, on many Linux systems you can use the ssh-copy-id utility:
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@remote_server
This command appends the public key to ~/.ssh/authorized_keys and ensures correct directory permissions.
Optional: Create an SSH Config Entry
If you connect to the same host frequently, simplify the command with a config file (~/.ssh/config):
Host myserver
HostName 203.0.113.45
User your_user
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
Now you can simply run ssh myserver instead of typing the full command.
Connecting via SSH
Basic Connection
Use the following command to initiate a connection:
ssh -i ~/.ssh/id_ed25519 user@remote_server
If you set a passphrase, you’ll be prompted to enter it now.
Verify the Connection
After logging in, run:
ssh -v user@remote_server
The -v flag enables verbose output, allowing you to see that the client is using your private key (Offering public key... followed by Authentication succeeded).
Using an SSH Agent
To avoid entering the passphrase repeatedly, start an SSH agent and add your key:
eval "$(ssh-agent -s)" # start the agent
ssh-add ~/.ssh/id_ed25519 # add the key (you’ll enter the passphrase once)
The agent stores the decrypted key in memory, enabling password‑less logins for the session.
Managing Keys Securely
Rotate Keys Periodically
If you suspect a key compromise, generate a new pair and replace the old public key on the server Worth keeping that in mind..
Remove Unused Keys
Regularly audit ~/.ssh/authorized_keys on each server and delete entries that no longer correspond to active keys.
Store the Private Key in an Encrypted Container
On high‑security environments, consider using tools like gpg or hardware security modules (HSMs) to protect the private key file.
Common Issues and Troubleshooting
| Symptom | Likely Cause | Solution |
|---|---|---|
Permission denied (publickey) |
Public key not in authorized_keys or wrong file permissions |
Verify the key is correctly appended, run chmod 700 ~/.That said, ssh && chmod 600 ~/. ssh/authorized_keys |
ssh: Could not resolve hostname |
Wrong hostname or DNS issue | Double‑check the HostName entry in the config file or the command |
Load key "/home/user/.ssh/id_ed25519": bad permissions |
Private key file is world‑readable | Run `chmod 600 ~/. |