If like me, you have a lot of *nix boxes that you login to from one machine, typing your password over and over can be a pain. Also, if like me, you automate jobs on remote machines, SSH can be a great way to go. However you probably don't want to hang out until 1AM when no one is around to type in the password to execute that job via SSH. It is quite simple to have SSH automatically login for you and run your job, or just log you in so you can work at the console.
I've used this on various *nix platforms and OSX with great success.
The first thing we need to do is setup some keys to use. We're going to generate the public and private key pair for SSH to use on your local machine or the machine you want to initiate the SSH session from.
Command breakdown: ssh-keygen is the command we are using. The "-b" option specifies the number of bits used in the key. The "-t" option specifies the type of key pair that we are going to create. In this example we are using DSA.
NOTE: There are a variety of different types of key pairs that we could use and there is quite a discussion we could get into about this, but that definitely goes above and beyond the scope of this how-to.
When you run the above command you will receive the following promps:
Pressing "enter" at all the above prompts will work fine. One thing to keep in mind would be that if you do enter a passphrase you will be required to enter that passphrase before you can use your key.
After our keys are generated, use your preferred method of transferring your public key (in this example the public key is called id_dsa.pub and is kept in /home/user/.ssh/) to the remote machine. Rsync, scp, whatever you prefer...
Once the key exists on the remote machine login there and get to a command prompt. We are now going to copy the public key from the orinal machine into a place that lets this machine know that it is an accepted host. Run the following command to do so.
Command breakdown: Cat, probably no explanation needed there... In this case, the user needs to be the user you are going to login as and we are appending the contents to the authorized_keys file.
As a final step let's make sure the permissions on this users .ssh directory are correct.
That is it! You should be able to login to this machine from the original with no password prompt and you should also be able to run commands on this machine from the origina via SSH with no password needed.
Post new comment