Change password shell script

#!/usr/local/bin/expect -f
# Password change shell script, tested on Linux and FreeBSD
# ----------------------------------
# It need expect tool. If you are using Linux use following command
# to install expect
# apt-get install expect
# FreeBSD user can use ports or following command:
# pkg_add -r -v expect
# ----------------------------------
# If you are using linux change first line
# From:
#!/usr/local/bin/expect -f
# To:
#!/usr/bin/expect -f
# -----------------------------------------------
# Copyright (c) 2006 nixCraft project 
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit for more information.
# -------------------------------------------------------------------------
# display usage
if {$argc!=2} {
   send_user "usage: $argv0 username password \n"
# script must be run by root user
set whoami [exec id -u]
if {$whoami!=0} {
   send_user "You must be a root user to run this script\n"
set timeout -1
match_max 100000
# stopre password
set password [lindex $argv 1]
# username
set user [lindex $argv 0]
# opem shell
spawn $env(SHELL)
# send passwd command
send -- "passwd $user\r"
expect "assword:"
send "$password\r"
expect  "assword:"
send "$password\r"
send "\r"
expect eof
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics via:
Category List of Unix and Linux commands
File Management cat
Firewall Alpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN Alpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
17 comments… add one
  • Blackuser Jun 28, 2012 @ 10:44


    best would be to edit the script:

    send -- "passwd $user\r"


    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 Nov 24, 2010 @ 22:35

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

  • rogi Jun 30, 2010 @ 15:20

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

  • Luke Sep 21, 2009 @ 18:29

    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??

    pass=> /root/pass
    cat /root/pass | chpasswd
    rm -f /root/pass
    sudo echo root:$pass >> /root/pass
    sudo cat /root/pass | chpasswd
    sudo rm -f /root/pass
    exit 0 #the only way you should ever exit script

    • Luke Sep 21, 2009 @ 19:00

      THAT is not my script?? Lemme try again…

      pass=> /root/pass
      cat /root/pass | chpasswd
      rm -f /root/pass
      sudo echo root:$pass >> /root/pass
      sudo cat /root/pass | chpasswd
      sudo rm -f /root/pass
      exit 0

      • Luke Sep 21, 2009 @ 19:02

        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?
        pass=> /root/pass
        cat /root/pass | chpasswd
        rm -f /root/pass
        sudo echo root:$pass >> /root/pass
        sudo cat /root/pass | chpasswd
        sudo rm -f /root/pass
        exit 0

  • Obxnux Aug 18, 2009 @ 22:57

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

  • Vlad (Small Business Blog) Aug 3, 2009 @ 4:43

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

  • Fred Jun 17, 2009 @ 0:18

    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 Jul 3, 2011 @ 6:06

      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 Dec 12, 2008 @ 0:30

    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 Nov 16, 2010 @ 8:43

      Worked for me.

      • text Feb 8, 2011 @ 13:34


        • Mark Mar 29, 2011 @ 17:37

          Epic Win!!!
          Works here 3

          • lokeshw24 Sep 1, 2011 @ 19:34

            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 Aug 18, 2008 @ 16:12

    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 @ 19:01

    Long story short.

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

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.