Ssh-copy-id without password to user `postgres`

ssh-keys

I have N hosts where can I do passwordless login via ssh to user root.

For those machines I like to add a ssh-pub-key to authorized_keys of user postgres.

Goal: I want passwordless login to user postgres, too.

AFAIK the tool ssh-copy-id does not help here, since I want the action "add ssh-pub-key to user postgres" to be passwordless.

How to solve this?

Best Answer

Well, if you can login passwordlessly as root you could do something like this I guess:

scp pub_key root@<host>:
ssh root@<host> 'mkdir -p ~postgres/.ssh; cat pub_key >> ~postgres/.ssh/authorized_keys; chown -R postgres.postgres ~postgres/.ssh; chmod 644 ~postgres/.ssh/authorized_keys; chmod 700 ~postgres/.ssh; rm -f pub_key'

which will first copy the public key (again) to the remote host, then add that file to the authorized_keys of the postgres user and make sure it has the right permissions. It'll also make sure the postgres/.ssh directory exists and has the right ownership/permissions

Related Topic