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

Adam Rosi-Kessel adam at rosi-kessel.org
Sun Sep 4 12:53:51 EDT 2005


You could copy it over, grep the authorized_keys for the file, and only
add it if it's not in there. I think any solution to this problem is
going to be some sort of hack.

On Sat, Sep 03, 2005 at 04:44:31PM -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?
> 
> 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?
-- 
Adam Rosi-Kessel
http://adam.rosi-kessel.org
-------------- 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/20050904/9f3ef3c4/attachment.pgp


More information about the Linux-disciples mailing list