Sabbir Hossain
Full Stack Software Engineer
How to Setup and Generate SSH Keys on Mac book and Ubuntu: A Step-by-Step Guide
Secure Shell (SSH) keys are a foundational tool for securing remote access. Compared to password-based login, SSH key authentication is safer, more convenient, and ideal for automation. In this guide, you’ll learn how to generate an SSH key pair on Ubuntu, install the public key on a server, and configure things properly.
What Are SSH Keys & Why Use Them?
SSH key pairs use public-key cryptography: you keep a private key (secret) on your local machine, and place the public key on remote servers.
When you try to log in, the server verifies you have the corresponding private key—without transferring the private key itself.
Advantages: stronger security (harder to brute force), passwordless login, better for scripting and automation.
Modern recommendations favor algorithms like Ed25519 for key generation (vs older RSA).
Step 1: Check for Existing Keys
Before creating a new key, see if you already have one:
If the command returns one or more files (e.g.
id_ed25519.pub,id_rsa.pub), you already have a public key.You can reuse an existing key (if you trust it) or back it up and generate a fresh one.
Note: generating a new key may overwrite the existing default key unless you specify a custom name.
Step 2: Generate a New SSH Key Pair
Run the ssh-keygen command. A common and modern approach:
Here:
-t ed25519— chooses the Ed25519 algorithm (strong and fast)-C "..."— adds a comment / label (often your email) to identify the key later.You’ll be prompted:
File to save key: press Enter to accept default (
~/.ssh/id_ed25519) or specify a custom name.Passphrase: you can enter a strong passphrase (recommended) or leave blank (not recommended for high-security systems).
If the default file already exists, you’ll be asked whether to overwrite—be careful to avoid losing access tied to old key(s).
After success, you’ll see output like:
Step 3: Copy the Public Key to the Remote Server
Now that you have a public key file (e.g. ~/.ssh/id_ed25519.pub), you need to add it to the authorized_keys file of the remote server and user account.
This will:
-
Prompt for the remote user’s password (one last time)
-
Append your public key to
~/.ssh/authorized_keysof that user on the remote machine -
Set correct permissions (often) automatically
Method B: Manual method (if ssh-copy-id is unavailable)
1. Display your public key:
Copy the full contents (starts with ssh-ed25519 AAAA… … comment).
2. Log into the remote server (temporarily with password):
3. On the remote server:
Paste your public key as a single line into that file
Save it, then:
Important: file and directory permissions are strict. If they’re too permissive, SSH will refuse key authentication
Step 4: Test SSH Key Authentication
From your local machine:
If it works, you should be logged in without being prompted for the remote user’s password.
If you set a passphrase, you’ll be prompted for it (or your SSH agent may cache it).
If you see an error like
Permission denied (publickey), something is wrong (permissions, key mismatch, missing public key).
To debug, you can use verbose mode:
The -v (or -vvv) flags show detailed debug output which helps trace where the authentication fails (e.g. which keys were tried, if files were rejected, etc.).
Step 5: (Optional but Recommended) Disable Password Authentication
Once key authentication is confirmed working, you can disable password-based login on the server. This hardens security (no one can log in with a password).
1. Edit the SSH server config on remote:
2. Locate (or add) these lines:
Copy the full contents (starts with ssh-ed25519 AAAA… … comment).
3. Save and exit, then restart SSH:
⚠️ Warning: Before disabling password login, make absolutely sure your key login works (in a separate session). Otherwise, you may lock yourself out.
Best Practices & Tips
Use a passphrase: encrypting your private key adds a layer of protection if your machine is compromised.
SSH Agent: use
ssh-agentto cache decrypted keys so you don’t need to enter your passphrase every time.Different keys per server: for better isolation, you may generate distinct key pairs per server or use a naming convention.
Key rotation: periodically replace old keys with new ones and remove old public keys from servers.
Backup your private key: store it in a secure, encrypted location. Losing it means losing access.
Permissions matter:
~/.sshshould be700,authorized_keys600, private key file600(only readable by you).Disable root SSH login: force login using non-root user + sudo to improve security.
Use secure algorithms: Ed25519, or RSA with 4096 bits, are good choices. Avoid deprecated or weak algorithms.
Troubleshooting Common Issues
Permission denied errors: often due to incorrect permissions on
.ssh,authorized_keys, or private key files. Make sure only the owner has access.SSH not running / port issues: confirm SSH daemon is active on server (
sudo systemctl status sshd) and firewall allows port 22 (or whichever port used).Wrong public key or mismatch: ensure the exact public key string from your
.pubfile is present inauthorized_keys(no line breaks or extra spaces).Multiple keys not used: SSH may try other keys first—use
ssh -i path/to/keyor configure~/.ssh/configto force a specific key.Agent not forwarding or loaded: ensure
ssh-agentis running and your key is added (viassh-add).
Conclusion
Generating and using SSH keys is an essential skill for anyone managing remote servers. With a secure key pair in place, you eliminate much of the risk associated with password logins, while making your workflow smoother and automatable.
If you like, I can also create a version of this post formatted for WordPress, Markdown, or with your site’s branding and code highlighting. Do you want me to prepare that for you?