Skip to main content

Command Palette

Search for a command to run...

How to Create SSH Key in Linux

Updated
6 min read

Creating an SSH key in Linux is one of the best ways to secure your remote connections. If you want to access servers without typing your password every time, SSH keys are the way to go. They provide a safer and more convenient method than traditional passwords.

In this article, I’ll guide you through the process of generating SSH keys on your Linux system. You’ll learn how to create, manage, and use these keys to connect securely to remote machines. Let’s dive in and make your Linux experience smoother and safer.

What is an SSH Key and Why Use It?

SSH keys are a pair of cryptographic keys used to authenticate your identity when connecting to a remote server. Instead of using a password, the server checks if your private key matches the public key stored on it.

Here’s why SSH keys are better than passwords:

  • Stronger security: Keys are much harder to crack than passwords.
  • Convenience: No need to enter your password every time.
  • Automation: Perfect for scripts and automated tasks.
  • No risk of password theft: Your private key never leaves your machine.

Using SSH keys helps protect your data and makes managing multiple servers easier.

Preparing Your Linux System for SSH Key Generation

Before creating your SSH key, you need to ensure your Linux system has the necessary tools installed. Most Linux distributions come with OpenSSH pre-installed, but it’s good to check.

To verify, open your terminal and type:

ssh -V

If you see a version number, OpenSSH is installed. If not, install it using your package manager:

  • For Ubuntu/Debian:

    sudo apt update
    sudo apt install openssh-client
    
  • For Fedora/CentOS:

    sudo dnf install openssh-clients
    

Once OpenSSH is ready, you can proceed to generate your SSH key pair.

Step-by-Step Guide to Creating an SSH Key in Linux

Creating an SSH key involves generating a public and private key pair. The private key stays on your computer, while the public key is copied to the server.

Follow these steps:

  1. Open your terminal.

  2. Run the ssh-keygen command:

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

    Explanation:

    • -t rsa specifies the RSA algorithm.
    • -b 4096 sets the key size to 4096 bits for strong security.
    • -C adds a comment, usually your email, to identify the key.
  3. Choose the file location:

    You’ll be prompted to save the key. Press Enter to accept the default location (~/.ssh/id_rsa).

  4. Set a passphrase (optional but recommended):

    Adding a passphrase encrypts your private key. You’ll need to enter it when using the key. If you want no passphrase, just press Enter twice.

  5. Key generation completes:

    You’ll see a message showing the key fingerprint and location.

Your SSH key pair is now ready.

Understanding SSH Key Files and Their Purpose

After generating your SSH key, you’ll find two important files in your ~/.ssh directory:

File NamePurpose
id_rsaYour private key (keep secret)
id_rsa.pubYour public key (share with servers)

The private key should never be shared or exposed. The public key is what you upload to remote servers to grant access.

You can view your public key with:

cat ~/.ssh/id_rsa.pub

Copy this key when adding it to your server’s authorized keys.

How to Add Your SSH Public Key to a Remote Server

To use your SSH key for logging into a server, you need to add your public key to the server’s ~/.ssh/authorized_keys file.

Here are common methods:

This command copies your public key to the server automatically.

ssh-copy-id username@remote_host

Replace username and remote_host with your server’s details. You’ll be prompted for your password once.

Manually Adding the Key

If ssh-copy-id is not available:

  1. Connect to your server with a password:

    ssh username@remote_host
    
  2. Create the .ssh directory if it doesn’t exist:

    mkdir -p ~/.ssh
    chmod 700 ~/.ssh
    
  3. On your local machine, copy your public key:

    cat ~/.ssh/id_rsa.pub
    
  4. Paste the key into the server’s authorized_keys file:

    echo "paste_your_public_key_here" >> ~/.ssh/authorized_keys
    chmod 600 ~/.ssh/authorized_keys
    
  5. Exit and test your SSH login without a password.

Managing SSH Keys: Best Practices and Security Tips

Keeping your SSH keys secure is crucial. Here are some tips:

  • Use a strong passphrase: Protect your private key with a password.
  • Limit key usage: Use different keys for different servers or purposes.
  • Regularly update keys: Replace keys periodically to reduce risk.
  • Backup your keys: Store copies securely in case of loss.
  • Use SSH agents: Tools like ssh-agent help manage keys without entering passphrases repeatedly.
  • Disable password login on servers: Once keys are set up, disable password authentication for better security.

Following these practices keeps your connections safe and your keys protected.

Troubleshooting Common SSH Key Issues

Sometimes, you might face problems using SSH keys. Here are common issues and fixes:

  • Permission denied (publickey):

    • Check if your public key is correctly added to authorized_keys.
    • Ensure file permissions are correct (~/.ssh should be 700, authorized_keys 600).
    • Verify you’re using the right username and host.
  • SSH agent not forwarding keys:

    • Start the agent with eval "$(ssh-agent -s)".
    • Add your key using ssh-add ~/.ssh/id_rsa.
  • Passphrase prompt every time:

    • Use ssh-agent to cache your passphrase.
    • Consider using keychain tools for easier management.
  • Multiple keys and conflicts:

    • Specify the key with ssh -i ~/.ssh/your_key username@host.
    • Configure keys in ~/.ssh/config for automatic selection.

Advanced SSH Key Options and Alternatives

Beyond the basic RSA keys, Linux supports other key types and options:

  • Ed25519 keys:

    ssh-keygen -t ed25519 -C "your_email@example.com"
    

    These keys are faster and considered more secure than RSA.

  • ECDSA keys:

    ssh-keygen -t ecdsa -b 521 -C "your_email@example.com"
    
  • Changing key comment or passphrase:

    Use ssh-keygen -p to update your passphrase.

  • Using hardware security keys:

    Devices like YubiKey can store SSH keys for enhanced security.

Choosing the right key type depends on your security needs and compatibility.

Conclusion

Creating an SSH key in Linux is a straightforward process that greatly improves your security and convenience when accessing remote servers. By generating a strong key pair, adding your public key to servers, and managing your keys carefully, you can avoid password hassles and protect your connections.

Remember to keep your private key safe, use passphrases, and follow best practices to maintain security. Whether you’re a developer, system admin, or casual user, mastering SSH keys is a valuable skill that makes your Linux experience smoother and safer.


FAQs

How do I generate an SSH key with a different algorithm?

Use the ssh-keygen command with the -t option. For example, to create an Ed25519 key, run ssh-keygen -t ed25519 -C "your_email@example.com".

Can I use the same SSH key on multiple servers?

Yes, you can copy your public key to multiple servers. However, for better security, consider using different keys for different servers.

What if I forget my SSH key passphrase?

If you forget your passphrase, you cannot recover it. You’ll need to generate a new SSH key pair and update the public keys on your servers.

How do I check which SSH key is being used?

Run ssh -v username@host to see debug information, including which key is offered during authentication.

Is it safe to share my public SSH key?

Yes, the public key is meant to be shared with servers. It does not compromise your security since the private key remains secret.

More from this blog

L

LinuxBloke | Linux Tips, Tricks & Troubleshooting

672 posts