Problem: When running the ssh-keyscan command in cron it emails me the output of ssh-keyscan every day. The email simply contains the following.

# <hostname> SSH-2.0-OpenSSH_5.3

My (simplified) cron job:

host=`uname -n`
SSHKey=`ssh-keyscan $host`
echo $SSHKey >> /root/.ssh/known_hosts

My question: How do I prevent ssh-keyscan from writing anything to the shell?

2 Answers 2


redirect stderr into /dev/null

host=`uname -n`
SSHKey=`ssh-keyscan $host 2> /dev/null`
echo $SSHKey >> /root/.ssh/known_hosts
  • Thanks, I was assuming that output was stdout not stderr and I was redirecting the wrong stream in my tests. I appreciate your quick reply.
    – tbenz9
    Commented Aug 12, 2015 at 17:05
  • Is there a reason to use `…` and not $(…)
    – A.B.
    Commented Aug 12, 2015 at 20:18
  • it has the same meaning as you can see in manual page for bash
    – Jakuje
    Commented Aug 12, 2015 at 21:03

An old question, I wonder why no one has suggested the following variant:

(ssh-keyscan $host >>/root/.ssh/known_hosts) 2> >(grep -vE '^#')

This will only suppress the stderr output starting with #, i.e. real error messages will not be suppressed.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .