How do I install an ssh key ubuntu server

To install an SSH key on your Ubuntu server for secure, passwordless login, follow these steps:


🛠️ Step 1: Generate SSH Key Pair (on your local machine)

Open a terminal and run:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  • Press Enter to accept the default file location (~/.ssh/id_rsa)
  • Optionally enter a passphrase for added security

This creates:

  • ~/.ssh/id_rsa — your private key
  • ~/.ssh/id_rsa.pub — your public key

📤 Step 2: Copy Public Key to Ubuntu Server

Use ssh-copy-id to transfer your public key:

ssh-copy-id username@your_server_ip

If your server uses a custom port (e.g. 2222):

ssh-copy-id -p 2222 username@your_server_ip

This appends your public key to the server’s ~/.ssh/authorized_keys file.


🔐 Step 3: Verify SSH Key Authentication

Try logging in:

ssh username@your_server_ip

If successful, you won’t be prompted for a password.


🧱 Optional: Disable Password Authentication (for extra security)

Edit the SSH config on the server:

sudo nano /etc/ssh/sshd_config

Set these options:

PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM no

Then restart SSH:

sudo systemctl restart ssh

Leave a Reply