How to Generate and Utilize SSH Keys on Linux

How to generate SSH keys – step-by-step guide for secure server access

Generating and Using SSH Keys on Linux (Step-by-Step)

SSH keys provide a secure and convenient way to authenticate to remote servers without relying on passwords. They offer stronger security and greatly reduce the risk of brute-force attacks.

Tip: SSH keys are much harder to brute-force than passwords and are considered a best practice for server access.

Step 1 — Check for Existing SSH Keys

Before generating a new key pair, check whether you already have SSH keys:

ls ~/.ssh

If you see files such as id_rsa and id_rsa.pub, you already have an SSH key pair and can use it immediately.

Warning: Do not overwrite an existing private key unless you are sure it is no longer needed. Otherwise, you may lose access to servers that depend on it.

Step 2 — Generate a New SSH Key Pair

If you need a new key pair, run the following command:

ssh-keygen -t rsa -b 2048
  • -t rsa: Specifies the key type
  • -b 2048: Sets the key size

You will be prompted to choose a location for the new key:

~/.ssh/id_rsa

You can also set an optional passphrase for additional protection.

Note: A passphrase encrypts your private key on disk and adds an extra layer of security.

Step 3 — Copy the Public Key to the Remote Server

To enable key-based access, copy your public key to the remote server:

ssh-copy-id username@remote_server

Replace username with your remote account name and remote_server with the server’s address.

Step 4 — Test SSH Key Login

After copying the key, test your connection:

ssh username@remote_server

If you set a passphrase, you will be prompted to enter it.

Additional Tips

  • Use SSH Agent
    • Start the SSH agent:
    eval "$(ssh-agent -s)"
    • Add your key:
    ssh-add ~/.ssh/id_rsa
  • Configure SSH
    • You can configure per-host settings in:
    ~/.ssh/config
  • Revoke Access
    • If your private key is compromised, remove the corresponding public key from authorized_keys on the server.
  • Use Separate Keys
    • Consider using different SSH keys for different servers or services.
Tip: You can reuse the same SSH key by adding the public key to multiple servers.

Frequently Asked Questions

What is an SSH key?

An SSH key is a cryptographic authentication method that allows secure access to a server without using a password.

Do I need a passphrase?

A passphrase adds an extra layer of security by encrypting your private key. It is recommended, especially for sensitive environments.

Can I use the same SSH key on multiple servers?

Yes. You can add the same public key to the authorized_keys file on different servers and use one private key to connect.

What happens if I lose my private key?

If you lose your private key, you will not be able to access servers that rely on it. You must generate a new key and update access.

You May Also Like
Proxmox commands cheat sheet terminal output
Read More

Proxmox Commands – cheat sheet

Managing Proxmox Virtual Environment (PVE) through the command line can significantly speed up administration tasks, especially when working…
secure ssh configuration changing default ssh port for linux and windows servers
Read More

How to Change the SSH Port

Why Change the Default SSH Port? Changing the default SSH port is a common security practice that helps…