Copy ssh public key to multiple Linux hosts












10














I am trying to copy .ssh/id_rsa.pub from our central server to multiple servers. I have the following script which I usually use to push changes to the different servers.



#!/bin/bash


for ip in $(<IPs); do
# Tell the remote server to start bash, but since its
# standard input is not a TTY it will start bash in
# noninteractive mode.
ssh -q "$ip" bash <<-'EOF'



EOF

done


But in this case, I need to cat the public key on the local server and then add that to multiple servers. Is there a way by using the above here document script to execute the following.



cat .ssh/id_rsa.pub |ssh tony@0.0.0.0 'cat > .ssh/authorized_keys'









share|improve this question
























  • why you need to cat on local since you're copied from one central location to remotes ?
    – klerk
    Jul 17 '14 at 21:43










  • I need to add central server's public key hence local server. Sorry for the confusion.
    – user67186
    Jul 17 '14 at 21:48
















10














I am trying to copy .ssh/id_rsa.pub from our central server to multiple servers. I have the following script which I usually use to push changes to the different servers.



#!/bin/bash


for ip in $(<IPs); do
# Tell the remote server to start bash, but since its
# standard input is not a TTY it will start bash in
# noninteractive mode.
ssh -q "$ip" bash <<-'EOF'



EOF

done


But in this case, I need to cat the public key on the local server and then add that to multiple servers. Is there a way by using the above here document script to execute the following.



cat .ssh/id_rsa.pub |ssh tony@0.0.0.0 'cat > .ssh/authorized_keys'









share|improve this question
























  • why you need to cat on local since you're copied from one central location to remotes ?
    – klerk
    Jul 17 '14 at 21:43










  • I need to add central server's public key hence local server. Sorry for the confusion.
    – user67186
    Jul 17 '14 at 21:48














10












10








10


5





I am trying to copy .ssh/id_rsa.pub from our central server to multiple servers. I have the following script which I usually use to push changes to the different servers.



#!/bin/bash


for ip in $(<IPs); do
# Tell the remote server to start bash, but since its
# standard input is not a TTY it will start bash in
# noninteractive mode.
ssh -q "$ip" bash <<-'EOF'



EOF

done


But in this case, I need to cat the public key on the local server and then add that to multiple servers. Is there a way by using the above here document script to execute the following.



cat .ssh/id_rsa.pub |ssh tony@0.0.0.0 'cat > .ssh/authorized_keys'









share|improve this question















I am trying to copy .ssh/id_rsa.pub from our central server to multiple servers. I have the following script which I usually use to push changes to the different servers.



#!/bin/bash


for ip in $(<IPs); do
# Tell the remote server to start bash, but since its
# standard input is not a TTY it will start bash in
# noninteractive mode.
ssh -q "$ip" bash <<-'EOF'



EOF

done


But in this case, I need to cat the public key on the local server and then add that to multiple servers. Is there a way by using the above here document script to execute the following.



cat .ssh/id_rsa.pub |ssh tony@0.0.0.0 'cat > .ssh/authorized_keys'






shell-script ssh






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 17 '14 at 22:22









Gilles

528k12810561583




528k12810561583










asked Jul 17 '14 at 21:38









user67186

1572411




1572411












  • why you need to cat on local since you're copied from one central location to remotes ?
    – klerk
    Jul 17 '14 at 21:43










  • I need to add central server's public key hence local server. Sorry for the confusion.
    – user67186
    Jul 17 '14 at 21:48


















  • why you need to cat on local since you're copied from one central location to remotes ?
    – klerk
    Jul 17 '14 at 21:43










  • I need to add central server's public key hence local server. Sorry for the confusion.
    – user67186
    Jul 17 '14 at 21:48
















why you need to cat on local since you're copied from one central location to remotes ?
– klerk
Jul 17 '14 at 21:43




why you need to cat on local since you're copied from one central location to remotes ?
– klerk
Jul 17 '14 at 21:43












I need to add central server's public key hence local server. Sorry for the confusion.
– user67186
Jul 17 '14 at 21:48




I need to add central server's public key hence local server. Sorry for the confusion.
– user67186
Jul 17 '14 at 21:48










