Running Commands on remote machine without a passoword
Linux and Windows
OpenSSH to OpenSSH (ssh2)
Check that ssh functions at all: SSH can replace telnet even without keys. Suppose you are connecting to the remote computer remote.com. as user “testacct”. Run
$ ssh testacct@remote.com
and ssh will ask if you want to keep connecting, type “yes”, and then it should ask for your password and open a shell in testacct’s home directory on remote.com, just like telnet. If this fails, there is a problem somewhere. Make sure ssh is installed on your end, and also make sure that remote.com is accepting ssh connections. If it’s not, you’re wasting your time.
Once ssh is functioning we will set up the keys so that it will no longer be necessary to send passwords. If you are curious about the theory of this then read up on “public key cryptography”.
Create your keys: You need to create private and public ssh keys and put them in the proper place with the proper permissions. In your home directory create a folder .ssh ($ mkdir .ssh), if there is none. Note that Windows may make it difficult for you to create a file starting with “.” if you try to do it with their tools; e.g. Windows Explorer. Next, create the keys with the command
$ ssh-keygen -t dsa
The ssh-keygen program will ask for a passphrase, just hit the “Enter” key unless for some reason you know you want a passphrase. This creates the keys id_dsa and id_dsa.pub and puts them in .ssh/. The private key id_dsa must be readable only by you; change its permissions with
$ chmod 600 .ssh/id_dsa
Put the public key on the remote computer: In this section we are assuming the remote computer is also running OpenSSH. Somehow, you must get the .ssh/id_dsa.pub key onto the remote computer, whether by email, ftp, carrying it over on a floppy (sneakernet), etc.; the cool way to do it is to use scp, which was installed along with ssh. Suppose the remote computer is named remote.com, and your account there is “testacct”. To copy the file to remote, run
$ scp .ssh/id_dsa.pub testacct@remote.com:
Don’t forget the trailing colon. You will be asked for testacct’s password on remote before the copying commences. The file will be copied to testacct’s home directory on remote.
Install the public key on the remote computer: (We assume the remote computer is running OpenSSH on Linux or UNIX!) Once id_dsa.pub is on the remote computer, login into the remote computer (you can use ssh to login with your password as described above). From your home directory (where you should see your newly arrived id_dsa.pub) create a .ssh folder if none exists. Then append your id_dsa.pub to a file in .ssh with
$ cat id_dsa.pub >> .ssh/authorized_keys
This will create the file authorized_keys if none exists. The id_dsa.pub key may be removed from the remote computer’s home directory, if you like. The .ssh folder on the remote computer must have the correct permissions, you may set them with
$ chmod 700 .ssh
Checking the password-less connection: Now the command
$ ssh testacct@remote.com
should give you a password-less connection to remote.com. Likewise, scp should be password-free.
By the way, all the commands you do by first logging into the remote computer can be done remotely, one at a time, using ssh. For example, you can run run “$ ssh testacct@remote.com ls” and get a listing of your home directory files on the remote computer. See the documentation for details.