How does SSH find a local key?

March 14, 2017 3.6k views
DigitalOcean Security Ubuntu 16.04

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?

2 Answers

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

  • Thank you! So, when I enter "ssh (ip-address) (password)" or something into the terminal, how does the computer find the .pub file associated with that server?

    • It doesn't have to be "associated", it just offers all the public keys and sees which is accepted.

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

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

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

Have another answer? Share your knowledge.