SSH + Change password using root











up vote
1
down vote

favorite












I'm trying to connect to various servers to change the password of a specific user.



I hope that you can help me to build a Script to do that thing, the complexity is that I have to use sudo and send the password in order to connect on the remote server as root.



I'm trying something like this but is not working:



sshpass -p pass ssh user@server << EOF
echo pass | sudo -S -u root "echo 'user2:pass2' | chpasswd"
EOF


To better explain what I want to do, these are the steps that I need to do:




  1. connect to a server with my user.

  2. using "sudo" to connect as a root.

  3. execute "echo 'user2:pass2' | chpasswd" in order to change the password of the user2.


This is the error:




Pseudo-terminal will not be allocated because stdin is not a terminal. stat: cannot stat `pipe:[2670580091]': No such file or directory [sudo] password for user1: sudo: echo 'user2:XXXXXXX' | chpasswd: command not found











share|improve this question




















  • 1




    Try adding -t to the ssh command to force it to allocate a tty which sudo requires to enter a password. It would also be helpful to update your answer with how/why that particular command fails (error, hangs, no output etc).
    – Michael Daffin
    Jan 8 at 22:16

















up vote
1
down vote

favorite












I'm trying to connect to various servers to change the password of a specific user.



I hope that you can help me to build a Script to do that thing, the complexity is that I have to use sudo and send the password in order to connect on the remote server as root.



I'm trying something like this but is not working:



sshpass -p pass ssh user@server << EOF
echo pass | sudo -S -u root "echo 'user2:pass2' | chpasswd"
EOF


To better explain what I want to do, these are the steps that I need to do:




  1. connect to a server with my user.

  2. using "sudo" to connect as a root.

  3. execute "echo 'user2:pass2' | chpasswd" in order to change the password of the user2.


This is the error:




Pseudo-terminal will not be allocated because stdin is not a terminal. stat: cannot stat `pipe:[2670580091]': No such file or directory [sudo] password for user1: sudo: echo 'user2:XXXXXXX' | chpasswd: command not found











share|improve this question




















  • 1




    Try adding -t to the ssh command to force it to allocate a tty which sudo requires to enter a password. It would also be helpful to update your answer with how/why that particular command fails (error, hangs, no output etc).
    – Michael Daffin
    Jan 8 at 22:16















up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm trying to connect to various servers to change the password of a specific user.



I hope that you can help me to build a Script to do that thing, the complexity is that I have to use sudo and send the password in order to connect on the remote server as root.



I'm trying something like this but is not working:



sshpass -p pass ssh user@server << EOF
echo pass | sudo -S -u root "echo 'user2:pass2' | chpasswd"
EOF


To better explain what I want to do, these are the steps that I need to do:




  1. connect to a server with my user.

  2. using "sudo" to connect as a root.

  3. execute "echo 'user2:pass2' | chpasswd" in order to change the password of the user2.


This is the error:




Pseudo-terminal will not be allocated because stdin is not a terminal. stat: cannot stat `pipe:[2670580091]': No such file or directory [sudo] password for user1: sudo: echo 'user2:XXXXXXX' | chpasswd: command not found











share|improve this question















I'm trying to connect to various servers to change the password of a specific user.



I hope that you can help me to build a Script to do that thing, the complexity is that I have to use sudo and send the password in order to connect on the remote server as root.



I'm trying something like this but is not working:



sshpass -p pass ssh user@server << EOF
echo pass | sudo -S -u root "echo 'user2:pass2' | chpasswd"
EOF


To better explain what I want to do, these are the steps that I need to do:




  1. connect to a server with my user.

  2. using "sudo" to connect as a root.

  3. execute "echo 'user2:pass2' | chpasswd" in order to change the password of the user2.


This is the error:




Pseudo-terminal will not be allocated because stdin is not a terminal. stat: cannot stat `pipe:[2670580091]': No such file or directory [sudo] password for user1: sudo: echo 'user2:XXXXXXX' | chpasswd: command not found








ssh sudo root






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 7 at 10:15









Pierre.Vriens

96841015




96841015










asked Jan 8 at 22:03









Jose

61




61








  • 1




    Try adding -t to the ssh command to force it to allocate a tty which sudo requires to enter a password. It would also be helpful to update your answer with how/why that particular command fails (error, hangs, no output etc).
    – Michael Daffin
    Jan 8 at 22:16
















  • 1




    Try adding -t to the ssh command to force it to allocate a tty which sudo requires to enter a password. It would also be helpful to update your answer with how/why that particular command fails (error, hangs, no output etc).
    – Michael Daffin
    Jan 8 at 22:16










1




1




Try adding -t to the ssh command to force it to allocate a tty which sudo requires to enter a password. It would also be helpful to update your answer with how/why that particular command fails (error, hangs, no output etc).
– Michael Daffin
Jan 8 at 22:16






Try adding -t to the ssh command to force it to allocate a tty which sudo requires to enter a password. It would also be helpful to update your answer with how/why that particular command fails (error, hangs, no output etc).
– Michael Daffin
Jan 8 at 22:16












3 Answers
3






active

oldest

votes

















up vote
0
down vote













I've executed things as root over ssh using:



ssh -t  you@server <<EOF 
echo pass_for_you | sudo -S bash -c "commands to do as root";
EOF





share|improve this answer




























    up vote
    0
    down vote













    would this help ? or something similar ? assuming I understood what you are trying to do ?



    for i in server{1,2,3}; do
    ssh -t user@$i 'sudo passwd user2'
    done





    share|improve this answer























    • I can't do that because the server have policies where the only user can execute passwd is root. In order to change the password I have to do the next steps: 1) connect to a server with my user 2) using "sudo" to connect as a root 3) execute "echo 'user2:pass2' | chpasswd" in order to change the password of the user2
      – Jose
      Jan 9 at 13:22










    • for i in server{1,2,3}: do ssh -t user@$i 'sudo su - ; passwd user2 ' done cant remember if that prompts for password or not
      – ssvegeta96
      Jan 9 at 16:18




















    up vote
    0
    down vote













    The ansible module "user" should work for your problem. But you have to provide the already crypted password-hash instead.
    Make the hash with:



    mkpasswd --method=sha-512


    Execute a ansible adhoc-command like this with the quoted generated hash-string:



    ansible -v all -i <hostname>, --become --extra-vars 'ansible_become_pass=<sudo password> ansible_password=<sudo password>' --user=<ssh user> -k -m user --args='name=root update_password=always password="$6$IZjuXoio1$zHpQQDZGDPwG8mr2R6Mrt1C8Nqstui75enT/o0oSVJ3M6rqff8993kmAaTgbc9q9HTgPD2jtZukEqgeIGKfUN0"'


    Prerequesites: ansible and sshpass on client machine.






    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',
      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%2f415684%2fssh-change-password-using-root%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      I've executed things as root over ssh using:



      ssh -t  you@server <<EOF 
      echo pass_for_you | sudo -S bash -c "commands to do as root";
      EOF





      share|improve this answer

























        up vote
        0
        down vote













        I've executed things as root over ssh using:



        ssh -t  you@server <<EOF 
        echo pass_for_you | sudo -S bash -c "commands to do as root";
        EOF





        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          I've executed things as root over ssh using:



          ssh -t  you@server <<EOF 
          echo pass_for_you | sudo -S bash -c "commands to do as root";
          EOF





          share|improve this answer












          I've executed things as root over ssh using:



          ssh -t  you@server <<EOF 
          echo pass_for_you | sudo -S bash -c "commands to do as root";
          EOF






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 9 at 14:37









          gat1

          617




          617
























              up vote
              0
              down vote













              would this help ? or something similar ? assuming I understood what you are trying to do ?



              for i in server{1,2,3}; do
              ssh -t user@$i 'sudo passwd user2'
              done





              share|improve this answer























              • I can't do that because the server have policies where the only user can execute passwd is root. In order to change the password I have to do the next steps: 1) connect to a server with my user 2) using "sudo" to connect as a root 3) execute "echo 'user2:pass2' | chpasswd" in order to change the password of the user2
                – Jose
                Jan 9 at 13:22










              • for i in server{1,2,3}: do ssh -t user@$i 'sudo su - ; passwd user2 ' done cant remember if that prompts for password or not
                – ssvegeta96
                Jan 9 at 16:18

















              up vote
              0
              down vote













              would this help ? or something similar ? assuming I understood what you are trying to do ?



              for i in server{1,2,3}; do
              ssh -t user@$i 'sudo passwd user2'
              done





              share|improve this answer























              • I can't do that because the server have policies where the only user can execute passwd is root. In order to change the password I have to do the next steps: 1) connect to a server with my user 2) using "sudo" to connect as a root 3) execute "echo 'user2:pass2' | chpasswd" in order to change the password of the user2
                – Jose
                Jan 9 at 13:22










              • for i in server{1,2,3}: do ssh -t user@$i 'sudo su - ; passwd user2 ' done cant remember if that prompts for password or not
                – ssvegeta96
                Jan 9 at 16:18















              up vote
              0
              down vote










              up vote
              0
              down vote









              would this help ? or something similar ? assuming I understood what you are trying to do ?



              for i in server{1,2,3}; do
              ssh -t user@$i 'sudo passwd user2'
              done





              share|improve this answer














              would this help ? or something similar ? assuming I understood what you are trying to do ?



              for i in server{1,2,3}; do
              ssh -t user@$i 'sudo passwd user2'
              done






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jan 9 at 16:19

























              answered Jan 8 at 23:09









              ssvegeta96

              577




              577












              • I can't do that because the server have policies where the only user can execute passwd is root. In order to change the password I have to do the next steps: 1) connect to a server with my user 2) using "sudo" to connect as a root 3) execute "echo 'user2:pass2' | chpasswd" in order to change the password of the user2
                – Jose
                Jan 9 at 13:22










              • for i in server{1,2,3}: do ssh -t user@$i 'sudo su - ; passwd user2 ' done cant remember if that prompts for password or not
                – ssvegeta96
                Jan 9 at 16:18




















              • I can't do that because the server have policies where the only user can execute passwd is root. In order to change the password I have to do the next steps: 1) connect to a server with my user 2) using "sudo" to connect as a root 3) execute "echo 'user2:pass2' | chpasswd" in order to change the password of the user2
                – Jose
                Jan 9 at 13:22










              • for i in server{1,2,3}: do ssh -t user@$i 'sudo su - ; passwd user2 ' done cant remember if that prompts for password or not
                – ssvegeta96
                Jan 9 at 16:18


















              I can't do that because the server have policies where the only user can execute passwd is root. In order to change the password I have to do the next steps: 1) connect to a server with my user 2) using "sudo" to connect as a root 3) execute "echo 'user2:pass2' | chpasswd" in order to change the password of the user2
              – Jose
              Jan 9 at 13:22




              I can't do that because the server have policies where the only user can execute passwd is root. In order to change the password I have to do the next steps: 1) connect to a server with my user 2) using "sudo" to connect as a root 3) execute "echo 'user2:pass2' | chpasswd" in order to change the password of the user2
              – Jose
              Jan 9 at 13:22












              for i in server{1,2,3}: do ssh -t user@$i 'sudo su - ; passwd user2 ' done cant remember if that prompts for password or not
              – ssvegeta96
              Jan 9 at 16:18






              for i in server{1,2,3}: do ssh -t user@$i 'sudo su - ; passwd user2 ' done cant remember if that prompts for password or not
              – ssvegeta96
              Jan 9 at 16:18












              up vote
              0
              down vote













              The ansible module "user" should work for your problem. But you have to provide the already crypted password-hash instead.
              Make the hash with:



              mkpasswd --method=sha-512


              Execute a ansible adhoc-command like this with the quoted generated hash-string:



              ansible -v all -i <hostname>, --become --extra-vars 'ansible_become_pass=<sudo password> ansible_password=<sudo password>' --user=<ssh user> -k -m user --args='name=root update_password=always password="$6$IZjuXoio1$zHpQQDZGDPwG8mr2R6Mrt1C8Nqstui75enT/o0oSVJ3M6rqff8993kmAaTgbc9q9HTgPD2jtZukEqgeIGKfUN0"'


              Prerequesites: ansible and sshpass on client machine.






              share|improve this answer

























                up vote
                0
                down vote













                The ansible module "user" should work for your problem. But you have to provide the already crypted password-hash instead.
                Make the hash with:



                mkpasswd --method=sha-512


                Execute a ansible adhoc-command like this with the quoted generated hash-string:



                ansible -v all -i <hostname>, --become --extra-vars 'ansible_become_pass=<sudo password> ansible_password=<sudo password>' --user=<ssh user> -k -m user --args='name=root update_password=always password="$6$IZjuXoio1$zHpQQDZGDPwG8mr2R6Mrt1C8Nqstui75enT/o0oSVJ3M6rqff8993kmAaTgbc9q9HTgPD2jtZukEqgeIGKfUN0"'


                Prerequesites: ansible and sshpass on client machine.






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  The ansible module "user" should work for your problem. But you have to provide the already crypted password-hash instead.
                  Make the hash with:



                  mkpasswd --method=sha-512


                  Execute a ansible adhoc-command like this with the quoted generated hash-string:



                  ansible -v all -i <hostname>, --become --extra-vars 'ansible_become_pass=<sudo password> ansible_password=<sudo password>' --user=<ssh user> -k -m user --args='name=root update_password=always password="$6$IZjuXoio1$zHpQQDZGDPwG8mr2R6Mrt1C8Nqstui75enT/o0oSVJ3M6rqff8993kmAaTgbc9q9HTgPD2jtZukEqgeIGKfUN0"'


                  Prerequesites: ansible and sshpass on client machine.






                  share|improve this answer












                  The ansible module "user" should work for your problem. But you have to provide the already crypted password-hash instead.
                  Make the hash with:



                  mkpasswd --method=sha-512


                  Execute a ansible adhoc-command like this with the quoted generated hash-string:



                  ansible -v all -i <hostname>, --become --extra-vars 'ansible_become_pass=<sudo password> ansible_password=<sudo password>' --user=<ssh user> -k -m user --args='name=root update_password=always password="$6$IZjuXoio1$zHpQQDZGDPwG8mr2R6Mrt1C8Nqstui75enT/o0oSVJ3M6rqff8993kmAaTgbc9q9HTgPD2jtZukEqgeIGKfUN0"'


                  Prerequesites: ansible and sshpass on client machine.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 at 15:30









                  linuxlupus

                  113




                  113






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f415684%2fssh-change-password-using-root%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

                      List directoties down one level, excluding some named directories and files

                      list processes belonging to a network namespace

                      list systemd RuntimeDirectory mounts