Linux – Programmatically Change Password


If you need to script a password change you have two main options:

  1. Write a script that interacts with the command line and executes passwd
  2. Use the usermod command

Option one is a bit of a mess and option two requires an encrypted version of the password. At first, we couldn’t figure out how to encrypt the password but then we realized that you could use the Perl crypt function.

An example of number two:


usermod -p `perl -e 'print crypt("test", "salt")'` root;

That’s all there is to it!



  1. stharmon  •  Nov 7, 2008 @04:24

    So what is the password actually set to? What is the “test”, and “salt”?

  2. Ryan  •  Nov 10, 2008 @18:49

    The password in this example is “test”. For more information see the Perl crypt() function doc.

Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>