# SSH Tutorial: Keys, Config, and a Safer First Login

## Quick answer

SSH keys let you log in to a server without typing a password each time: generate a key pair, keep the private key on your own machine, and put the public key on the server. Add a short entry to your ssh config file, and `ssh shop` replaces the long command with the hostname, user, and key file. This post does the whole setup for a small shop server, permission error included.

A small clothing shop in Karachi runs its website and orders on one rented server, with [NGINX in front of it](https://practicaldevblog.blogspot.com/2026/11/nginx-explained-server-in-front-of-your.html). The owner logs in with a password shared with the staff. The password is short, because a long one is hard to type on a phone. When a staff member leaves, nobody can say for sure who still knows it. The shop does not need a security product. It needs a login that cannot be guessed or shared by accident, and can be taken away in one step. That is what an SSH key pair does.

## What I would check before using it

My first check is whether the server accepts key logins. Look at `/etc/ssh/sshd_config` and confirm `PubkeyAuthentication` is on and `AuthorizedKeysFile` points at `.ssh/authorized_keys`. If the server only allows passwords, a key will not help until that setting changes and the SSH service restarts. If you do not control the server, ask the person who does.

My second check is the key type. Ed25519 is my default, as judgement: short and fast, and supported by every modern server. RSA with 4096 bits is the fallback for an old server that does not know ed25519, and anything shorter is not worth generating in 2026. On a current Linux install, which the [Linux Explained: The Operating System Running the World](https://practicaldevblog.blogspot.com/2026/10/linux-explained-operating-system.html) post covers, ed25519 just works.

My third check is the passphrase. A passphrase protects the private key if the laptop is stolen, and `ssh-agent` means you type it once per session instead of once per login. A key with no passphrase suits a throwaway test or an automated job, and is a bad idea for the one key that reaches production. Decide before generating, because changing it later means reissuing the key or running `ssh-keygen -p`.

My fourth check is day-to-day use: one key per machine, so a lost laptop costs one key, not every login, and a short config entry removes the daily typing that pushes people back to passwords.

My verdict, as judgement: use ed25519 keys with a passphrase and an ssh config entry for any server you touch more than once. It flips when the server only allows passwords, or when the login is a one-off where a password is simpler.

## A Karachi shop server, reached by key

The demo is the shop from the intro: one server, `shop.example.com`, one deploy user, and a throwaway test key in `/tmp/ssh-demo`, never a real credential.

```
$ ssh -V
OpenSSH_9.6p1 Ubuntu-3ubuntu13.20, OpenSSL 3.0.13 30 Jan 2024
```

Generate the key pair: ed25519, a comment naming the shop, no passphrase for this test key:

```
$ ssh-keygen -t ed25519 -C "shop@karachi-shop" -f /tmp/ssh-demo/id_ed25519 -N ""
Generating public/private ed25519 key pair.
Your identification has been saved in /tmp/ssh-demo/id_ed25519
Your public key has been saved in /tmp/ssh-demo/id_ed25519.pub
The key fingerprint is:
SHA256:phUdGfahzF9ArxIDBJdtds9+WmEp4WIWxVR14lnBm4c shop@karachi-shop
```

The private key, `id_ed25519`, is the secret; the public key, `id_ed25519.pub`, goes on the server:

```
$ ls -l /tmp/ssh-demo
-rw------- 1 root root 411 Dec  5 03:01 id_ed25519
-rw-r----- 1 root root  99 Dec  5 03:01 id_ed25519.pub
```

Check the fingerprint, then write the config entry:

```
$ ssh-keygen -y -lf /tmp/ssh-demo/id_ed25519.pub
256 SHA256:phUdGfahzF9ArxIDBJdtds9+WmEp4WIWxVR14lnBm4c shop@karachi-shop (ED25519)
```

Now write the config entry:

```
Host shop
    HostName shop.example.com
    User deploy
    IdentityFile /tmp/ssh-demo/id_ed25519
    IdentitiesOnly yes
```

`ssh -G` prints the effective settings, catching typos before they waste a login attempt:

```
$ ssh -F config -G shop
user deploy
hostname shop.example.com
port 22
identitiesonly yes
identityfile /tmp/ssh-demo/id_ed25519
```

Here is where the classic mistake arrived: I loosened the private key permissions, and the next check refused it outright:

```
$ chmod 644 id_ed25519
$ ssh-keygen -y -f id_ed25519
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for 'id_ed25519' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "id_ed25519": bad permissions
```

SSH will not use a private key other users can read, because anyone who can read it can log in as you. The fix is one command, then the same check again:

```
$ chmod 600 id_ed25519
$ ls -l id_ed25519
-rw------- 1 root root 411 Dec  5 03:01 id_ed25519
$ ssh-keygen -y -f id_ed25519
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIR2f3X8MfsRyQ9TdZY9p2r0mN0uUJz40/Vu7HGJmfEu shop@karachi-shop
```

The key loads again. Copy the public key to the server, then log in with the short command:

```
$ ssh-copy-id deploy@shop.example.com
$ ssh shop
```

EXPECTED, not run here: outbound SSH is blocked in this sandbox, so the copy and the first login were not executed. Normally, the first command asks for the server password once and appends the public key to `~/.ssh/authorized_keys`, and the second then logs in without asking for one. If it still asks, `ssh -v shop` shows which key was offered and why the server turned it down.

<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5FkLbLzZ810sZOIAJfufzN3KMeIzaWSfBHD_UfjCn2c52HznDlVriGYwjQ5FjrhvZ77UO7zK9hxqKNGI1D_mbwB4E_F8AYDrdQl6LWBaCEcH-UlBXrdhDETdJQ3tk5pFsUi5FHwEZ43vtQ13VeLqmJJ6kLh7qAF5rVvaVYR_wRlrCh8pKYGZ9x4tMLUY/s1600/batch-69-ssh-terminal.png" alt="Terminal output showing an ed25519 SSH key generated for a Karachi shop server, the ssh config parsed with ssh -G, a permissions error on a 0644 private key, and the fixed key printing its public key." />

## Common mistakes and how to fix them

First, the private key with loose permissions, shown above: `Permissions 0644 for 'id_ed25519' are too open`, followed by `Load key "id_ed25519": bad permissions`. SSH ignores the key entirely, so the login falls back to a password. The fix is `chmod 600` on the private key, and `700` on the `~/.ssh` folder itself.

Second, adding a key with no agent running: `ssh-add id_ed25519` answers `Could not open a connection to your authentication agent` and exits with status 2. The fix is to start the agent first: `eval "$(ssh-agent -s)"` prints `Agent pid 2401`, and `ssh-add id_ed25519` then answers `Identity added`.

Third, connecting to a server where SSH is not running at all: `ssh: connect to host localhost port 22: Connection refused`. The fix is on the server: install and start the SSH service, and check the firewall allows port 22. Check the service before re-checking the key, because the hostname can be right while nothing is listening.

## Frequently Asked Questions

Do I need a different key for each server? No, one key per machine is enough. The same public key can go on many servers, while the private key never leaves the machine that generated it. If a laptop is lost, remove that one public key from every server and generate a fresh pair.

Is ed25519 better than RSA? For new keys, yes, as judgement. Ed25519 keys are shorter, faster to generate, and modern servers support them. RSA with 4096 bits still works and is the right choice for an old server that does not accept ed25519.

What happens if I lose my private key? You lose that login, and that is by design: nobody can recover it for you. Keep a second way in, such as the hosting provider console, then remove the lost public key from every server that had it.

## What to remember

- Generate ed25519 keys, one pair per machine, with a passphrase on any key that reaches a real server.
- A private key must be readable only by you: `chmod 600`, and `~/.ssh` should be `700`.
- An ssh config entry turns a long command into `ssh shop`, and `ssh -G shop` shows the settings it will use.
- When a login fails, read the error: a refused key, a missing agent, and a refused connection are three different problems.

## Where this leaves you

The shop now has a login that cannot be guessed and can be revoked in one step, instead of a password everyone remembers. The permission error was the useful part: SSH would rather refuse a readable key than let it be used. Set up one key pair for a server you already use, keep the private key at `600`, and try the short `ssh` command from your config. If the server still asks for a password, the verbose output tells you where the key got stuck.

## Further reading

- [ssh(1) manual page](https://man7.org/linux/man-pages/man1/ssh.1.html)
- [OpenSSH manual pages](https://www.openssh.com/manual.html)
- [Generating a new SSH key and adding it to the ssh-agent (GitHub Docs)](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent)
- [Linux Explained: The Operating System Running the World](https://practicaldevblog.blogspot.com/2026/10/linux-explained-operating-system.html)
- [Git Branching Explained: Work Without Breaking the Main Code](https://practicaldevblog.blogspot.com/2026/10/git-branching-explained.html)
- [NGINX Explained: The Server in Front of Your Server](https://practicaldevblog.blogspot.com/2026/11/nginx-explained-server-in-front-of-your.html)
- [VS Code Explained: The Editor That Ate the World](https://practicaldevblog.blogspot.com/2026/10/vs-code-explained-editor-that-ate-world.html)

Waqas Shahid is a software engineer from Karachi with more than 12 years of experience, including 6+ years in engineering leadership. He writes The Practical Developer.

Originally published at https://practicaldevblog.blogspot.com/2026/12/ssh-keys-config-tutorial.html
