Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to protect a sub domain where the user is only given access for 1 day. Is it possible to set up a cron job that will automatically update a .htpasswd file with a new password daily?

share|improve this question
up vote 3 down vote accepted

You should probably use an application that was meant to do this, rather than hacking something together with cron. But, if you must make it hackalicious...

You'll have to run htpasswd in "batch mode". From the htpasswd man page:

htpasswd -mb /usr/web/.htpasswd-all jones Pwd4Steve

          Encrypts the password from the command line (Pwd4Steve)
          using the MD5 algorithm, and stores it in the specified

Something like this should work:

# There are a bunch of ways to get a new password, this is one:
NEW_PASS=`cat /dev/urandom|tr -dc "a-zA-Z0-9-_\$\?"|fold -w 9|head -n1`
# This should do it
# You'll probably want to email the password to your user, 
# so they can actually use the new password
share|improve this answer

Normally I would only answer: Yes it is possible.

But you can even integrate a database where the user and password is stored. For example mod_auth_mysql. And then you can even enable/disable/modify everything with standard SQL. I'll only leave a hint to this question and the homepage. Sometimes this approach does not work but in this case you could go around this by replacing mod_auth_mysql with mod_auth_pam and then integrate the MySQL stuff into PAM. But I'll let this as an exercise for the reader...

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.