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?

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.

×
2 answers

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.

Submit an Answer