How to Generate SSH Keys on Ubuntu?

SSH keys are cryptographic security keys that provide a more secure authentication method than passwords when connecting to remote servers. Ubuntu makes it easy to generate SSH key pairs using the ssh-keygen command-line tool.

This guide will walk you through generating SSH keys on Ubuntu, understanding the different key types, and adding your public key to remote servers for secure authentication.

The Benefits of Using SSH Keys over Passwords

SSH keys offer significant security advantages over traditional password authentication:

  • Enhanced Security SSH keys use cryptographic algorithms that are nearly impossible to crack without access to the private key

  • No Brute Force Attacks Unlike passwords, SSH keys cannot be guessed through repeated login attempts

  • Convenient Access Once configured, SSH keys enable passwordless login to remote servers

  • Automation Support Perfect for automated scripts and CI/CD pipelines that need secure server access

Generating an SSH Key Pair on Ubuntu

To generate an SSH key pair on Ubuntu, open a terminal window by pressing Ctrl + Alt + T and run the following command:

ssh-keygen

By default, this generates an RSA key pair with 2048-bit encryption. The command creates two files: a private key (id_rsa) and a public key (id_rsa.pub) in the ~/.ssh directory.

Advanced Key Generation Options

For enhanced security, you can specify additional parameters:

ssh-keygen -t ed25519 -b 4096 -C "your-email@example.com"

To save keys with a custom name:

ssh-keygen -t rsa -b 4096 -f ~/.ssh/my_custom_key

Types of SSH Keys

Key Type Security Level Key Size Performance
RSA High 2048/4096 bits Good
Ed25519 Highest 256 bits Excellent
ECDSA High 256/384/521 bits Very Good
DSA Low (deprecated) 1024 bits Fair

Ed25519 is recommended for new key generation due to its superior security and performance characteristics.

Adding a Passphrase for Extra Security

When generating your SSH key, you'll be prompted to enter a passphrase. This adds an additional layer of security:

Enter passphrase (empty for no passphrase): 
Enter same passphrase again:

A passphrase protects your private key even if someone gains access to your key file. You can leave it empty for convenience, but this reduces security.

Adding Your Public Key to a Remote Server

To use SSH key authentication, you must copy your public key to the remote server. The most efficient method is using ssh-copy-id:

ssh-copy-id username@remote-server-ip

Manual Method

If ssh-copy-id is unavailable, manually append your public key to the remote server's authorized keys file:

cat ~/.ssh/id_rsa.pub | ssh username@remote-server-ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

Verification

Test your SSH key authentication by connecting to the remote server:

ssh username@remote-server-ip

If configured correctly, you should connect without entering a password (unless you set a passphrase for your private key).

Best Practices

  • Use Strong Key Types Prefer Ed25519 or RSA with 4096 bits

  • Set File Permissions Ensure private key has 600 permissions: chmod 600 ~/.ssh/id_rsa

  • Use Passphrases Add passphrases to private keys for enhanced security

  • Regular Key Rotation Replace old keys periodically

  • Backup Keys Securely Store private keys in secure, encrypted storage

Conclusion

SSH keys provide robust security for remote server access on Ubuntu. By generating strong key pairs using ssh-keygen and properly configuring them on remote servers, you establish a secure, password-free authentication system. Always use modern key types like Ed25519 and follow security best practices to maintain optimal protection.

Updated on: 2026-03-17T09:01:38+05:30

17K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements