One of the ways to get around login issues with ssh
, scp
, and sftp
(all use the same protocol and sshd
server) is to create public/private key pairings.
Some servers may disallow this, but most sites don't. These directions are for Unix/Linux/Mac. As always, Windows is a wee bit different although the cygwin environment on Windows does follow these steps.
- On your machine, create your public/private key using
ssh-keygen
. This can vary from system to system, but the program should lead you through this.
- When
ssh-keygen
is finished, you will have a $HOME/.ssh
directory on your machine. This directory will contain a public key and a private key. There will be two more files that are generated as you go along. One is known_hosts
which contains the fingerprints of all known hosts you've logged into. The second will be called either authorized_keys
or authorized_keys2
depending upon your implementation.
- If it's not there already, log into the remote host, and run
ssh-keygen
there too. This will generate a $HOME/.ssh
directory there as well as a private/public key pair. Don't do this if the $HOME/.ssh
directory already exists and has a public and private key file. You don't want to regenerate it.
- On the remote server in the
$HOME/.ssh
directory, create a file called authorized_keys
. In this file, put your public key. This public key is found on your $HOME/.ssh
directory on your local machine. It will end with *.pub
. Paste the contents of that into authorized_keys
. If authorized_keys
already exists, paste your public key in the next line.
Now, when you log in using ssh
, or you use scp
or sftp
, you will not be required to enter a password. By the way, the user IDs on the two machines do not have to agree. I've logged into many remote servers as a different user and setup my public key in authorized_keys
and have no problems logging directly into that user.
Doing Private Public Key Authentication on Windows
If you use Windows, you will need something that can do ssh
. Most people I know use PuTTY which can generate public/private keys, and do the key pairing when you login remotely. I can't remember all of the steps, but you generate two files (one contains the public key, one contains the private key), and configure PuTTY to use both of those when logging into a remote site. If that remote site is Linux/Unix/Mac, you can copy your public key and put it into the authorized_keys
file.
If you can use SSH Public/Private keys, you can eliminate the need for passwords in your scripts. Otherwise, you will have to use something like Expect or Perl with Net::SSH which can watch the remote host and enter the password when prompted.