≡ Menu

Change password shell script

{ 17 comments… add one }
  • Blackuser June 28, 2012, 10:44 am

    Hi,

    best would be to edit the script:

    send -- "passwd $user\r"

    to

    send -- "LC_ALL=C passwd $user\r"

    The script is universally usable after that change. Otherwise the script wont run for german e.g. I edited, and it works perfectly for ubuntu.

    Edited by Admin: Added pre tags to the code.

  • Leonardo November 24, 2010, 10:35 pm

    chage -d 1 user_name —> with this you need to change your password every day force by the root-

  • rogi June 30, 2010, 3:20 pm

    Without „set timeout -1“ this script working well, you have just to wait some seconds.

  • Luke September 21, 2009, 6:29 pm

    Ok, I’m trying to create a script that crontab will run every day that changes root pass to a random 20 digit string. (obxnux, I think we’re wanting something similar) I have tried chpasswd, but it doesn’t work, sets root pwd to nill. (don’t worry, I’ve rectified that before this post) My script’s at the end, what am I doing wrong??

    #!/bin/bash
    iam=$(whoami)
    pass=> /root/pass
    cat /root/pass | chpasswd
    rm -f /root/pass
    else
    sudo echo root:$pass >> /root/pass
    sudo cat /root/pass | chpasswd
    sudo rm -f /root/pass
    fi
    exit 0 #the only way you should ever exit script

    • Luke September 21, 2009, 7:00 pm

      THAT is not my script?? Lemme try again…

      #!/bin/bash
      iam=$(whoami)
      pass=> /root/pass
      cat /root/pass | chpasswd
      rm -f /root/pass
      else
      sudo echo root:$pass >> /root/pass
      sudo cat /root/pass | chpasswd
      sudo rm -f /root/pass
      fi
      exit 0

      • Luke September 21, 2009, 7:02 pm

        I’m begining to think there’s something wrong here, under the “iam” line is supposed to be one that assigns a random 20 digit phrase to $pass?? why won’t it post correctly?
        #!/bin/bash
        iam=$(whoami)
        pass=> /root/pass
        cat /root/pass | chpasswd
        rm -f /root/pass
        else
        sudo echo root:$pass >> /root/pass
        sudo cat /root/pass | chpasswd
        sudo rm -f /root/pass
        fi
        exit 0

  • Obxnux August 18, 2009, 10:57 pm

    Hi, I would like to know how can I make a script for change root password using crontab. Thank you

  • Vlad (Small Business Blog) August 3, 2009, 4:43 am

    Fred,
    so executing history -c at the end would not help? Checked on my machine and it works.

  • Fred June 17, 2009, 12:18 am

    All of these are bad ideas. You do not want your password being processed via the shell. Most shells keep a history file of commands executed – and this will show up in them. In general, you do not want your password saved in cleartext anywhere, regardless of file system controls.

    • Frank July 3, 2011, 6:06 am

      The password command doesn’t store the information in clear text. Running a history command will not return with:

      17: passwd 123456

      The user can’t even see if they’re typing in the password correctly so really the only way someone could get the password is if they were watching you type it in and you were a somewhat slow and “distinguished” typist.

  • goteguru December 12, 2008, 12:30 am

    chpasswd is your friend:
    echo username:password | chpasswd

    You can even do
    cat passlist.txt | chpasswd
    where passlist.txt is a newline delimited list of username:password pairs.

    • f varas November 16, 2010, 8:43 am

      Thanks!!
      Worked for me.

      • text February 8, 2011, 1:34 pm

        THANKS ! WORKS HERE 2

        • Mark March 29, 2011, 5:37 pm

          Epic Win!!!
          Works here 3
          Thanx

          • lokeshw24 September 1, 2011, 7:34 pm

            Doesnt work here 1 :(

            It gave the following error :
            echo one:different | chpasswd ( “one” is username & “different” is the new password i wish to give it )
            Changing password for one.
            chpasswd: (user one) pam_chauthtok() failed, error:
            Authentication token manipulation error
            chpasswd: (line 1, user one) password not changed

            Pls help ….

  • mi6oo2 August 18, 2008, 4:12 pm

    Um, yea Tamilan – that doesn’t actually work. Did you try it yourself? All it does on my machine is remove a shell from the test user and not change the password.
    The problem I’m running into with the above script is that it isn’t properly returning to the cli when done. In fact it is a pain in the ass to run within another script.

  • Tamilan May 20, 2008, 7:01 pm

    Long story short.

    echo $password | /usr/bin/passwd –stdin user1

Security: Are you a robot or human?

Leave a Comment