My google search results little helpful so I asked here.

My Ubuntu server now has many duplicated entries in both files ~/.ssh/authorzied_keys and ~/.ssh/known_hosts

I wonder if there is a command/utility to remove those duplicated lines and list them once only?


The commandline utilities are called uniq and sort. You can simply pipe the file through them to get only unique entries:

sort ~/.ssh/authorized_keys | uniq > ~/.ssh/authorized_keys.uniq

and then replace the old file with the new one:

mv ~/.ssh/authorized_keys{.uniq,}

The ~/.ssh/known_hosts are handled by ssh itself and should not contain any duplicates (if you modified it by hand, it can and then you can use the same approach as above).

| improve this answer | |
  • 1
    Your command should not take into account the comment part (the optional last field in a line) which may vary arbitrarily – xhienne Jan 19 '17 at 13:35
  • @xhienne yes, in ideal case, if this would be a problem in the authorized keys. The known hosts do not have comments. – Jakuje Jan 19 '17 at 22:35

ssh-keygen -R hostname will remove the entry for specified hostname

| improve this answer | |
  • Upvote. Simple to te point. Then the connection can be reestablished with ssh-copy-id -i ~/.ssh/id_rsa.pub othersysname like normal. – SDsolar Jun 22 '18 at 5:01
  • This can work only with known_hosts. – Jakuje Aug 5 '19 at 11:43

My issue was with multiple entries from one host with different host keys. This was from a Jenkins build slave. App server was rebuilt. Hostname/IP was moved to new server but the host keys were different. Here's the one liner I came up with:

cat known_hosts | cut -f1 -d' ' | sort | uniq -c | \ 
sed '/^ *1 /d' | awk '{print $2}' | while read line; do \
ssh-keygen -R $line; ssh-keyscan $line; \

Take the first field in known_hosts, sort it, uniq -c to count instances of each hostname, sed to delete any lines with a count of 1, then ssh-keygen -R to remove the host, and finally ssh-keyscan to add the current host-key back to known_hosts.

WARNING: If you did not expect the host keys to change, then you should figure out why they did before running this. Host keys are there to ensure you're connecting to the host you're expecting to (and not some man in the middle host).

| improve this answer | |

Since you want to keep one of each of the duplicates, the simplest solution is probably to just use a text editor to delete the lines of each of those files that no longer have correct information.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.