First, I'm TRYING to use rsa keys because I'm using the script to create RSA keys, but initially I have to connect to the remote machine to pass over the newly generated authentication key. Having premade keys is what I'm doing, but I'd like an easier way to get the key to all of these machines.

So here's the deal: I need to connect to 100+ machines and pass over a public key file plus set permissions and turn on key authentication. Right now I can do it in 2 lines: one scp command to transfer and one ssh command to set permissions and turn key authentication back on. But as it is, it's asking for the password twice, for each machine. With over 100 machines needing these keys so I never have to do the password thing again, I don't want to type this password 200 times.

I need some way to insert the password during the connection, just this once (well twice technically). I was hoping for the ability to store the password in a variable while the script ran and then be done with it after the connections are made and the public key is transferred to all these machines.


Edit: forgot to mention, I can't install anything. So I can't use expect.

  • 4
    man 1 ssh-copy-id ... a command which is much less well known than it should be.
    – derobert
    Nov 2, 2012 at 17:32
  • You can't install anything on your local machine? You'd only want expect there, not on all the remote machines.
    – derobert
    Nov 2, 2012 at 17:39
  • It's complicated.
    – Kraden
    Nov 2, 2012 at 18:20

1 Answer 1


You don't need to do two connections to do what you're doing. Assuming ssh-copy-id won't work for you, here is something from a script I wrote a long time ago (before I knew about ssh-copy-id) that does it in one ssh:

tar c $keys | ssh "$1" "set -e; cd \`mktemp -d\`; tar x; mkdir -p ~/.ssh; chmod go-w ~/.ssh; cat $keys >> ~/.ssh/authorized_keys; chmod go-w ~/.ssh/authorized_keys"

Yes, that's one huge line. $keys holds the names of all the public keys (remember, this is an old script):

for k in identity.pub id_rsa.pub id_dsa.pub; do
        [ -e ~/.ssh/$k ] && keys="$keys $k"

The key thing is ssh is a binary-safe transport, so you can use it to transfer tar to get your files across in the same session as is running your chmod, etc.

  • I hadn't thought of putting the content of the key into memory...I'll try this out.
    – Kraden
    Nov 2, 2012 at 18:20
  • I didn't try this exact thing, but the idea was sound and is now working. ssh can echo a variable into a file on the remote server and in essence "transfer" that file. Really you're just creating a new file on the remote server that contains the same information. NOTE: I am still prompted for the password, but instead of it being multiple times, it's just the once, which is what I wanted. Thanks again!
    – Kraden
    Nov 2, 2012 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.