SSH tricks: controlling users and fingerprint collisions

Translation of the original in Catalan. Read it in: CatalàCastellano

A couple of tricks for SSH, one for the server (sshd) and the other for the client (ssh).

If we want to allow only some users to access our system via SSH we have to put the AllowUsers directive in the /etc/ssh/sshd_config file. I got this trick from Restrict SSH per user.

An example:

AllowUsers root oysteivi otheradmin

The other trick is very useful when we write scripts that use for example: scp, rsync or other similar tools. Sometimes no matter how much we use a system of public key authentication with ssh that is not enough because there may be a conflict in the fingerprint that we have stored (or not) for the server we are going to connect to. Then we are asked whether we want to save that fingerprint if we do not have it stored, or whether we accept that there is a conflict between the stored fingerprint and the one the server is sending us. For more details about the problem you can consult this securityfocus article SSH Host Key Protection. What is more, this example will surely help to refresh your memory about what I mean.

 $ ssh ssh-server.example.com
  The authenticity of host 'ssh-server.example.com (12.18.429.21)' can't be established.
  RSA key fingerprint is 98:2e:d7:e0:de:9f:ac:67:28:c2:42:2d:37:16:58:4d.
  Are you sure you want to continue connecting (yes/no)? yes

In order to control the behaviour of this event we can do it with the StrictHostKeyChecking=[yes|no|ask] parameter, we can put this in /etc/ssh/ssh_config or on the command line through the -o flag.

Example forcing the check:

  $ ssh -o 'StrictHostKeyChecking=yes' ssh-server.example.com
  No RSA host key is known for localhost and you have requested strict checking.
  Host key verification failed.

Example asking for the check:

  $ ssh -o stricthostkeychecking=ask ssh-server.example.com
  The authenticity of host 'ssh-server.example.com (12.18.429.21)' can't be established.
  RSA key fingerprint is 98:2e:d7:e0:de:9f:ac:67:28:c2:42:2d:37:16:58:4d.
  Are you sure you want to continue connecting (yes/no)? yes