Sub topic 5
Protecting Your Private Key with a Passphrase
A passphrase is a password that protects your private SSH key on your local computer. This chapter assumes that you can use a Linux or macOS command line, have generated an SSH key pair, and understand basic Unix file permissions.
Passwordless SSH does not mean that the private key has no protection. It means the remote server can authenticate you without asking for your server account password. Your private key should still have a passphrase. If someone copies the private key file from your laptop, the passphrase prevents them from using it immediately.
What the passphrase protects
An SSH key pair has two files:
- The public key can be installed on servers. It is safe to copy.
- The private key must remain secret. It proves that you own the key pair.
When you create a key with a passphrase, SSH encrypts the private key on disk. The passphrase unlocks the key for use. The server does not receive the passphrase.
sequenceDiagram
participant U as User
participant C as SSH client
participant S as SSH server
U->>C: Enter passphrase
C->>C: Unlock encrypted private key
C->>S: Prove possession of private key
S->>C: Accept authenticated sessionThe passphrase is different from the password for your remote Linux account. You can use a passphrase even when the server has password authentication disabled.
Create a key with a passphrase
When ssh-keygen prompts for a passphrase, enter a long phrase that you can remember. Do not leave it empty unless you have a specific reason and another protection mechanism.
ssh-keygen -t ed25519 -C "alice@laptop" -f ~/.ssh/id_ed25519The command creates:
~/.ssh/id_ed25519, the encrypted private key~/.ssh/id_ed25519.pub, the public key
A good passphrase is long and unique. Several unrelated words are usually easier to remember than a short, complicated password. Do not reuse your email, server, or account password.
If you already created a key without a passphrase, add or change one with:
ssh-keygen -p -f ~/.ssh/id_ed25519The command asks for the old passphrase, then asks for the new one. It updates the private key without changing the public key, so you do not need to install the public key again.
Avoid repeated prompts with ssh-agent
Entering the passphrase for every SSH connection is inconvenient. An SSH agent is a local process that keeps an unlocked key in memory for the current session.
Start an agent and load your key:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519Enter the passphrase once when ssh-add asks for it. Later SSH commands can use the key through the agent:
ssh [email protected]The agent does not store the passphrase as plain text. However, any process that can access your agent may be able to use the loaded key. Use agent forwarding carefully, especially on servers you do not fully control. A compromised remote host could abuse a forwarded agent while your session is open.
List keys currently loaded in the agent:
ssh-add -lRemove one key when it is no longer needed:
ssh-add -d ~/.ssh/id_ed25519Remove every loaded key:
ssh-add -DCheck that the key is protected
You can inspect the key file without displaying its contents:
ls -l ~/.ssh/id_ed25519The permissions should normally show that only your user can read and write the file. If the file is readable by other users, fix it:
chmod 600 ~/.ssh/id_ed25519A passphrase protects the key if the file is stolen, but it does not protect an already unlocked key from a compromised local computer. Keep your operating system updated, lock your screen, and remove keys from the agent when you finish sensitive work.
Exercise
You generated ~/.ssh/id_ed25519 without a passphrase. How can you protect it without reinstalling its public key on the server?
Show answer
Run ssh-keygen -p -f ~/.ssh/id_ed25519, enter the current passphrase if one exists, and provide a new passphrase. The public key does not change.