4 Answers
4






active

oldest

votes


















16














With this simple loop you can automate it and spread to all remote servers.



#!/bin/bash
for ip in `cat /home/list_of_servers`; do
ssh-copy-id -i ~/.ssh/id_rsa.pub $ip
done





share|improve this answer





















  • Hi, I accept your anwser and it worked fine. Thanks
    – user67186
    Jul 17 '14 at 22:16










  • +1. I'm a big fan of a very easy scripting style that get the job done!
    – Laith Al Obaidy
    Dec 24 at 19:12



















2














For copying your public key, you have something in-built in openssh itself. So instead of cat and ssh use this :-



ssh-copy-id -i ~/.ssh/id_rsa.pub YOUR-REMOTE-HOST





share|improve this answer





















  • I want to execute the command copying central server's public to remote servers which exist in IP file. The script iterates through them. So, your example may not be useful here. Thanks
    – user67186
    Jul 17 '14 at 21:52



















0














The accepted answer won't work if one needs to put someone else's public key to multiple machines. So, I've come up with the following solution:



cat add-vassal-tc-agents.sh



#!/bin/bash
set -x # enable bash debug mode
if [ -s vassal-public-key.pub ]; then # if file exists and not empty
for ip in `cat tc-agents-list.txt`; do # for each line from the file
# add EOL to the end of the file and echo it into ssh, where it is added to the authorized_keys
sed -e '$s/$/n/' -s vassal-public-key.pub | ssh $ip 'cat >> ~/.ssh/authorized_keys'
done
else
echo "Put new vassal public key into ./vassal-public-key.pub to add it to tc-agents-list.txt hosts"
fi


This script adds the new key to the users on the list of machines, provided that the environment it is run on has the access.



Example of tc-agents-list.txt:



root@10.10.0.1
root@10.10.0.2
root@10.10.0.3
root@10.10.0.4





