Setting up SSH keys with the correct permissions is a classic "do it once, do it right" task. If the permissions are too loose, SSH will actually refuse to use the keys for security reasons.
Here is the quick, step-by-step guide to generating an Ed25519 key pair and setting the exact permissions needed for your ~/.ssh directory and files.
Open your terminal and run the following command. The -t flag specifies the type, and -C adds a comment (usually your email) to help you identify the key.
ssh-keygen -t ed25519 -C "your_email@example.com"
- File location: It will ask where to save the key. Press
Enterto accept the default location (~/.ssh/id_ed25519). - Passphrase: It will ask for a passphrase. This adds an extra layer of security. Type one in, or press
Enterto leave it empty (though a passphrase is highly recommended).
To secure your keys, you need to restrict access so that only you (the owner) can read or modify them.
Run these commands in your terminal:
# 1. Set permissions for the .ssh directory itself
chmod 700 ~/.ssh
# 2. Set permissions for your private key (Crucial! Nobody else should see this)
chmod 600 ~/.ssh/id_ed25519
# 3. Set permissions for your public key (Can be slightly more relaxed)
chmod 644 ~/.ssh/id_ed25519.pub
If you also have an authorized_keys file in that directory (where you paste keys allowed to log into your machine), it should also be set to 600:
chmod 600 ~/.ssh/authorized_keys
Here is a quick reference for exactly what the chmod permissions mean for your SSH setup:
| Directory / File | Recommended chmod |
What it means |
|---|---|---|
~/.ssh/ (Directory) |
700 |
Only the owner can read, write, and enter the directory. |
id_ed25519 (Private Key) |
600 |
Only the owner can read and write to this file. |
id_ed25519.pub (Public Key) |
644 |
Owner can read/write; others can only read it. |
authorized_keys (If exists) |
600 |
Only the owner can read and write to this file. |
You can double-check that you got it right by running:
ls -la ~/.ssh
You should see drwx------ for the . (current directory) and -rw------- for your private key.