Ever wanted to run a script where you use SSH? Maybe you were SSH/SCP/SFTP’ing to multiple different servers and did not want to type in a password over and over again?
Maybe you wanted to run a script that was set up on a Cron, and could not make it run because it was asking for a password at 3:00am in the morning when no one was around…
Here is how you do it!
Step 1 – Connect to the server manually
First you want to make sure that you have connected to that server at least once before. If you HAVE connected at least once before, then you can skip “Step 1″. If you have not, you should see something like this:
$ ssh username@www.address.com
The authenticity of host 'www.address.com (###.###.###.###)' can't be established.
RSA key fingerprint is xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx.
Are you sure you want to continue connecting (yes/no)?
Go ahead and say yes, connect and make sure that your password was correct. Once you have done then, exit out from the remote server by typing:
$ exit
Step 2 – Getting the Authorized Key
You should now be back on your main server. This is the server will you will want to run your scripts from. This is be the “launch point” to connect to other servers without using a password. To do this, we need the “key” for the server we want to connect to. Type the following:
$ cat ~/.ssh/id_dsa.pub
This will result is a one more more crazy long strings. They should look something like this, but much longer:
ssh-dss ABACBFD2++fds3NzaC1kc3MAAACBAPsKpv7Ok3kepS2z+tnuc0t8FLfNc3ARNJRq0Z+hQD ... username@www.address.com
* NOTE: You might have a lot of these in the file. The way to tell which string you want to use is to look at the END of each of the strings. You will see the “username=server”, and that will let you know which one you want to copy and paste.
Copy that long string into your clipboard.
Step 3 – Connect to the remote server
You now need to connect to the remote server again. Go ahead and ssh in:
$ ssh username@www.address.com
Once on the remote server, you need to to go to the hidden .ssh folder in home directory of the username you logged in with.
$ ssh ~/.ssh/
*NOTE: This directory may not exist. If it does not, don’t worry, you can simply create it and then move into that directory by typing:
$ mkdir ~/.ssh/
cd ~/.ssh/
Step 4 – Update the authorized_keys file
Now we need to either create (if it does not yet exist) or update (if it does exist) the “authorized_keys” file.
$ sudo vi authorized_keys
Once in the file, you can PASTE the long string that we got from “Step 2″.
Save, Quit the file, and exit out from the remote server
$ exit
Step 5 – Testing the new setup
All that needs to be done now is to test if your new password-less connection works. You can do this by simply typing:
$ ssh username@www.address.com
This should put you on the remote server without having to type in a password. Your Crons, and scripts should now be able to log in to that server without a password as well!
Congrats and have fun!
Read Also: