SSH Key Management: Stop Using Passwords

Learn to generate SSH key pairs, copy public keys to servers, configure SSH config, and harden your server with key-only authentication.

SSH Key Management: Stop Using Passwords
📧

Get weekly IT guides

Join 5,000+ IT professionals

Subscribe Free

Every time you type a password to SSH into a server, you’re doing something a hacker could intercept, brute-force, or phish from you. SSH keys make this attack surface disappear entirely.

Introduction

SSH keys use public-key cryptography — a method where you hold a private secret while distributing a public “lock” that only your key can open. Once set up, you log in with zero typing. No password to forget. No password to steal. No brute-force possible.

What You'll Learn
  • How SSH keys work (public vs private key)
  • How to generate a key pair with ssh-keygen
  • How to copy your public key to a server
  • How to use ~/.ssh/config for shortcuts
  • How to disable password login entirely (the secure standard)
Mental Model: The Padlock System

Imagine you want access to 10 different offices:

  • You give each office a padlock that only you can open (public key).
  • You keep your unique key (private key) in your pocket.
  • Any office can leave the padlock on its door — it’s useless without your key.
  • If someone steals the padlock, they still can’t get in — they need your key.

That’s exactly how SSH key auth works.

Public vs Private Keys

Diagram showing private key vs public key created by ssh-keygen
ssh-keygen creates two files: a private key (stays on your machine) and a public key (goes to every server).

The rule is simple:

  • Private key (id_ed25519) — stays on your machine. Never share it. Ever.
  • Public key (id_ed25519.pub) — safe to share. Copy it to every server you want to access.

Step 1: Generate Your Key Pair

Modern algorithm recommendation: Ed25519 (faster and more secure than RSA).

ssh-keygen -t ed25519 -C "yourname@yourcomputer"

Generate a new Ed25519 key pair with a comment label

beginner

You’ll be asked for a passphrase — this encrypts your private key file. Use a strong one.

Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/shekhar/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase): ****
Your identification has been saved in /home/shekhar/.ssh/id_ed25519
Your public key has been saved in /home/shekhar/.ssh/id_ed25519.pub

View your public key:

cat ~/.ssh/id_ed25519.pub

Print your public key — this is what you copy to servers

beginner

Step 2: Copy Your Public Key to a Server

The Easy Way: ssh-copy-id

ssh-copy-id user@server-ip

Automatically append your public key to the server's authorized_keys

beginner

This command:

  1. Reads ~/.ssh/id_ed25519.pub
  2. SSHs into the server with your password (one last time)
  3. Appends your public key to ~/.ssh/authorized_keys on the server

The Manual Way (if ssh-copy-id isn’t available)

cat ~/.ssh/id_ed25519.pub | ssh user@server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

Manually append your public key to the server

beginner

Test the connection:

ssh user@server-ip

Should now log in without asking for a password

beginner
Permissions Matter

SSH is strict about file permissions. If it fails, run these on the server:

chmod 700 ~/.ssh

chmod 600 ~/.ssh/authorized_keys


Step 3: Create an SSH Config File for Shortcuts

Typing ssh -i ~/.ssh/id_ed25519 ubuntu@54.23.88.101 -p 2222 every time is painful. Use ~/.ssh/config:

Host myserver
    HostName 54.23.88.101
    User ubuntu
    Port 2222
    IdentityFile ~/.ssh/id_ed25519

Host staging
    HostName 10.0.1.50
    User deploy
    IdentityFile ~/.ssh/id_ed25519_staging

Now you can just type:

ssh myserver

Uses all settings from ~/.ssh/config automatically

beginner
ssh staging

Connects to the staging server with its specific key

beginner

Step 4: Disable Password Login (Harden the Server)

Once you’ve confirmed key-based login works, disable passwords entirely. This stops brute-force attacks completely.

On the server, edit the SSH daemon config:

sudo nano /etc/ssh/sshd_config

Open the SSH server configuration file

beginner

Find and change these lines:

PasswordAuthentication no
PermitRootLogin no
PubkeyAuthentication yes

Then restart SSH:

sudo systemctl restart sshd

Apply the new SSH config

beginner
Critical: Test Before Closing Your Session

Open a second SSH terminal and test you can still log in BEFORE closing the current one. If you lock yourself out, you’ll need console access to fix it.


Managing Multiple Keys

If you work with GitHub, AWS, and multiple servers, you’ll have multiple keys:

ssh-keygen -t ed25519 -f ~/.ssh/id_github -C "github"

Create a key specifically for GitHub

beginner
ssh-keygen -t ed25519 -f ~/.ssh/id_aws -C "aws-production"

Create a key for AWS access

beginner

In your ~/.ssh/config:

Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_github

Host aws-prod
    HostName ec2-xx-xx-xx-xx.compute.amazonaws.com
    User ec2-user
    IdentityFile ~/.ssh/id_aws

Useful Day-to-Day SSH Key Commands

TaskCommand
List loaded keysssh-add -l
Add key to agentssh-add ~/.ssh/id_ed25519
Remove all keys from agentssh-add -D
Check server’s host keyssh-keyscan server-ip
Copy file to serverscp file.txt user@server:/path/
Copy directory to serverscp -r dir/ user@server:/path/

Hands-On Challenge

  1. Generate a new Ed25519 key pair on your machine.
  2. View the public key with cat ~/.ssh/id_ed25519.pub.
  3. If you have access to a server (even a local VM), copy the key with ssh-copy-id.
  4. Test passwordless login.
  5. Bonus: Create a ~/.ssh/config entry and test the shortcut.

Key Takeaways

  • SSH keys use public-key cryptography — private key stays local, public key goes to servers.
  • Use Ed25519 (not RSA) for new keys — it’s smaller, faster, and more secure.
  • ssh-copy-id user@server is the easiest way to install your public key.
  • ~/.ssh/config lets you define shortcuts and per-host settings — use it.
  • Disable PasswordAuthentication entirely once keys are working — this is the professional standard.
🧠

Test Your Knowledge

Take a quick 4-question quiz to check your understanding.

📧

Get weekly IT guides

Join 5,000+ IT professionals

Subscribe Free
Type to start searching...