Creating a password for a user in Drupal 7

Ok, so you've exported your database from one system to another, but your login isn't working. Why? I'm not really sure but it seems the hash relies on the hostname in Drupal 7? Haven't really dug in yet to figure out the specifics. So with Drupal 6 you would just md5 a password and then stick it in the database. But oh no, we can't md5 anymore in D7, so what do we do.

Well a bit of grepping found scripts/ shell script in our Drupal install. So, if you need to create a new password for you user, just run (from your Drupal directory):

scripts/ yourpassword

You have to run this from your Drupal directory, if you run it from the scripts directory itself (for example, ./ yourpassword) then it will fail

where your password is the desired password for the user. Then hop over into MySQL and update the user with the new password hash, for example:

"UPDATE user SET pass='$S$CpiOFu0x5iXapILxJCsVJGbh2fHB11HLGjBDp2XbfofmpiqOd1xj' where uid='1';"

And yay, now we can login.

Side note: Wanted to update this to say, in some environments, you might get the error: "-bash: ./scripts/ /usr/bin/php: bad interpreter: No such file or directory"

One can get around this by creating a custom php script and using "drush php-script" to run it. So create test.php and in it put the code:

require './includes/';
print user_hash_password('yournewpassword');

Then from command line in your Drupal root directory run:
drush php-script test.php

Obviously, that is a less than preferable way to do things, but it works in a jam.


Even easier...

Nice. You could make it even easier with Drush and a little shell scripting. I'm sure I'll write a script for this soon... something like:

drush sql-query "UPDATE user SET pass='$(./scripts/ $1)' WHERE uid = 1;"

That's interesting, I'm going

That's interesting, I'm going to try that out, not sure what I think of the approach, but if it was safe and sane then it would make life easier yes.

Command line n00b

This seems to be the official solution to this issue, but I have to admit I'm having some trouble getting the script to return the hash in the Windows command prompt. I seem to be doing everything properly -- running it from the root, surrounding the text in quotes because it's Windows -- but all that returns is the set of instructions from the script, without any accompanying hash. Is it supposed to output there, or am I missing something? Am I supposed to be using something besides cmd.exe in Windows?

Thanks so much!

I admit I haven't tested it

I admit I haven't tested it on Windows, BUT my guess is that you should be running this from Windows Powershell, not from a normal cmd.exe command prompt. I'll give it a try on a Win box soon and report back. If that doesn't work, you may have to use cygwin to rush the bash script on windows.

I keep getting the message

I keep getting the message thats cripts is not recognized as a command. Please...What am I doing wrong?


i had to include another file to make it work
one thing i noticed is that the password changes each time it is hashed
require './includes/';
require './includes/';
print user_hash_password('newpass');

Add new comment

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

Comment using an existing account (Google, Twitter, etc.)