PublicKeyAuthentication has a weakness: if your private key is stored unprotected, anybody who gains access to your computer will be able to use your credentials to prove his identity and pretend to be you, gaining entry to machines that he should have no access to. Therefore the private key is stored to disk encrypted with a passphrase. To use the key, the SSH client must decrypt it, so it has to prompt you for your passphrase.
This makes PublicKeyAuthentication less convenient than password authentication: every time you log in somewhere, you have to type a long passphrase rather than a short password.
DO use passphrases. It's very tempting to use a passphraseless key so that you "don't have to type in a password every time". Instead, read on. Authentication agents provide a solution to this. OpenSSH's agent is called ssh-agent(1), PuTTY's is called Pageant. Typically, you launch the agent when you log onto your local machine, which prompts you for the passphrases of any keys you have. The agent then remains persistent and provide your credentials to any client that needs them, so you will no longer be prompted for the passphrase. When you log out, the agent shuts down.
Another good option for a 'trusted' box is keychain which will allow you to run cronjobs over ssh even when you are logged out.
NOTE: Do not run an agent on hosts you do not trust. Their SuperUser can then steal your keys.
This is what ssh-keygen(1) is for.
ssh-keygen -t dsa # or, effectively equivalent as far as SSH is concerned: ssh-keygen -t rsa
If you accepted the defaults for ssh-keygen(1) you should have two new files in ~/.ssh, id_dsa and id_dsa.pub (or id_rsa and id_rsa.pub). The .pub file is your public key. You transfer a copy of this key to all remote hosts that you wish to use your key pair with. The easy way to do so is by using ssh-copy-id(1):
ssh-copy-id -i ~/.ssh/id_dsa.pub hostname
This adds a copy of the public key in id_dsa.pub to the ~/.ssh/authorized_keys file on the remote machine, and makes sure that neither the ~/.ssh directory nor the authorized_keys file are group or world writable (if they are, sshd will refuse to read the file). Note that if you have a SSH agent running (see below) you can omit the "-i /.ssh/id_dsa.pub" switch and ssh-copy-id(1) will just add whatever keys are being held by the agent.
Next, on any local machine that you wish to SSH from, you must have the private key id_dsa (unless you forward an SSH agent; see below) and it must not be readable by anyone other than the owner. Obviously the directory and these files must be owned by the correct user. If the permissions are wrong, SSH will refuse to read them (without telling you, unfortunately – it only cries to syslogd(8)).
You can tell sshd(8) to allow a certain key to only be used by certain hosts or for certain activities. A brief summary of the available options is below. See sshd(8) for the more extensive documentation. These options are specified as a set of comma seperated options before the key in the authorized_keys file. Spaces are not allowed in an option unless the option is surrounded by double quotes.
Using the from keyword with a list of globs you can restrict which hosts are able to login using the key. Eg. the following will only allow this key to be used from localhost and hosts in the .example.com domain:
from="*.example.com,localhost" ssh-dss XXXX....base64..keyid....= username@host
You can also prefix a glob with a ! to negate it.
Using the command keyword you can specify a single command to be executed when the key is used to login, any other command specified by the user will be ignored at the ssh session will end once the command specified in the authorized_keys file has completed.
You can prevent a key from being used to forward various things by using the no-port-forwarding, no-agent-forwarding, no_X11_forwarding options. Or you can specify a limited range of allowed port forwards using the permitopen option. Multiple permitopen options may be specified. Eg. the following would allow someone to login and setup port forward to ports 80 and 25 on host 10.2.1.56:
permitopen="10.2.1.55:80",permitopen="10.2.1.56:25" ssh-dss XXXX....base64..keyid....= username@host
ssh-agent(1) is designed to run as an ancestor process to any ssh(1) session you wish to manage keys for. The preferred mode of operation (although there are other ways) is to invoke ssh-agent(1) with a program as its argument, which will then be spawned by the agent. This might be a WindowManager, your Shell, or something of the sort. As soon as you exit that program your authentication details get cleaned up and the agent exits.
/usr/bin/ssh-agent -- /usr/X11R6/bin/twm
When using xdm(1)? or another display manager, it should be configured appropriately to use a call as in the line above, rather than calling your WindowManager directly.
Now all you have to do is get your WindowManager to call ssh-add(1) when it starts. ssh-add(1) is how you authenticate yourself for the use of a key. When running under X, you can cause it to run one of the X variants of ssh-askpass(1)? by redirecting its input from /dev/null, eg. /usr/bin/ssh-add < /dev/null &
Pilfered from the fine manual:
There is an elegant way to combine these methods independently of whether your WindowManager provides a facility for autoexecuting commands at launch. Using the exec command, the shell can replace itself with a new ssh-agent(1) instance if it cannot detect an existing agent. The new agent then immediately spawns a new shell to re-execute the script, which now successfully detects the agent and continues to do its actual work. In practice, your .xinitrc or .xsession might look something like this:
#!/bin/bash # check if there's no agent already if [ -x /usr/bin/ssh-agent -a -z "$SSH_AUTH_SOCK" ] ; then exec /usr/bin/ssh-agent $0 fi # # the usual .xinitrc mumbo jumbo goes here # /usr/bin/ssh-add < /dev/null & exec /usr/X11R6/bin/twm
You can easily do something similar if you use CommandLine only systems; in that case, your .bash_profile might look like this:
#!/bin/bash -x # check if there's no agent already if [ -x /usr/bin/ssh-agent -a -z "$SSH_AUTH_SOCK" ] ; then exec /usr/bin/ssh-agent sh -c "exec -a '$0' -- $SHELL" fi # # usual .bash_profile mumbo jumbo goes here # # add keys unless we've already done so if [ -x /usr/bin/ssh-add ] && ! ssh-add -l &> /dev/null ; then /usr/bin/ssh-add fi
The check for existing keys was added here because in contrast to your .xinitrc, .bash_profile is typically executed quite frequently – f.ex, for every xterm(1) you open.
Under Debian 3.0 (Woody), and possibly others, ssh-agent(1) is normally set up to run like this anyway. If you use one of the standard session options, it all works fine. However, if (as in my case) you run a custom setup (ie, have a heavily modified .xsession file), then the ssh-agent(1) either doesn't get called for some reason, or it dies early. Using the above script should solve this.
Page 144 of Linux Server Hacks by O'Reilly shows another, more convenient but less secure variant on the way to run an agent (page 144). The above script doesn't provide an immediate way to pass the agent information between virtual consoles, so you'll usually spawn a new agent for each of them and so have to enter the credentials once for each. The script shown below is more convenient in that a single agent running will suffice, but there's no automatic mechanism for terminating agents, so it will hang around indefinitely. If you are not concerned about this and want more comfort, see below:
# the `hostname` part is there so that this script can be run from the same home # NFS-mounted on different machines without them clobbering each other's settings AGENTFILE=~/.agent.`hostname`.env # don't do anything if there's already an agent, such as when # logging into this machine with agent forwarding enabled on the remote end if [ -z "$SSH_AUTH_SOCK" ]; then # have settings? if [ -f $AGENTFILE ]; then # load them . $AGENTFILE > /dev/null # make sure they're not invalid if [ ! kill -0 $SSH_AGENT_PID > /dev/null 2> ]; then echo "Stale agent file found. Spawning new agent..." eval `ssh-agent | tee $AGENTFILE` ssh-add fi else # no existing settings found, start new agent and save them echo "Starting ssh-agent..." eval `ssh-agent | tee $AGENTFILE` ssh-add fi fi
These methods should be mergable such that you get the benefits of both. I need to mull over the best way to do this. —AristotlePagaltzis
If you're a paranoid ol' bugger like me, and leave your machine logged in most of the time, but don't want to leave your keys freely accessible to everyone while you're away from the computer, here's a simple script that sniffs when the screensaver runs, and removes all your keys, and, when the screensaver is dismissed it prompts you for your keys again:
#!/bin/bash KEYS="id_dsa id_rsa identity insecure sourceforge" delete_all_keys() { ssh-add -D } add_all_keys() { local OK_KEYS unset OK_KEYS for i in $KEYS; do [ -r ~/.ssh/$i ] && OK_KEYS="$OK_KEYS /home/$USER/.ssh/$i" done echo Adding keys... ssh-add $OK_KEYS echo done } exec xscreensaver-command -watch | while read command arg; do case $command in LOCK) delete_all_keys ;; UNBLANK) add_all_keys ;; RUN) echo "Changing screensaver ($arg)" ;; BLANK) #placeholder ;; *) echo Unknown command: $command echo " with arg: $arg" esac done
Place this script somewhere in your $PATH (eg. ~/bin/screenwatch) then start it from either your .xsession or your session manager (GNOME, KDE, etc.) with x-terminal-emulator -e /bin/screenwatch.
To save a lot of more typing, you can forward ssh-agent(1) information with the -A option to SSH. You can thus keep all your credentials on a single machine. NOTE: Do not forward agent connections to hosts you do not trust. Their SuperUser can steal your keys.
.ssh/config convenience (see SSHNotes and ssh_config(5)) is achieved using ForwardAgent yes.
If your home directory is available to multiple machines, some might or might not have ssh-agent running already; you might or might not have forwarded authentication. The following in your $HOME/.profile sets up ssh-agent if it is not present for a particular sh/bash/ksh session, but does not clobber forwarded authentication:
if [ -z "$SSH_AGENT_PID" -a -z "$SSH_AUTH_SOCK" -o ! -S "$SSH_AUTH_SOCK" ]; then eval `ssh-agent` trap "kill -1 $SSH_AGENT_PID" EXIT fi
Use your favourite text editor Vim to edit /etc/ssh/sshd_config on the machine you wish to ssh to, and set these options.
ChallengeResponseAuthentication no PasswordAuthentication no UsePAM no
Now you will HAVE to have to have a key if you wish to SSH into that machine. If not you will NOT be prompted for a password but instead will see: Permission denied (publickey)
Part of CategorySecurity and CategoryNetworking
7 pages link to SSHKeys: