using ssh-agent

Sometimes it’s useful to connect to many different lab machines at the same time. Having to type in your password or passphrase for each connection would be a pain. ssh-agent is a program that stores your passphrase and provides it automatically for you when logging in.

Here’s how I use ssh-agent when I first connect to a CS machine (details explained below):

$ ssh-agent bash
$ ssh-add
Enter passphrase for .....:

The above sequence runs ssh-agent as the parent of the bash shell, so anything done in that bash shell uses the agent. I then add my ssh key to the agent, which stores it for future use.

As a result of running ssh-agent and ssh-add, I can now ssh into any lab machine without having to type my passphrase!

For example, if I am on bacon and run the above commands, then ssh to carrot, then oil:

BACON[~]$ ssh-agent bash
BACON[~]$ ssh-add
Enter passphrase for /home/knerr/.ssh/id_rsa:
BACON[~]$ ssh carrot
Welcome to: carrot running 18.04
CARROT[~]$ exit
BACON[~]$ ssh oil
Welcome to: oil running 18.04
OIL[~]$ exit

Notice that I didn’t have to type in my passphrase when sshing to carrot or oil!

parallel-ssh

I usually do the above sequence (run ssh-agent and ssh-add) in the first terminal I create when I log in. After that I can use parallel-ssh or other programs to connect to multiple lab machines (without typing my passphrase for each login!). Here’s a simple example:

$ parallel-ssh -h /usr/swat/db/hosts.mainlab -i uptime 
[1] 09:46:04 [SUCCESS] cheese
 09:46:04 up 14 days, 12:23, 13 users,  load average: 0.02, 0.05, 1.00
[2] 09:46:05 [SUCCESS] lime
 09:46:05 up 14 days, 12:23,  0 users,  load average: 1.00, 1.00, 1.00
[3] 09:46:05 [SUCCESS] mushroom
 09:46:05 up 14 days, 12:23,  3 users,  load average: 2.00, 1.08, 0.04
[4] 09:46:05 [SUCCESS] caper
 09:46:05 up 14 days, 12:23,  0 users,  load average: 1.00, 1.06, 1.00
[5] 09:46:05 [SUCCESS] bacon
 09:46:05 up 14 days, 12:23,  0 users,  load average: 1.00, 2.00, 2.00
[6] 09:46:05 [SUCCESS] basil
 09:46:05 up 14 days, 12:23,  0 users,  load average: 1.04, 1.01, 1.00
....
....

In the example above I use parallel-ssh to connect to all machines listed in /usr/swat/db/hosts.mainlab (all machines in the main 240 lab), and I interactively (-i) run the uptime command (shows load, users, etc) on each machine.

adding tmux

This step isn’t needed, but can be very useful if opening up multiple terminals and windows (each subsequent window makes use of the ssh-agent with the stored ssh key included), or running long jobs (you can start your job, detach from the tmux session, log out, then re-attach at a later time).

$ ssh-agent bash
$ ssh-add
Enter passphrase for .....:
$ tmux

See also:


Back to SwatCS Help Docs