How do I set up SSH keys on a Linux/Unix system?

Assuming you have SSH installed and in use on both ends…

The connection is user-based. So, (for the basics) everything you do, works only with the according user. With one exception: the general SSH settings that tell the SSH daemon to use keys instead of passwords.

You might want to log in on your server first to prevent trouble with the login, once you have created the key. Log in with the user you are usually going to access first. Of course, that is not going to be root. SSHD will prevent that in most cases anyway. Hopefully. You can always use something like sudo or su to switch to root, but you should never allow root access from the outside. In the users home directory go to the .ssh directory. In it you will either find a file authorized_keys or otherwise create one. Open it and just leave it open for now.

Since home users would usually use only one default user, you are likely logged in with that on your client anyway. Open your console and use ssh-keygen to generate a key. You might want to research in the manual for that, since there are different encryption options and key lengths. In terms of files you might want to use the default settings.

Once that is done, you are going to find a .ssh directory in the users home directory and in that a few files. One of them being a .pub file. The content of this .pub file is the public key. This is what you now copy and paste over into the authorized_keys file on the server. Save the file.

Once that is done, on the server go to /etc/ssh and edit the sshd_config. I’m not going to go over that in detail at this point. But use your best judgement and in case you miss something or are not sure, research the according setting. Shouldn’t be too hard, if you know your basics. When you are done, save it and reload the SSH daemon on the server with “systemctl reload sshd”.

That should do the trick. But do yourself a favor and don’t log out yet. Test if it worked. Use a second connection to try. Again, you are going to have to use the users where the keys are stored (not saying, there are no other options. But generally, that’s how it works). On both ends. After that you might also want to take a look at the logs. If it worked, instead of reload use restart to restart the daemon and try again.

Why? Because reload only loads the new settings but skips it in case there are errors. This is used for uninterrupted server services. Once it works, you can take the risk and restart the service to see, if everything is really working and the settings are there. Just to make sure. Try it again. Everything should be working. As long as you are testing, stay logged in with the first connection. This allows you to make adjustments in case something goes wrong. Once you log out or get logged out, you might have a problem, if the settings didn’t work!

============================================

If this answer is helpful for you, please don’t forget to upvote it on Quora:

https://www.quora.com/How-do-I-set-up-SSH-keys-on-a-Linux-Unix-system/answer/Chris-Bailey-364

error: