guides / ssh.en
Adding my SSH key to your server
How to add my public SSH key to your server. The commands check the key fingerprint, add the key only once and set the correct permissions.
What you need
- Access to the server (Linux) as the account you want to give me access to.
curlorwgetandssh-keygenon the server (usually part of OpenSSH).
This guide adds my public SSH key to your server. After that I log in to the server with the key, without typing a password.
My key
| Type | ED25519 |
|---|---|
| Fingerprint (SHA256) | SHA256:+zmRgF8O83cBBpFxo8pGdQtMvqjqUBbT1jA79HSTn7M |
| File | miroslavkral.eu/files/ssh-key.pub |
Adding the key
Step 1 Log in to the server
Log in as the user you are giving me access to. The key works only for this user.
For the root account, switch first with sudo -i.
Step 2 Run the commands
Copy the whole block and paste it into the terminal:
url=https://miroslavkral.eu/files/ssh-key.pub
fp=SHA256:+zmRgF8O83cBBpFxo8pGdQtMvqjqUBbT1jA79HSTn7M
key=$(curl -fsSL "$url" 2>/dev/null || wget -qO- "$url")
case "$key" in "ssh-ed25519 "*) got=$(printf '%s\n' "$key" | ssh-keygen -lf - 2>/dev/null | awk '{print $2}') ;; *) got= ;; esac
if [ "$got" = "$fp" ] && [ "$(printf '%s' "$key" | tr -d '\r\n')" = "$key" ]; then
if mkdir -p ~/.ssh && chmod 700 ~/.ssh &&
{ [ ! -s ~/.ssh/authorized_keys ] || [ -z "$(tail -c1 ~/.ssh/authorized_keys)" ] || echo >> ~/.ssh/authorized_keys; } &&
{ grep -qxF "$key" ~/.ssh/authorized_keys 2>/dev/null || printf '%s\n' "$key" >> ~/.ssh/authorized_keys; } &&
chmod 600 ~/.ssh/authorized_keys &&
grep -qxF "$key" ~/.ssh/authorized_keys; then
echo "OK: key $fp is in ~/.ssh/authorized_keys"
else
echo "ERROR: writing the key or setting permissions failed." >&2
fi
else
echo "ERROR: key not downloaded or fingerprint mismatch. Nothing changed." >&2
fiWhat the commands do:
- They download the key (curl, or wget) and check that the file holds only this one key, with the right fingerprint. Otherwise they change nothing.
- They create
~/.sshwith mode 700. - If
authorized_keysdoes not end with a newline, they add one at the end. Otherwise the keys would run together on one line. - They add the key only once. Running them again is safe.
- They set
authorized_keysto 600. Otherwise SSH ignores the key. - If any step fails, they print
ERROR:and noOK:.
Step 3 Check the result and write to me
If ERROR: or another error appears, send me the complete output. After OK:, contact me; I will verify that I can log in.
- The commands printed
OK: key SHA256:... is in ~/.ssh/authorized_keys. - You wrote to me and I tested the login.
Manual install (alternative)
You can also download the key yourself and add it as a new line to ~/.ssh/authorized_keys. Before you add it, check the fingerprint with ssh-keygen -lf ssh-key.pub. It must print the fingerprint from the table above.
Removing access
You can remove my access at any time. This command deletes every line with my key, regardless of its comment:
sed -i '/AAAAC3NzaC1lZDI1NTE5AAAAIC1bOpOtVOxptl4QBVzLY2mWMUEuGeh2+0k\/PgoqTYjO/d' ~/.ssh/authorized_keysTroubleshooting common problems
ERROR is printed
- Check that the server can reach the internet.
- The server needs
curlorwget. On Debian and Ubuntu, installcurlwithapt install curl(as root). - Does traffic go through a proxy that returns its own page? Then add the key manually (see above).
- Old OpenSSH (before version 7.2) cannot run the commands. Use the manual install.
The commands worked, but key login fails
- The key is only set for the user who ran the commands. Tell me which user it is.
- The home directory must not be writable by the group or others. Otherwise SSH rejects the key.
- The SSH server settings can block key login. I will check that, write to me.
If nothing helps, write to me: which server, which user, and what the commands printed.