Apollo Connecting the World

Diligence and Intelligence

Do not type password when ssh to a remote machine

leave a comment »

Sometimes you may feel it annoying to type password to ssh onto a remote machine, especially when you are running an automated program which involves ssh to other machines and run commands. Here is a method that enable a user to ssh without typing password.

Principle:
a) Create a key pair on local machine
b) Put the publick key on the remote machine, keep the private key on local machine
c) When ssh to the remote machine, the private & public key matching will be done, and matching will lead to the logging

Assuming there are three machines. A: local machine. B,C: remote machines you want to ssh to.

1. On local machine A, run:
$ssh-keygen -t rsa -f ~/.ssh/id_rsa

This command will generate an rsa key pair. “id_rsa” is the private key, and “id_rsa.pub” is the public key.
(Or you can run: ssh-keygen -t rsa, and do according to the prompted info)

When you are asked to enter passphrase, simply leave it empty. (If you have ssh-agent on A, then you can enter the passphrase. Or you still have to enter this passphrase every you ssh to B and C)

Note:
You have to set the privilege of id_rsa on local machine to:600:
$chmod 600 ~/.ssh/id_rsa

2. Append the content of id_rsa.pub to “~/.ssh/authorized_keys” on remote machine B and C.
In a terminal of the local machine, type:
$cat ~/.ssh/id_rsa.pub | ssh yourname@machineB “cat – >> ~/.ssh/authorized_keys”

Or you can upload the public key to the machine B, and in machine B, do:
$cat id_rsa.pub >> .ssh/authorized_keys

Note:
Maybe machine B and C have to restart the ssh service:
$sudo /etc/init.d/ssh restart # on machine B and C
(works on Ubuntu, similar in other Linux)

3. If A has ssh-agent, and you entered a passphrase when generating the RSA key pair, then run:
$ssh-add
and do according to the prompted information.
And then you can ssh without entering password

Written by apollozhao

2010/03/03 at 13:47

Posted in Linux, ssh

Tagged with , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: