Password-Free SSH

It surprises me sometimes how often I see people typing a password every time they login to a remote system with SSH. For someone working on a cluster or supercomputer, this can be many times a day. Not only that, but it makes it difficult to automate remote commands and scripts when you are not present at the keyboard, such as via launchd or cron. Setting up SSH for password-free access can be a real time saver, but can be a bit confusing, which probably explains why so many don't bother. So here is the quick tour of password-free SSH configuration.

Option 1: No Passphrase

The easiest option to getting password free access is to use the RSA2 protocol, and not enter a passphrase when creating your SSH keys. (Don't confuse the passphrase with your account password — they are two separate entities.) You can enter a passphrase for each set of SSH keys you generate; it can be longer than a password, and can include spaces. But if you create a set of keys without any passphrase, you will be able to login without typing a password or passphrase.

First create the keys

ssh-keygen -t rsa

This will prompt you for where you want to keep the key files. You can just use the default by pressing enter.

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/drew/.ssh/id_rsa): 
Created directory '/Users/drew/.ssh'.

You will then be asked for a passphrase. Just press enter, and press enter again when reprompted.

Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/drew/.ssh/id_rsa.
Your public key has been saved in /Users/drew/.ssh/id_rsa.pub.

Now we need to tell SSH that the key just created is authorized for access. To do this, add the ~/.ssh/id_rsa.pub content to the ~/.ssh/authorized_keys file. If the file doesn't yet exist, you can simply copy the id_rsa.pub file:

cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys

Now we need to copy all of these files to the remote machine.

scp -r ~/.ssh username@remote.host.com:

You should see the following

The authenticity of host 'remote.host.com (10.0.0.10)' can't be established.
RSA key fingerprint is 0a:3f:10:2c:22:18:03:93:0a:58:ee:cc:2c:d9:e5:7f.
Are you sure you want to continue connecting (yes/no)? yes

Enter yes at the prompt, and then enter your remote account password when prompted.

That's it. You should now be able to login to the remote machine without a password.

ssh username@remote.host.com

You may be thinking that not having a passphrase is a security risk. Yes and no. Yes, it isn't as secure as having a passphrase, but it is still very secure, because for anyone to login to your remote account, they need to have your private key, and to get that, they need to be logged into your account. If they are logged into your account, you have a serious problem anyway.

Option 2: Command-line SSH Agent

If you just can't bring yourself to have an empty passphrase, there is another option available to you: an SSH agent. In this case, create the keys as above, but enter a passphrase. Once you have a set of keys, start up an SSH agent:

ssh-agent

You should see output like this

SSH_AUTH_SOCK=/tmp/ssh-IOCUDuxaPz/agent.2564; export SSH_AUTH_SOCK;
SSH_AGENT_PID=2565; export SSH_AGENT_PID;
echo Agent pid 2565;

These are commands that you can use in your shell to set the environment up so that the agent is used. Because you probably want to execute these immediately after starting the agent, you could use this command to start the agent instead:

eval `ssh-agent`

That will evaluate the output of the command, setting up the environment as appropriate.

You now need to add your keys to the agent. To do this, use ssh-add

ssh-add

This will prompt you for any passphrases you are using. When you are finished, you should be able to login to the remote machine from the shell without typing a password or passphrase.

There is one serious drawback to this: the agent only works for programs that have their environment configured properly. Each new shell will need to be configured with the environment variables that were printed by the ssh-agent command. You can do some tricky stuff in your resource scripts to do this. For example, when starting the SSH agent, you could dump the output to a file:

ssh-agent > ~/.ssh-agent_config

And then, in your shell resource script (eg .bashrc), you can source this file:

source ~/.ssh-agent_config

This should work for your shells, but it won't help you much for using password-free SSH in applications like Xcode. For that, you will need...

Option 3: SSH Agent Applications

Probably the best way to use SSH agent is to use it via an application like SSH Agent. Such applications start up the SSH agent for you, and set environment variables at login such that all applications have access to the agent. You can then use SSH with Xcode, for example, to update code from a Subversion repository.

There are several applications that do this. A quick search of VersionTracker reveals a number of different candidates, and I recommend you try them out and see which fits your needs best. They all do basically the same thing: at login, environment variables are set so that SSH knows to query the application for passphrases. The application adds your SSH keys to the agent when it is launched (like ssh-add), and then supplies SSH with passphrases when requested.

Conclusions

That's it in a nutshell. Option 3 is probably the most robust, and is not too difficult. If you are very lazy, you could get away with Option 1. The only option I recommend against is Option 0, because typing your password every time you login is a frightful waste of time and energy, and limits your possibilities in regard to remote automation.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

No Passphrase...

I just recently set up a number of machines for passphrase-free SSH access because I was setting up OpenMPI. One thing I forgot to do on one machine was to manually ssh to that machine for the first time and manually say "yes" to

The authenticity of host 'remote.host.com (10.0.0.10)' can't be established.
RSA key fingerprint is 0a:3f:10:2c:22:18:03:93:0a:58:ee:cc:2c:d9:e5:7f. Are you sure you want to continue connecting (yes/no)? yes

To find out why OpenMPI was not working on that machine cost me a few hours...

So just to save you the hazzle: Try it out manually before running scripts and programmes that use SSH :-)

SSHKeychain

I use an application called SSHKeychain http://www.sshkeychain.org. It works well and can also be configured to automatically open SSH tunnels for port forwarding.

-Mark

Serious security issue with Option 1

In option 1, you have said:

>Now we need to copy all of these files to the remote machine.
>scp -r ~/.ssh username@remote.host.com:

This will cause your private key to be copied as well, so anyone with access to ~/.ssh/ on the remote machine can then copy it and use it to login as you. Specifically, the private key is in ~/.ssh/id_rsa, (no .pub) and you should be guarding this file carefully.

You should just copy the contents of ~/.ssh/id_rsa.pub into the remote system's ~/.ssh/authorized_keys , and nothing else.

Regards,
Josh

Apple Keychain?

@Josh:This will cause your private key to be copied as well, so anyone with access to ~/.ssh/ on the remote machine can then copy it and use it to login as you. Specifically, the private key is in ~/.ssh/id_rsa, (no .pub) and you should be guarding this file carefully.

You are right. But I think Drew does mention this risk at the end of Option 1??

@Drew: You may be thinking that not having a passphrase is a security risk. Yes and no. Yes, it isn't as secure as having a passphrase, but it is still very secure, because for anyone to login to your remote account, they need to have your private key, and to get that, they need to be logged into your account. If they are logged into your account, you have a serious problem anyway.

Anyway..Mark mentioned SSHKeyChain earlier. SSHKeyChain's website mentions that it integrates with Apple Keychain, so it would be a good option.

Copying private keys

Hi Josh,

You are right that anyone on the remote machine with access to your .ssh directory could then access your Mac. (That's exactly why I copy the key, so that I can access the Mac from the remote machine.) I am assuming that the only people with that access are system administrators, and that you trust them. If you don't, you have a problem anyway.

Security is a double edged sword: too little, and you can be hacked, too much, and you can't work efficiently. If you can't trust the system admin on your cluster or supercomputer, you should not put your private key there, true, but if you do, I don't see a big risk.

I always work with the same set of keys on all systems. It's just a lot easier that way. I can just copy .ssh and I'm done. That way I can not only login to a supercomputer from my Mac, but also log back into the Mac (or, eg, scp a file) from the supercomputer. You could do this with separate private keys for each machine, but I don't think it is worth the trouble. If any of your accounts are compromised, you have a serious problem anyway. Having separate private keys may stop the hacker getting to your other accounts, but to me it's not worth the trouble. It's up to you what you think is worth the risk.

Drew

---------------------------
Drew McCormack
http://www.maccoremac.com
http://www.macanics.net
http://www.macresearch.org

scp

The scp step can also be done by ssh, if you are intending to send only one kye from one machine to the other:

cat ~/.ssh/id_rsa.pub | ssh remote_server "cat - >> ~/.ssh/authorized_keys"

Paulo

All true... :)

I do agree with you. My only thought was that there are often situations where you wouldn't totally trust the other machine and its sys-admins. Hosting providers, university accounts, etc. In those situations it's fine to put your public key on the other machine but not the private key.

I guess what I should be doing is using SSHKeychain instead... time to add a new tool to the box.

In any case, thanks for the article, it was a good overview.

ssh password woes

option one is what ive been doing quite happily for aix,linux, etc but for macs it still keeps saying password: :O(

so i went through it clean on a second mac and guess what same thing there. fyi i do:

cd .ssh
ssh-keygen -t rsa -f ~/.ssh/id_rsa_$hostname)
cat *.pub > authorized_keys
> known_hosts
ssh $(hostname)

... and get this

RSA key fingerprint is 03:4f:f4:7d:ba:3b:24:66:58:5a:f7:0b:dc:5a:cd:fd.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'spmac2.home,192.168.1.5' (RSA) to the list of known hosts.
Password:
Last login: Mon Jun 4 02:46:34 2007
Welcome to Darwin!
spmac2:~ james$ logout
Connection to spmac2.home closed.
spmac2:~/.ssh james$ ssh $(hostname)
Password:
Last login: Mon Jun 4 02:49:45 2007 from spmac2
Welcome to Darwin!
spmac2:~ james$

confused? i am

ssh password woes

option one is what ive been doing quite happily for aix,linux, etc but for macs it still keeps saying password: :O(

so i went through it clean on a second mac and guess what same thing there. fyi i do:

cd .ssh
ssh-keygen -t rsa -f ~/.ssh/id_rsa_$hostname)
cat *.pub > authorized_keys
> known_hosts
ssh $(hostname)

... and get this

RSA key fingerprint is 03:4f:f4:7d:ba:3b:24:66:58:5a:f7:0b:dc:5a:cd:fd.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'spmac2.home,192.168.1.5' (RSA) to the list of known hosts.
Password:
Last login: Mon Jun 4 02:46:34 2007
Welcome to Darwin!
spmac2:~ james$ logout
Connection to spmac2.home closed.
spmac2:~/.ssh james$ ssh $(hostname)
Password:
Last login: Mon Jun 4 02:49:45 2007 from spmac2
Welcome to Darwin!
spmac2:~ james$

confused? i am

RE: ssh woes

This probably has to do with the ssh configuration on the remote mac you are trying to log into. By default, the Mac has pretty strict restrictions on SSH. You can change them by editing /etc/ssh_config on the remote machine, or adding a .ssh/config file in your remote account directory.

Either way, I think you probably need to add this:


HostbasedAuthentication yes

You may also need to add this line to the /etc/ssh_config file:


EnableSSHKeysign yes

More information about configuring ssh can be obtained by entering 'man ssh_config'

Drew

---------------------------
Drew McCormack
http://www.maccoremac.com
http://www.macanics.net
http://www.macresearch.org