How To Search authorized_keys For Public Key And Remove It
Solution 1:
Would grep -v $pub_key /root/.ssh/authorized_keys > /tmp/authorized_keys.new
work? You can compared to two files to verify the work. Given the long strings of random characters that comprise the keys, it's probably sufficient to do this using a substring of $pub_key.
Solution 2:
I think I need to use sed, but can't figure out the syntax, here is what I have thus far:
sed -i '/$pub_key/ d' /root/.ssh/authorized_keys
Remove the space before d
, it will work as you expect:
sed -i '/$pub_key/d' /root/.ssh/authorized_keys
Solution 3:
Never forget the power of perl oneliners!
$pub_key="AAAABCD"; perl -i "/$pub_key/ or print" /root/.ssh/authorized_keys
That will perform an in-place edit and remove the offending key.
Or, perhaps you have a really really long authorized_keys file and you want to process it in parallel with gnu parallel?
pub_key="AAAABCD"; parallel -k "echo {} | grep -v $pub_key" </root/.ssh/authorized_keys
the possibilities are endless.
update: fixed typo that Ole pointed out. Thanks Ole!