O'Reilly logo

bash Cookbook by Cameron Newham, JP Vossen, Carl Albing

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using SSH Without a Password

Problem

You need to use SSH or scp in a script and would like to do so without using a password. Or you're using them in a cron job and can't have a password.[1]

Warning

SSH1 (the protocol) and SSH1 (the executables) are deprecated and considered less secure than the newer SSH2 protocol as implemented by OpenSSH and SSH Communications Security. We strongly recommend using SSH2 with OpenSSH and will not cover SSH1 here.

Solution

There are two ways to use SSH without a password, the wrong way and the right way. The wrong way is to use a public-key that is not encrypted by a passphrase. The right way is to use a passphrase protected public-key with ssh-agent or keychain.

We assume you are using OpenSSH; if not, consult your documentation (the commands and files will be similar).

First, you need to create a key pair if you don't already have one. Only one key pair is necessary to authenticate you to as many machines as you configure, but you may decide to use more than one key pair, perhaps for personal and work reasons. The pair consists of a private key that you should protect at all costs, and a public key (*.pub) that you can post on a billboard if you like. The two are related in a complex mathematical way such that they can identify each other, but you can't derive one from the other.

Use ssh-keygen (might be ssh-keygen2 if you're not using OpenSSH) to create a key pair. -t is mandatory and its arguments are rsa or dsa. -b is optional and specifies the number ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required