0
0
Linux-cliHow-ToBeginner · 3 min read

How to Set Up SSH Key on Linux: Simple Steps

To set up an ssh key on Linux, use ssh-keygen to create a key pair, then copy the public key to the remote server's ~/.ssh/authorized_keys file using ssh-copy-id. This enables secure, passwordless login via ssh.
📐

Syntax

The main commands to set up SSH keys are:

  • ssh-keygen -t rsa -b 4096 -C "your_email@example.com": Generates a new RSA key pair with 4096 bits and a comment.
  • ssh-copy-id user@remote_host: Copies your public key to the remote server for passwordless login.
  • ssh user@remote_host: Connects to the remote server using the SSH key.

Each part helps create, transfer, and use the SSH key securely.

bash
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
ssh-copy-id user@remote_host
ssh user@remote_host
💻

Example

This example shows how to generate an SSH key, copy it to a remote server, and connect without a password prompt.

bash
ssh-keygen -t rsa -b 4096 -C "alice@example.com"
# Press Enter to accept default file location and optionally set a passphrase
ssh-copy-id alice@192.168.1.10
ssh alice@192.168.1.10
Output
Generating public/private rsa key pair. Enter file in which to save the key (/home/alice/.ssh/id_rsa): Created directory '/home/alice/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/alice/.ssh/id_rsa. Your public key has been saved in /home/alice/.ssh/id_rsa.pub. The key fingerprint is: SHA256:... alice@example.com Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'alice@192.168.1.10'" and check to make sure that only the key(s) you wanted were added. Welcome to Ubuntu 20.04.4 LTS (GNU/Linux 5.4.0-104-generic x86_64) alice@remote:~$
⚠️

Common Pitfalls

Common mistakes when setting up SSH keys include:

  • Not setting correct permissions on ~/.ssh and authorized_keys (should be 700 for .ssh and 600 for authorized_keys).
  • Copying the private key instead of the public key to the server.
  • Using the wrong username or hostname when copying the key.
  • Not restarting the SSH service if configuration changes are made.

Fix permissions with chmod 700 ~/.ssh and chmod 600 ~/.ssh/authorized_keys.

bash
## Wrong: copying private key
scp ~/.ssh/id_rsa user@remote:~/.ssh/

## Right: copying public key
ssh-copy-id user@remote
📊

Quick Reference

CommandDescription
ssh-keygen -t rsa -b 4096 -C "email@example.com"Generate a new RSA SSH key pair with 4096 bits
ssh-copy-id user@hostCopy your public key to the remote server for passwordless login
ssh user@hostConnect to the remote server using SSH key authentication
chmod 700 ~/.sshSet correct permissions on .ssh directory
chmod 600 ~/.ssh/authorized_keysSet correct permissions on authorized_keys file

Key Takeaways

Use ssh-keygen to create a secure SSH key pair on your Linux machine.
Copy only the public key to the remote server using ssh-copy-id for easy setup.
Ensure correct permissions on ~/.ssh and authorized_keys to avoid access issues.
Test your SSH connection to confirm passwordless login works.
Avoid sharing your private key; keep it secure and private.