To use SSH keys on Linux for secure, password-less authentication, follow these steps:
1. Generate an SSH Key Pair
ssh-keygen -t ed25519 -C "your_email@example.com" # Recommended (Ed25519)
Or for RSA:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Output: Two files created:
- Private key:
~/.ssh/id_ed25519
(keep this secure; never share it). - Public key:
~/.ssh/id_ed25519.pub
(copy to servers).
2. Copy the Public Key to the Server
Method 1: Use ssh-copy-id
:
ssh-copy-id -i ~/.ssh/id_ed25519.pub username@server_ip
- Enter your server password when prompted.
- This appends your public key to
~/.ssh/authorized_keys
on the server.
Method 2: Manual Copy:
Display your public key:
cat ~/.ssh/id_ed25519.pub
Log in to the server and paste the key into ~/.ssh/authorized_keys
:
mkdir -p ~/.sshecho "public_key_string" >> ~/.ssh/authorized_keyschmod 700 ~/.sshchmod 600 ~/.ssh/authorized_keys
3. Connect via SSH
ssh -i ~/.ssh/custom_key_name username@server_ip
Troubleshooting
chmod 700 ~/.ssh && chmod 600 ~/.ssh/id_ed25519*