DEV Community

Nate
Nate

Posted on Originally published at shadeterm.com

Install an SSH Public Key Without Guessing Which Key You Sent

A successful ssh-copy-id run does not guarantee that your next SSH connection will use the key you just installed. With multiple keys or an SSH agent, it is easy to install one public key and then test with another identity.

The reliable workflow is simple: select the public key explicitly, install it using an existing login, then test with its matching private key.

Install the key on Linux or macOS

First check that ssh-copy-id and the public key are available:

command -v ssh-copy-id
ls -l ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

If you do not have a key pair yet, create one. Keep the private key—the file without .pub—on your client machine:

ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519
Enter fullscreen mode Exit fullscreen mode

Now install the public key for the account you intend to use:

ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host
Enter fullscreen mode Exit fullscreen mode

For a server listening on a non-default port, pass the port with -p:

ssh-copy-id -i ~/.ssh/id_ed25519.pub -p 2222 user@host
Enter fullscreen mode Exit fullscreen mode

You must already be able to authenticate to the remote account. ssh-copy-id uses that existing access to append the public key to the account’s authorized_keys file; it cannot bypass a rejected login or install a key without permission.

On the first connection, SSH may ask you to trust the server’s host key. Verify its fingerprint through a trusted channel before accepting it. Do not disable host-key checking just to make the command proceed.

Why specify -i?

Without -i, ssh-copy-id may use keys loaded in your SSH agent. If no agent keys are available, it may select a matching public-key file from ~/.ssh. That default can be surprising when you have several identities.

Use -i to make the key being installed explicit. It points to the public key. The SSH client needs the corresponding private key later when you connect. If you need help creating a key pair, see this guide to generating SSH keys across Windows, macOS, and Linux.

You can preview what would be installed with a dry run:

ssh-copy-id -n -i ~/.ssh/id_ed25519.pub user@host
Enter fullscreen mode Exit fullscreen mode

The dry run may still contact the server to check which keys already work, but it does not write the key to authorized_keys.

Avoid using -f just to get past an “already installed” message. Forcing installation skips duplicate detection and can add duplicate lines. If the key is already accepted, verify it rather than appending it again.

Windows PowerShell: append the public key over SSH

Windows’ built-in OpenSSH client includes ssh, but not the Unix ssh-copy-id script. To install a key from PowerShell on a Linux server, send the public-key contents through an SSH connection:

Get-Content "$env:USERPROFILE\.ssh\id_ed25519.pub" |
  ssh user@host 'umask 077; mkdir -p ~/.ssh; cat >> ~/.ssh/authorized_keys; chmod 700 ~/.ssh; chmod 600 ~/.ssh/authorized_keys'
Enter fullscreen mode Exit fullscreen mode

This command expects a Unix-compatible shell on the remote server. It appends the key rather than replacing the file, but it does not check for duplicates. Running it repeatedly may add the same key more than once. The chmod commands set restrictive permissions on the remote account’s .ssh directory and authorized_keys file.

Windows as the SSH server is a different setup: Microsoft OpenSSH uses Windows paths and ACLs, not Unix chmod permissions. For connecting from Windows in general, see this PowerShell SSH connection guide.

Test the exact key you installed

Do not rely on a plain ssh user@host test if multiple keys or agent identities are available. Specify the matching private key and disable password authentication for this test:

ssh -i ~/.ssh/id_ed25519 \
  -o IdentitiesOnly=yes \
  -o PreferredAuthentications=publickey \
  -o PasswordAuthentication=no \
  -o KbdInteractiveAuthentication=no \
  user@host 'echo PUBLICKEY_OK'
Enter fullscreen mode Exit fullscreen mode

For a non-default port, add -p 2222. If the private key is protected with a passphrase, SSH may ask for that passphrase; it is not the remote account password.

A PUBLICKEY_OK response confirms that this test authenticated using public-key authentication. If it fails, run SSH with -v to inspect which identity is offered and whether the server accepts it:

ssh -v -i ~/.ssh/id_ed25519 -o IdentitiesOnly=yes user@host
Enter fullscreen mode Exit fullscreen mode

If the key is installed but rejected, check that the public and private keys match, the remote account is correct, and the server can read the account’s authorized_keys file. On a typical Unix server, .ssh should be owned by the account and commonly have mode 700; authorized_keys commonly has mode 600. SSH server policy and ownership checks matter too, so correct file modes alone do not guarantee acceptance.

The key rule is straightforward: install the .pub file, keep its private counterpart on your client, and verify the connection with that specific private key. Keep an existing working session open until the new login test succeeds.

I originally published a more detailed version of this guide on the ShadeTerm blog.

I'm also building ShadeTerm — an SSH client where every server gets its own workspace for terminals, SFTP, code, and databases.

Top comments (0)