SSH Login without Password
First of all I want to say that there is no way that you can remotely login into the shell of a secure remote-server without some kind of password.
So let’s start by giving a summary about what we will be doing here, well first we will remotely login that remote-server via ssh then we will leave our identity in that remote-server. As we left our identity on that server, that server will let us login without a password. Looks simple, right? So let’s get started with the tutorial.
What is SSH and How does it works.
Well to put it simply, SSH or Secure Shell Protocol is used to remotely access, control and modify your remote servers over the internet or over your local network.
As for the working of ssh you have two options in windows:
- First is to use Putty
- And second is to use a simple command in the command prompt that is..
ssh {user}@{host}
And for linux and mac you should use a terminal.
And if you want to know more about SSH and its working, you can refer to this tutorial by hostinger.
Let’s see what is happening behind the scenes.
(Disclaimer: If you are in a hurry this section is just knowledge of this and that. So, you should move to the next section by just hitting this link.)
So let’s start by remotely logging into the server (“logging into” don’t know if it is the right phrase..lol😂) and for that let’s enter the command.(I will be using terminal).
ssh {user}@{host}
This is the example of how you should use the command. And yup, I know the username that I assigned to my server is kind of cringy, but….you know what, It’s my choice and I will assign whatever the hell I want. Anyway let’s move on with the tutorial.
So, after you will enter the command the output will be something like this
Note that this type output will only be shown when you log into the server for the very first time. And for what you should do now. Well, just type “yes” and move on.
If you want to know more about this then just google it man, as this is some topic for another tutorial.
After entering “yes” for an answer, the output would be something like this.
Now just enter the password of the server and hit enter
And after entering the password you would see something like this.
Ok ok, I know you are not here for this type of ssh tutorial. So, let’s just logout by typing command logout
and login again, but this time with a -v
flag like this.
ssh -v superadmin@10.16.10.67
And the output would be something like this.
Now don’t worry about this pretty big mess.
And focus on this part
As you can see, our client is trying some keys on the server, but as we haven’t created any keys yet, the attempt failed. Thus, the client is asking for a password.
So, just hit “ctrl + C”, and move to the next step.
Generating SSH key
From this section onwards it’s a fun ride.
Now, to generate a ssh key we just have to type a simple command on the client side terminal and that is
ssh-keygen
And the output would be something like this
Here, it is asking if you want to save the key somewhere else with some other name. But, we will be going with the default location and default name, so just hit enter and wait for the next output.
In this output, it is asking for a passphrase for the key. To put it briefly, it can be used for setting a separate password for your ssh connection on other devices. I will go with no passphrase so I’ll just leave it empty.
And yes, there will be a confirmation after you hit enter, so just leave it empty, if you are also going with “no passphrase”.Else, you will have to enter the passphrase that you chose.
Now, as you can see our SSH key is generated. So, let’s move to our “NEXT STEP”, that is.
SCP: copy files over ssh
Before going to the copying part, I would like to briefly introduce scp
. For this tutorial, you should only know that scp
is a command that will help you to copy files over the internet or local network via ssh. And there is an example on how to use the command.
scp {file-you-want-to-copy} {user}@{host}:/
The section of copying and pasting.
Ohkayi, so this step is pretty simple I think. For this step you have to change your directory to .ssh/
directory which will be located in your home directory. And then just type the ls
command. Just like this.
As you can see, there are two files with the name id_rsa
. The first file, with name id_rsa
but no extension is a private key, that you obviously should not give to anyone. And the second one, with the .pub
extension is the public key that we will be giving to our server.
So now, we will be using scp
to copy id_rsa.pub
to our server, like this.
And yep for now it will surely ask for a password.
Now you can login into the server’s home directory for the user(for me that is superadmin’s home directory). And hit ls to check if the ssh key is present.
So, my SSH key is certainly present. Then I think it’s time to move to the next section.
Let’s set the authorized_keys on the server side .
Now, if this is your first key in the remote server, then follow along else skip to the next part of the section.
If this is your first key, then there are more chances that you will not have a .ssh
directory in your home directory. Then let’s start by creating one, by using this command in your home directory.
mkdir .ssh
After creating the .ssh
directory, we will be renaming the id_rsa.pub
to authorized_keys
using this command.
mv id_rsa.pub authorized_keys
Now as we are done with renaming the file, we just have to move this authorized_keys
file to the .ssh
directory like this. And we are done.
mv authorized_keys .ssh/
Ohkayi, so what will you do if you want to save more than one key? Let’s see the procedure for that one.
Now, for adding another key you just have to use this command.
cat id_rsa.pub .ssh/authorized_keys
So, we are done.
After completing all these steps, you just have to log out of your server, and login again. But this time your server will not ask for a password.
Conclusion
Now, I have shown you this amazing way to login into your remote server via SSH but without the need of entering password again and again. I wish you a happy coding journey😊✌️.
And, if you have any questions or remarks, please leave a comment below and if you really liked my work then you can buy me a coffee.