Generate the ssh-key run the command in your terminal
then either rename your key or type enter to continue with the generic given name which is id_rsa for private key and id_rsa.pub for public key
ssh-keygen -t rsa
If the ssh key is meant for GitHub, go to settings look for ssh keys and gpg keys, and paste the copied keys after running the cat command here. Then you are done. You can now use the key to access your GitHub.
If the key was meant to be a server key then ssh into your server using your root password and run the following commands in root.
mkdir /home/user/.ssh touch /home/user/authorized_keys sudo nano /home/user/authorized_keys
Run the following command on your local machine:
This will give you the public which you copy and paste into the authorized_keys file after the sudo nano command opens the file.ctr + x, you will be prompted if you want to save the file type 'Y' for yes then enter to save. Now exit the server by typing exit then ssh into the server as shown below:
Replace the user with your username and the IP address with your IP and it should automatically ssh you into your server without needing the password.
Type the following commands in your bash terminal to open the file
sudo nano /etc/ssh/sshd_config
After which navigate to the section similar to as below and change the yes to no.
#LoginGraceTime 2m PermitRootLogin yes #StrictModes yes #MaxAuthTries 6 #MaxAuthSessions 10
After changing the option to no CTR + x a prompt of whether you want to save the new file will appear type 'Y' then enter to save.
Reload the saved file by typing the command below and
sudo systemctl reload sshd
then you are done no one can ssh using the root password to your server. This is mainly to protect against brute force attacks into your server.
You will mainly do this when you want to be able to access other commands like git cloning or pull from Github or GitLab without being prompted for the password each time.
First, confirm that the user created in your server owns the ssh file.run
then look for the ssh file
edwin edwin 807 Sep 7 11:09 .profile
2 root root .ssh
As you can see my file is owned by the root, hence if I try to generate any ssh key I will get a permission denied error. I have to change ownership from root to the user I created. I do this by running the commands below in the terminal.
sudo chown -R edwin:edwin /home/edwin
If you run ls -la again you will notice the ssh file switched ownership from root to your current user. i.e mine is now edwin edwin .ssh
After this is done you can run in the terminal:
ssh-keygen -t rsa
You will get prompted with a file path in case you want to rename the ssh file type if you don't need this, otherwise, type the same path.i.e /home/user/.ssh/id_rsa_github.
Then type enter and enter a passphrase in, this is just for some extra security or you can skip this step by just pressing enter again until you see the weird figure below.
The key's randomart image is:
| ..+ |
| ..o. |
| OoE+.o |
| o=B==* |
| =+SBo* |
| o *+oo |
| o + .o |
| . o =. . |
| . +o |
eval `ssh-agent -s`
This will add the Identity the cat the id_rsa_github.pub or whatever you named your public key by running the command below.
copy this whatever is inside the file and paste it again to GitHub.
and now you are done.