Script to update user password in batch mode using pwgen and chpasswd

by on April 10, 2008 · 1 comment

  1. #!/bin/bash
  2. # Script to update user password in batch mode
  3. # You must be a root user to use this script
  4. # -------------------------------------------------------------------------
  5. # Copyright (c) 2005 nixCraft project
  6. # This script is licensed under GNU GPL version 2.0 or above
  7. # -------------------------------------------------------------------------
  8. # This script is part of nixCraft shell script collection (NSSC)
  9. # Visit http://bash.cyberciti.biz/ for more information.
  10. # ----------------------------------------------------------------------
  11. # /root is good place to store clear text password
  12. FILE="/root/batch.passwd"
  13.  
  14. # get all non-root user account
  15. # By default on most linux non-root uid starts
  16. # from 1000
  17. USERS=$(awk -F: '{ if ( $3 > 1000 ) print $1}' /etc/passwd)
  18.  
  19. # create file with random password
  20. echo "Generating file, please wait..."
  21.  
  22. # overwrite file, this is bash specific a better solution is cat > $FILE
  23. >$FILE
  24.  
  25. for u in $USERS
  26. do
  27. p=$(pwgen -1 -n 8) # create random password
  28. echo "$u:$p" >> $FILE # save USERNAME:PASSWORD pair
  29. done
  30. echo ""
  31. echo "Random password and username list stored in $FILE file"
  32. echo "Review $FILE file, once satisfied execute command: "
  33. echo "chpasswd < $FILE"
  34.  
  35. # Uncomment following line if you want immediately update all users password,
  36. # be careful with this option, it is recommended that you review $FILE first
  37. # chpasswd < $FILE


4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

  • ahmed

    hi
    thanks man
    this is very good bash script

Previous Script:

Next Script: