Blog | G5 Cyber Security

SSH Key Management: Security Best Practices

TL;DR

Using a separate SSH key per host (and user) is significantly more secure than using one SSH key for all hosts. While the single-key approach is easier to manage, it creates a much larger attack surface. This guide explains why and how to implement the best practice.

Why Separate Keys are Better

Think of your SSH keys like passwords. You wouldn’t use the same password for everything, right? If one service is compromised, all your accounts using that password are at risk. The same principle applies to SSH keys.

Step-by-Step Guide: Separate SSH Keys

  1. Generate a New Key Pair for Each Host/User Combination: Use the ssh-keygen command.
    ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa_hostname -C "user@hostname"

    Replace hostname with the actual hostname and adjust the path if needed. The `-t rsa` specifies RSA, `-b 4096` sets key length to 4096 bits (good security), and `-f` defines the filename.

  2. Copy the Public Key to the Host: Use ssh-copy-id. This is the easiest method if you have password access initially.
    ssh-copy-id -i ~/.ssh/id_rsa_hostname user@hostname

    You’ll be prompted for the user’s password on the remote host.

  3. Test the Connection: Try connecting using the new key.
    ssh -i ~/.ssh/id_rsa_hostname user@hostname

    If it connects without asking for a password, you’ve successfully added the key.

  4. Disable Password Authentication (Highly Recommended): Once keys are set up, disable password authentication on the host to prevent brute-force attacks.
    • Edit /etc/ssh/sshd_config as root.
    • Find and change these lines:
      PasswordAuthentication no
      ChallengeResponseAuthentication no
    • Restart the SSH service:
      sudo systemctl restart sshd
  5. Repeat for Each Host/User Combination: Follow steps 1-4 for every server and user you need to access.

Managing Multiple Keys

Using many keys can seem daunting, but SSH provides tools to make it manageable.

Risks of Using a Single SSH Key

Exit mobile version