SSH Tip: Hash Known Hosts

| No Comments | No TrackBacks

As any user of SSH is aware, the first time you connect to a remote host, OpenSSH caches the server's public key in ~/.ssh/known_hosts. If the server's private key ever changes, SSH will raise an ugly error alerting you of the risk that an untrusted third-party could be intercepting your new connection.

Unfortunately, the known_hosts file represents a small security risk. It contains a convenient list of all servers to which you connect. An attacker who gained access to your password or unencrypted private key would simply need to iterate down the list until your credentials were accepted.

OpenSSH can optionally hash the server names in known_hosts. This renders the file useless to prying eyes without impairing SSH's ability to check hosts against the list. Enabling this feature and hashing your existing known_hosts file is easy!

  1. Add the parameter "HashKnownHosts yes" to your ~/.ssh/config.
  2. Run "ssh-keygen -H".

Below the fold, I included a handy shell script that accomplishes the same thing.

One final consideration is your shell history storing your ssh commands. It's easy to configure bash to forget these. Just set HISTIGNORE="ssh *:scp *:sftp *" in your ~/.bashrc.

#!/bin/sh

# Add parameter to existing config, if any
echo "HashKnownHosts yes" > ~/.ssh/config.new
if [ -f ~/.ssh/config ]; then
  grep -v "^[[:space:]]*HashKnownHosts" \
      ~/.ssh/config                     \
      >> ~/.ssh/config.new
fi
mv ~/.ssh/config.new ~/.ssh/config

# Hash existing known hosts
ssh-keygen -H
rm ~/.ssh/known_hosts.old

No TrackBacks

TrackBack URL: http://www.vitalvector.com/mt/mt-tb.cgi/29

Leave a comment

Creative Commons License
This blog is licensed under a Creative Commons License.

About this Entry

This page contains a single entry by Matthew published on February 20, 2009 10:23 PM.

Friends, Stop Embarassing Yourself was the previous entry in this blog.

A Note to Drivers of West Los Angeles is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Recent Comments

  • jay Donnell: I'm sure they will go in that direction in the read more
  • jay: I'm going tomorrow :) read more
  • jay: You should have linked some of the stats :) I'm read more
  • Sue Denim: I only have on thing to say: XBOX read more
  • jay: If unemployment is high would a lack of a minimum read more
  • jay: Saying that we need to cut unnecessary social programs (I'm read more
  • Matthew: Davis's tripling of the vehicle registration fees amounted to some read more
  • jay: You're point is a bit implicit so please forgive me read more
  • Zack: That's pretty awesome. read more
  • Zack: I knew it. I'm going to go invent my own read more