How to generate SSH key on Linux and macOS

These days every company has several servers and they all need administration, but to remember the password for each server is a problem and also sometimes you can lose your password. At the same time you cannot put one universal password for all servers it will be insecure and put your systems at risk. Also, if your company employe leaves you will need to go and change all passwords, so it is better to use SSH key's it will not only be secure but also will be much easy if you maintain many Linux servers. This SSH key also can be used on GIT servers and other systems, that support SSH connection with a key.

So let's start, open your Linux or macOS terminal. When opened copy this command and paste it in only replace e-mail with your mail

If you are generating key on your macOS then 1 step login as root by opening Terminal and typing sudo su  and pressing enter, after that it will require you to enter your password

ssh-keygen -t rsa -C "[email protected]"

It will ask you where to save the new key and what will be its name, if you create 1 key for 1 time then better use all default by pressing enter, but if you already have some key then Just retape the path and key name. But in this how-to, we leave the default
Linux:

Enter file in wich to save key (/root/.ssh/id_rsa):

macOS:

Enter file in wich to save key (/Users/usernameofyourmac/.ssh/id_rsa):

Now it will ask you to enter a passphrase for none press enter. Now I would recommend for security to enter one this will be requested when you will log in to the server, this passphrase is used to unlock your key and if someone, for example, steals your key, then it can't be used without the passphrase

Enter passphrase (empty for no passphrase):

It will ask to confirm your passphrase enter the same as above if you have entered something.

Enter same passphrase again:

End you are done, you have a valid key, that is made from 2 files 1.  File id_rsa (This is the key) and id.rsa.pub (This is what you give to others, that asks for your key or set it in configuration so that it recognizes you and makes a secure connection), let's view the pub key file
Linux:

cat /root/.ssh/id_rsa.pub

macOS:

cat /Users/usernameofyourmac/.ssh/id_rsa.pub

Now you see your key, to if you have set up it correctly then you and inserted the key into the server ( How to insert the key into the server) then typing ssh [email protected] and pressing enter you should log in to your server.

For the tutorial how to add your SSH key to Linux root user click here

For the tutorial how to add a new user with SSH key in Linux click here