25

I have a 2 node hadoop cluster.

I ran this command on the master:

$ssh-copy-id -i /home/hadoop/.ssh/id_rsa.pub [email protected]

How can I undo this? I would actually like to reassign the key.

192.168.1.1 is the slave.

2 Answers 2

25

Identify the public key that you copied when you ran ssh-copy-id:

cat ~/.ssh/id_rsa.pub

SSH to the server you copied the key to:

ssh [email protected]

Edit the file ~hadoop/.ssh/authorized_keys on 192.168.1.1 using your preferred editor, and delete the line containing your key.

2
  • 2
    Is there a way to do it more automatic why? Like ssh-rm-id [email protected]
    – S.R
    Apr 18, 2018 at 10:22
  • 1
    @S.R I'm not aware of a single command that automates this. In theory, you could "automate" it yourself with a little one-liner using ssh to run a sed command (or similar) to edit ~/.ssh/authorized_keys and remove the line. See superuser.com/questions/429954/… Apr 18, 2018 at 10:32
9

If you have done a ssh-copy-id like:

remote='user@machine'
ssh-copy-id -i $remote

So you can access this remote machine without using a password:

ssh $remote

To undo it programmatically, you can script something like:

idssh=$(awk '{print $2}' ~/.ssh/id_rsa.pub)
ssh $remote "sed -i '\#$idssh#d' .ssh/authorized_keys"

I use it in scripts I need to scp several files, so I ask only once for password.

7
  • 1
    This might be a bit dangerous: you are grepping for the comment field of the key. It is an arbitrary string without any meaning and may be contained more than once. I'd grep either for the long AAA....== string (the actual key) or for the complete line from id_rsa.pub. But +1 for showing how to automate the removal of a key.
    – PerlDuck
    Jun 21, 2018 at 9:57
  • 1
    @PerlDuck you are right. It is much better to use the key ($2) itself than the third field. Thank you.
    – Javi M.
    Jun 23, 2018 at 9:46
  • @Javi M. I encountered another issue. The forward slash used by default as the delimiter in sed was in my public key. As a result, I found it best to use a semicolon as the sed delimiter since it appears unlikely to show up in public key. To do this, it was necessary to first escape the character. I ended up with something like this: ssh $remote "sed -i '\;$idssh;{d}' .ssh/authorized_keys"
    – ccalvert
    Oct 29, 2019 at 22:11
  • Here is some information on which characters can end up in a public key.
    – ccalvert
    Oct 29, 2019 at 22:20
  • Thanks @ccalvert. Based on your suggested comment I have approved chris-maes edition
    – Javi M.
    Jan 21, 2020 at 11:12

You must log in to answer this question.

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