Accessing Servers with SSH Keys

@fakhrulnugrohoJune 16, 2025

SSH keys are the first thing I set up on any new server. Compared to passwords they're both more secure (nothing to brute-force) and more convenient (no prompt on every login). This is the full setup, from generating a key pair to logging in with it.

πŸ”‘ What SSH keys are

An SSH key pair has two halves:

The server uses the public key to issue a challenge that only the holder of the private key can answer, so nothing secret ever travels over the wire.

πŸ› οΈ Step 1: Generate a key pair (if you don't have one)

BASH
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Press Enter to accept the default location (~/.ssh/id_rsa), then set a passphrase when prompted. The passphrase is optional, but it means a stolen private key file is still useless on its own β€” worth the small friction.

πŸ“ Step 2: If you already have a key pair

Check ~/.ssh/ before generating anything β€” a second key pair when one already exists just creates confusion:

BASH
ls ~/.ssh

If you see id_rsa (private key) and id_rsa.pub (public key), skip generation and go straight to installing the public key.

πŸ“€ Step 3: Add the public key to the server

  1. Print your public key and copy the output:

    BASH
    cat ~/.ssh/id_rsa.pub
    
  2. Log in to the server with whatever method you currently have (usually a password).

  3. Append the key to authorized_keys:

    BASH
    echo "your_public_key_contents" >> ~/.ssh/authorized_keys
    

    Note the >> β€” a single > would overwrite the file and lock out every key already installed.

πŸ”’ Step 4: Fix the permissions

SSH is strict about this: if these files are too open, it silently ignores them and falls back to password authentication.

BASH
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod 600 ~/.ssh/id_rsa

πŸ”— Step 5: Connect

BASH
ssh -i ~/.ssh/id_rsa username@server_ip

If the key is in the default location, you can drop the -i flag entirely β€” ssh looks there on its own. Adjust the command if your server uses a non-default port.

πŸ“Œ Troubleshooting

πŸ”š Conclusion

Once key-based login works, the natural next step is disabling password authentication entirely in sshd_config β€” at that point, brute-force attempts against your server become pointless.

Happy coding πŸš€