Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is there a command (or a one-liner) to remove a ssh key on a server? Something like the opposite of ssh-copy-id?

share|improve this question
    
Some SSH server software support the RFC 4819 protocol for managing authorized SSH keys, but it's so rare it's almost nonexistent on Linux :( –  grawity May 29 '12 at 7:38

3 Answers 3

Nope. You'll need to SSH in and use sed or grep to remove the key from the file.

share|improve this answer
    
Thank you. I will keep the question open a little longer to see if someone also can provide a script that does the opposite of ssh-copy-id –  grm May 30 '12 at 11:56

Start with locking the accounts, for example :

passwd -l userName
passwd -l vivek

Then deleating is simple

rm -rf /home/vivek/.ssh
share|improve this answer
1  
I don't think removing .ssh entirely is a good idea. –  slhck May 29 '12 at 9:31
    
Nor locking the account itself... –  grawity May 29 '12 at 14:36

As Ignatio suggested this can be done with grep -v.

Here is a example which removes the key containing some unique string or just deletes the authorized_keys file when no other key remains.

if test -f $HOME/.ssh/authorized_keys; then
  if grep -v "some unique string" $HOME/.ssh/authorized_keys > $HOME/.ssh/tmp; then
    cat $HOME/.ssh/tmp > $HOME/.ssh/authorized_keys && rm $HOME/.ssh/tmp;
  else
    rm $HOME/.ssh/authorized_keys && rm $HOME/.ssh/tmp;
  fi;
fi

Replace some unique string with something that only exists in the key you wish to remove.

As a oneliner over ssh this becomes

ssh hostname 'if test -f $HOME/.ssh/authorized_keys; then if grep -v "some unique string" $HOME/.ssh/authorized_keys > $HOME/.ssh/tmp; then cat $HOME/.ssh/tmp > $HOME/.ssh/authorized_keys && rm $HOME/.ssh/tmp; else rm $HOME/.ssh/authorized_keys && rm $HOME/.ssh/tmp; fi; fi'

Tested on Linux (SLES) and HP-UX.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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