Skip to main content

SSH Public Key

An SSH public key is the shareable half of an SSH key pair. It is placed in ~/.ssh/authorized_keys on a server so the server can verify that a client holds the matching private key.

Understanding SSH Public Keys

What Is an SSH Public Key?

Think of a key pair as a padlock and a key. The public key is the padlock: you can copy it anywhere and it does no harm if seen. The private key is the key that opens it. Installing your public key on a server means only someone holding your private key can log in.

What an SSH Public Key Looks Like

◈ DIAGRAM
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... rahul@devops.in
| | |
key type base64 key material optional comment

How to Generate and Install a Public Key

Bash
ssh-keygen -t ed25519 -C "rahul@devops.in"
# Creates ~/.ssh/id_ed25519 (private) and ~/.ssh/id_ed25519.pub (public)
cat ~/.ssh/id_ed25519.pub
ssh-copy-id -i ~/.ssh/id_ed25519.pub rahul@10.0.1.50 # appends to authorized_keys
ssh-keygen -l -f ~/.ssh/id_ed25519.pub # fingerprint

How to Restrict What a Key Can Do

Options at the start of an authorized_keys line limit a key's power.

TEXT
from="10.0.0.0/8" ssh-ed25519 AAAA... deploy-automation
command="/opt/scripts/backup.sh" ssh-ed25519 AAAA... backup-runner
  • from= limits which source addresses may use the key
  • command= forces one command no matter what the client asks for

Troubleshooting Public Keys

Symptom Command What to Check
Key not accepted ls -la ~/.ssh authorized_keys 600, dir 700
Wrong key offered ssh -i key user@host Specify the key
Key not found ssh-add -l Loaded in agent
Remember

~/.ssh must be 700 and authorized_keys must not be writable by group or others, or sshd silently refuses the keys. The home directory must also not be group or world writable.

Security

Remove public keys from authorized_keys when people leave or devices are lost. A key grants access for as long as it stays listed, and rotating keys at least annually is good practice.

Tip

The server's log, for example journalctl -u ssh (the unit is sshd on RHEL), usually states exactly why a key was refused.

Frequently Asked Questions

Why is it safe to share a public key but not a private key?

The public key only lets a server verify signatures made by the private key. You cannot feasibly derive the private key from it, so it can sit in GitHub profiles and authorized_keys files safely.

What commonly breaks key login on the server side?

Wrong permissions on `~/.ssh`, `authorized_keys`, or the home directory. sshd refuses the key and the client only sees "Permission denied", so check the server log.

Can one authorized_keys file hold many keys?

Yes, one key per line. Each line is a separate authorized client, so offboarding means deleting that person's line.

Found a mistake, an outdated step or a broken link?