[Linux-disciples] SSH: Testing whether ID is already in authorized_keys

Stephen R Laniel steve at laniels.org
Sat Sep 3 16:44:31 EDT 2005


I'm writing a script to do rsync backups, and before I do
the backup I'd like to copy the local user's SSH public key
into his remote ~/.ssh/authorized_keys file -- unless the
key is *already* in that file. The normal trick to do this
is to use

ssh-copy-id -i ~/.ssh/id_dsa.pub user at host.com

However, I've just noticed that ssh-copy-id doesn't check
whether the key is already in there. Is there any easy,
elegant way (as easy and elegant as ssh-copy-id) to only
copy the key over if it's not already in the remote
authorized_keys file?

I guess one hackish way around this would be to do

TEMPFILE=$(mktemp)
AUTHKEYSFILE=~/.ssh/authorized_keys
sort $AUTHKEYSFILE |uniq > $TEMPFILE
mv $TEMPFILE $AUTHKEYSFILE

which would eliminate any duplicate lines from the
authorized_keys file. But that seems like a hack. Does
anyone know of a cleaner way?

-- 
Stephen R. Laniel
steve at laniels.org
+(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.bostoncoop.net/pipermail/linux-disciples/attachments/20050903/92d3ff7a/attachment.pgp


More information about the Linux-disciples mailing list