share|improve this answer





























    0














    Here is my simple script to copy ssh-keygen to multiple servers without asking password everytime.



    for server in `cat server.txt`;  
    do
    sshpass -p "password" ssh-copy-id -i ~/.ssh/id_rsa.pub user@$server
    done





    share|improve this answer























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "106"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f145182%2fcopy-ssh-public-key-to-multiple-linux-hosts%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      16














      With this simple loop you can automate it and spread to all remote servers.



      #!/bin/bash
      for ip in `cat /home/list_of_servers`; do
      ssh-copy-id -i ~/.ssh/id_rsa.pub $ip
      done





      share|improve this answer





















      • Hi, I accept your anwser and it worked fine. Thanks
        – user67186
        Jul 17 '14 at 22:16










      • +1. I'm a big fan of a very easy scripting style that get the job done!
        – Laith Al Obaidy
        Dec 24 at 19:12
















      16














      With this simple loop you can automate it and spread to all remote servers.



      #!/bin/bash
      for ip in `cat /home/list_of_servers`; do
      ssh-copy-id -i ~/.ssh/id_rsa.pub $ip
      done





      share|improve this answer





















      • Hi, I accept your anwser and it worked fine. Thanks
        – user67186
        Jul 17 '14 at 22:16










      • +1. I'm a big fan of a very easy scripting style that get the job done!
        – Laith Al Obaidy
        Dec 24 at 19:12














      16












      16








      16






      With this simple loop you can automate it and spread to all remote servers.



      #!/bin/bash
      for ip in `cat /home/list_of_servers`; do
      ssh-copy-id -i ~/.ssh/id_rsa.pub $ip
      done





      share|improve this answer












      With this simple loop you can automate it and spread to all remote servers.



      #!/bin/bash
      for ip in `cat /home/list_of_servers`; do
      ssh-copy-id -i ~/.ssh/id_rsa.pub $ip
      done






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jul 17 '14 at 21:51









      klerk

      1,4141913




      1,4141913












      • Hi, I accept your anwser and it worked fine. Thanks
        – user67186
        Jul 17 '14 at 22:16










      • +1. I'm a big fan of a very easy scripting style that get the job done!
        – Laith Al Obaidy
        Dec 24 at 19:12


















      • Hi, I accept your anwser and it worked fine. Thanks
        – user67186
        Jul 17 '14 at 22:16










      • +1. I'm a big fan of a very easy scripting style that get the job done!
        – Laith Al Obaidy
        Dec 24 at 19:12
















      Hi, I accept your anwser and it worked fine. Thanks
      – user67186
      Jul 17 '14 at 22:16




      Hi, I accept your anwser and it worked fine. Thanks
      – user67186
      Jul 17 '14 at 22:16












      +1. I'm a big fan of a very easy scripting style that get the job done!
      – Laith Al Obaidy
      Dec 24 at 19:12




      +1. I'm a big fan of a very easy scripting style that get the job done!
      – Laith Al Obaidy
      Dec 24 at 19:12













      2














      For copying your public key, you have something in-built in openssh itself. So instead of cat and ssh use this :-



      ssh-copy-id -i ~/.ssh/id_rsa.pub YOUR-REMOTE-HOST





      share|improve this answer





















      • I want to execute the command copying central server's public to remote servers which exist in IP file. The script iterates through them. So, your example may not be useful here. Thanks
        – user67186
        Jul 17 '14 at 21:52
















      2














      For copying your public key, you have something in-built in openssh itself. So instead of cat and ssh use this :-



      ssh-copy-id -i ~/.ssh/id_rsa.pub YOUR-REMOTE-HOST





      share|improve this answer





















      • I want to execute the command copying central server's public to remote servers which exist in IP file. The script iterates through them. So, your example may not be useful here. Thanks
        – user67186
        Jul 17 '14 at 21:52














      2












      2








      2






      For copying your public key, you have something in-built in openssh itself. So instead of cat and ssh use this :-



      ssh-copy-id -i ~/.ssh/id_rsa.pub YOUR-REMOTE-HOST





      share|improve this answer












      For copying your public key, you have something in-built in openssh itself. So instead of cat and ssh use this :-



      ssh-copy-id -i ~/.ssh/id_rsa.pub YOUR-REMOTE-HOST






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jul 17 '14 at 21:44









      beginer

      2,0181117




      2,0181117












      • I want to execute the command copying central server's public to remote servers which exist in IP file. The script iterates through them. So, your example may not be useful here. Thanks
        – user67186
        Jul 17 '14 at 21:52


















      • I want to execute the command copying central server's public to remote servers which exist in IP file. The script iterates through them. So, your example may not be useful here. Thanks
        – user67186
        Jul 17 '14 at 21:52
















      I want to execute the command copying central server's public to remote servers which exist in IP file. The script iterates through them. So, your example may not be useful here. Thanks
      – user67186
      Jul 17 '14 at 21:52




      I want to execute the command copying central server's public to remote servers which exist in IP file. The script iterates through them. So, your example may not be useful here. Thanks
      – user67186
      Jul 17 '14 at 21:52











      0














      The accepted answer won't work if one needs to put someone else's public key to multiple machines. So, I've come up with the following solution:



      cat add-vassal-tc-agents.sh



      #!/bin/bash
      set -x # enable bash debug mode
      if [ -s vassal-public-key.pub ]; then # if file exists and not empty
      for ip in `cat tc-agents-list.txt`; do # for each line from the file
      # add EOL to the end of the file and echo it into ssh, where it is added to the authorized_keys
      sed -e '$s/$/n/' -s vassal-public-key.pub | ssh $ip 'cat >> ~/.ssh/authorized_keys'
      done
      else
      echo "Put new vassal public key into ./vassal-public-key.pub to add it to tc-agents-list.txt hosts"
      fi


      This script adds the new key to the users on the list of machines, provided that the environment it is run on has the access.



      Example of tc-agents-list.txt:



      root@10.10.0.1
      root@10.10.0.2
      root@10.10.0.3
      root@10.10.0.4





      share|improve this answer


























        0














        The accepted answer won't work if one needs to put someone else's public key to multiple machines. So, I've come up with the following solution:



        cat add-vassal-tc-agents.sh



        #!/bin/bash
        set -x # enable bash debug mode
        if [ -s vassal-public-key.pub ]; then # if file exists and not empty
        for ip in `cat tc-agents-list.txt`; do # for each line from the file
        # add EOL to the end of the file and echo it into ssh, where it is added to the authorized_keys
        sed -e '$s/$/n/' -s vassal-public-key.pub | ssh $ip 'cat >> ~/.ssh/authorized_keys'
        done
        else
        echo "Put new vassal public key into ./vassal-public-key.pub to add it to tc-agents-list.txt hosts"
        fi


        This script adds the new key to the users on the list of machines, provided that the environment it is run on has the access.



        Example of tc-agents-list.txt:



        root@10.10.0.1
        root@10.10.0.2
        root@10.10.0.3
        root@10.10.0.4





        share|improve this answer
























          0












          0








          0






          The accepted answer won't work if one needs to put someone else's public key to multiple machines. So, I've come up with the following solution:



          cat add-vassal-tc-agents.sh



          #!/bin/bash
          set -x # enable bash debug mode
          if [ -s vassal-public-key.pub ]; then # if file exists and not empty
          for ip in `cat tc-agents-list.txt`; do # for each line from the file
          # add EOL to the end of the file and echo it into ssh, where it is added to the authorized_keys
          sed -e '$s/$/n/' -s vassal-public-key.pub | ssh $ip 'cat >> ~/.ssh/authorized_keys'
          done
          else
          echo "Put new vassal public key into ./vassal-public-key.pub to add it to tc-agents-list.txt hosts"
          fi


          This script adds the new key to the users on the list of machines, provided that the environment it is run on has the access.



          Example of tc-agents-list.txt:



          root@10.10.0.1
          root@10.10.0.2
          root@10.10.0.3
          root@10.10.0.4





          share|improve this answer












          The accepted answer won't work if one needs to put someone else's public key to multiple machines. So, I've come up with the following solution:



          cat add-vassal-tc-agents.sh



          #!/bin/bash
          set -x # enable bash debug mode
          if [ -s vassal-public-key.pub ]; then # if file exists and not empty
          for ip in `cat tc-agents-list.txt`; do # for each line from the file
          # add EOL to the end of the file and echo it into ssh, where it is added to the authorized_keys
          sed -e '$s/$/n/' -s vassal-public-key.pub | ssh $ip 'cat >> ~/.ssh/authorized_keys'
          done
          else
          echo "Put new vassal public key into ./vassal-public-key.pub to add it to tc-agents-list.txt hosts"
          fi


          This script adds the new key to the users on the list of machines, provided that the environment it is run on has the access.



          Example of tc-agents-list.txt:



          root@10.10.0.1
          root@10.10.0.2
          root@10.10.0.3
          root@10.10.0.4






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jul 27 at 11:55









          Ilya Sheershoff

          614




          614























              0














              Here is my simple script to copy ssh-keygen to multiple servers without asking password everytime.



              for server in `cat server.txt`;  
              do
              sshpass -p "password" ssh-copy-id -i ~/.ssh/id_rsa.pub user@$server
              done





              share|improve this answer




























                0














                Here is my simple script to copy ssh-keygen to multiple servers without asking password everytime.



                for server in `cat server.txt`;  
                do
                sshpass -p "password" ssh-copy-id -i ~/.ssh/id_rsa.pub user@$server
                done





                share|improve this answer


























                  0












                  0








                  0






                  Here is my simple script to copy ssh-keygen to multiple servers without asking password everytime.



                  for server in `cat server.txt`;  
                  do
                  sshpass -p "password" ssh-copy-id -i ~/.ssh/id_rsa.pub user@$server
                  done





                  share|improve this answer














                  Here is my simple script to copy ssh-keygen to multiple servers without asking password everytime.



                  for server in `cat server.txt`;  
                  do
                  sshpass -p "password" ssh-copy-id -i ~/.ssh/id_rsa.pub user@$server
                  done






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 16 at 10:11









                  Thomas

                  3,69161225




                  3,69161225










                  answered Dec 16 at 9:10









                  Rajesh Kumar

                  1




                  1






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f145182%2fcopy-ssh-public-key-to-multiple-linux-hosts%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Morgemoulin

                      Scott Moir

                      Souastre