Question

How does SSH find a local key?

This is a really simple question about SSH. I’m new to this concept and struggling to figure it out.

I get that an SSH key has to be registered on the server and stored on the host computer. But how does the server find the file on the local machine? How does it know the filepath for the .pub file? I could store this key in any local folder, right?


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

The server doesn’t find the key on the local machine, the client on your local machine sends it.

On your local machine (not the server). Your user will have (or should have) a .ssh directory. You can find it by typing cd ~/.ssh. In that directory you will have a file called config. This is where you store all of the information about which private (not public) key to use when you try to connect to a remote server. The public key should be added as a single line to a remote server’s authorized_keys file. Just think of it as you keep your private key private and your public key is safe to share. You will want to make sure permissions are set right on the .ssh directory if you have any connection problems. Specifically the IdentityFile line in the config file is what you will need to set if you want to have multiple keys. Example config file:

Host *
# Forward ssh agent to the remote machine.
# This allows you to use services like GitHub on
# all remote servers
ForwardAgent yes

# example.com server
host example 123.123.123.123
hostname example.com
user remoteusername
IdentityFile ~/.ssh/specific_private_key

You can then connect using ssh example instead of ssh remoteusername@example.com or ssh remoteusername@123.123.123.123. You can also add specific ports if necessary. Type man ssh_config for all options.