SSH: Testing whether ID is already in authorized_keys

Michael R Head burner at suppressingfire.org
Sat Sep 3 23:08:46 UTC 2005


On Sat, 2005-09-03 at 16:31 -0400, Stephen R Laniel wrote:
> 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?

You could do something like this:

ssh-copy-id -i ~/.ssh/id_dsa.pub user at host.com
ssh user at hostname  "cat .ssh/authorized_keys |sort |uniq > .ssh/authorized_keys.uniq; mv .ssh/authorized_keys.uniq .ssh/authorized_keys"

> 
-- 
Michael R Head <burner at suppressingfire.org>
GPG: http://www.suppressingfire.org/~burner/gpg.key.txt (ID 23A02B1F)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20050903/43d79625/attachment.sig>


More information about the ubuntu-users mailing list