This question is repeating from Stack Overflow because i don't get answer from there.

I'm trying to make a simple script that will import the public key to other servers.

I have made 2 options you can choose. One is using ssh-copy-id, and the other is simple copy the pub key to the file on the other server. The second one works ok but I cant make this to work wiht ssh-copy-id. This is what I have for now:

ssh-copy-id -i $var1 $i

Where $var1 is "/path/to/the/public_key.pub" and $i is the servers where they are written in a txt file. The whole idea is I have my pub keys everywhere and I like to enter the pub key of a new admin guy everywhere. And it works ok but when I execute I have to enter my password. Any solution to do this with one time entering the password. And also I like to enter new line before adding the key. But I can't find solution how to do this with ssh-copy-id

  • Please delete the question on stack overflow so that this one doesn't get closed.
    – Ramesh
    Jul 29, 2014 at 19:48
  • You don't want to be asked for a password for each server it copies to?
    – Barmar
    Jul 29, 2014 at 19:51
  • That's the impression I got. From the manual page: "ssh-copy-id is a script that uses ssh(1) to log into a remote machine (presumably using a login password, so password authentication should be enabled, unless you've done some clever use of multiple identities). It assembles a list of one or more fingerprints and tries to log in with each key, to see if any of them are already installed." -- Naturally, if the public key is not already at the remote place, this means a lot of password typing. Jul 29, 2014 at 20:03
  • The duplicate on SO is here ... Random folks: I suggest if you have enough rep on SO, vote to close that one. It's not even on-topic there... Leave this copy open.
    – derobert
    Jul 29, 2014 at 20:53
  • I delete the post from Overflow. Yes, i have ubuntu and my private key with public also are stored in /root/.ssh/pub and priv. My key is on every server. I like when new key is change or add to add on all servers in authorized_keys. I have put servers in on file and loop through them executing the command above. But every session, command executed is asking me for password. Jul 30, 2014 at 8:27

2 Answers 2


The solution is this.

1) On your terminal, create an array with all hosts/IP addresses you want to copy the id_rsa.pub. For example

hosts=( host1 host2 host4 )

2) Create the expect file save it and make it executable using chmod +x filename

#!/usr/bin/expect -f

if { $argc != 4 } {
    puts stderr "usage: ./expt host pass user keyfile"
    exit 2

   set host [lindex $argv 0]
   set pass [lindex $argv 1]
   set user [lindex $argv 2]
   set keyfile [lindex $argv 3]

   spawn ssh-copy-id -i $keyfile $user@$host
   expect "assword:"
   send "$pass\n"
   expect eof

3) Now loop through the array which holds your hosts and send the id_rsa.pub

for host in "${hosts[@]}" ; do ./expt "$host" Y0urPassword username ~/.ssh/id_rsa.pub ; done

Wait until the public key is copied to all hosts.

  • I'll try this as soon as possible to gave you reply. But i don't like to store my password in a file. If there is no solution then ill go this way :). Jul 30, 2014 at 8:33
  • You can use password as an argument and pass it to the filename Jul 30, 2014 at 9:23
  • That was my idea to use enter pass and then use it as variable, but i can't find a way to use it with the command or when prompt for pass to get that variable. For example when the command execute it will ask me for password and to read id from a variable, or something like that ... im lost on how to solve this :) Jul 30, 2014 at 14:01
  • @user3523605 see my updated answer Jul 30, 2014 at 15:12
  • 1
    This solution only works when the host has already been added to known_hosts. What about edge cases where the box has never been visited and the user is prompted with, "Are you sure you want to continue connecting (yes/no)?"
    – a_arias
    Oct 14, 2014 at 22:53

Ok finally i have got this. Now work perfect. In the script after the for loop and do i add this line:

/usr/bin/expect <<EOD

and the conde of the expect procedure like:

spawn ssh-copy-id -i test.pub root@<name_host>
        expect "Enter passphrase for key"
        send "$pass\n"
        expect eof

and works perfect !

Thanks again for explain and helping val0x00ff


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .