Menu

Make SSH Use the Right Key with IdentityFile
📰

Make SSH Use the Right Key with IdentityFile

DEV Community: tutorial·Nate·1 day ago
#CmzVbHie
#devtotutorial#devto#dev#host#server
Long article (899 words) — no AI summary yet.
Reading 0:00
15s threshold

When you manage more than one server or SSH key, a connection can fail even though the right private key is on your machine. OpenSSH may be using a different identity, or the host settings you expected may not match the destination.

IdentityFile lets you associate a private key with a host in your SSH client configuration. Once set up, you can connect with a short alias instead of repeating -i , the username, and the server address each time.

Turn a working command into a host alias

Start with a connection that already works. For example:

ssh -i ~/.ssh/id_ed25519_prod [email protected]

Enter fullscreen mode

Exit fullscreen mode

Add a matching host block to ~/.ssh/config :

Host production HostName server.example.com User deploy IdentityFile ~/.ssh/id_ed25519_prod IdentitiesOnly yes

Enter fullscreen mode

Exit fullscreen mode

Now connect with:

ssh production

Enter fullscreen mode

Exit fullscreen mode

Host is the shortcut you type; HostName is the actual server address.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.