Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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've created a user ... but forgotten the password

mysql> create user 'blayo'@'%' identified by 'right';

Which Linux command line tool can encrypt the password the same way mysql 5.5 does ?

mysql> select Password,User from mysql.user
*920018161824B14A1067A69626595E68CB8284CB | blayo |

...to be sure I use the right one

$ tool right
share|improve this question
Why can't you just login as an administrator and define a new password for blayo? That's definitively faster than running through trillions of possible character combinations to find the right one. – patrix Aug 6 '12 at 20:10
Please do not post the same question to multiple Stack Exchange sites. The DBA version has been answered as well, which is a waste of effort. – Gilles Aug 6 '12 at 23:57
up vote 10 down vote accepted

Well, the trivial (perhaps cheating) way would be to run:

mysql -NBe "select password('right')"

This will produce a password using whatever password hashing scheme your version of mysql uses. [EDIT: added -NB, which gets rid of the column names and ascii table art.]

share|improve this answer
And, handily, this will store your shiny new password in clear text both in your MySQL log and in your bash account history log. – Craig Sep 14 '14 at 18:16

The hash is sha1(sha1(password)). Since there is no salt (which is a grave security flaw), you can look up the hash in a table.

With just POSIX tools plus sha1sum from GNU coreutils or BusyBox, sha1(sha1(password)) is annoying to compute because the sha1sum command prints out the digest in hexadecimal and there is no standard tool to convert to binary.

awk "$(printf %s 'right' | sha1sum |
       sed -e 's/ .*//' -e 's/../, 0x&/g' \
           -e 's/^/BEGIN {printf "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c"/' \
           -e 's/$/; exit}/')" | sha1sum

Python has standard digests in its standard libraries, so it's a simple one-liner.

printf %s 'right' |
python -c 'from hashlib import sha1; import sys; print sha1(sha1(sys.stdin.read()).digest()).hexdigest()'

Or with the password inside the one-liner:

python -c 'from hashlib import sha1; print sha1(sha1("right").digest()).hexdigest()'
share|improve this answer
I love this answer in spirit, but it isn't quite working. When I hit the enter key, it still wants to keep reading input instead of moving on. Also, the input echos back to the screen, which might not be desirable. How about something like this, though? unix.stackexchange.com/a/155583/37401 – Craig Sep 14 '14 at 19:33
@Craig How to read interactive input is beyond the scopre of the question. I assume that the script already has the password; usually it's stored in a file, it's rare to type a database password interactively. If you want to read interactive input, you can use the read shell builtin to read a line; run stty echo first if you want to turn off echo and ssty +echo to turn it back on. In Python, you can use the getpass module. – Gilles Sep 14 '14 at 19:49
Yeah, I know... and fair enough! I just hate horrible security the further down the road I go, and MySQL security is pretty horrible in general, so I have this knee-jerk tendency to want to inject a little security insight wherever I get a chance. ;-) – Craig Sep 14 '14 at 20:42

It still kind of blows my mind that MySQL doesn't bother obfuscating passwords on the command line and in logs. And that's the only reason I'm adding an answer rather than just commenting on @Gilles answer.

So, of course, you could just log into MySQL as an admin and set a new password for your blayo user, as @patrix suggested.

However, the standard way to do that is by using MySQL's password() function, which takes a plaintext password as an argument (seriously?).

If you do that, you leave the plaintext version of your MySQL user's password sitting around in your bash history and in your MySQL logs, for easy retrieval later on by whoever manages to get access to those log files.

Wouldn't it be better to have a little utility that would prompt for the password, without echoing it to the screen or to your logs, then provide you with the resulting MySQL-compatible hash?

So, modifying @Gilles's answer just a bit, how about a little shell script that uses Python, like the following. You could easily modify this to run a SQL statement against your MySQL database to set the password all at once. But even without going that far, just copy and paste the resulting hash into a SQL statement to update the users table:


mysqlpwd=$(/usr/bin/python -c 'from hashlib import sha1; import getpass; print "*" + sha1(sha1(getpass.getpass("New MySQL Password:")).digest()).hexdigest()')

echo $mysqlpwd
share|improve this answer
@HalosGhost; what was the edit about? It didn't change anything? ;-) – Craig Sep 15 '14 at 2:42
It enforced syntax highlighting (as noted in the edit summary) and made obvious by the actual text of the edit. – HalosGhost Sep 15 '14 at 3:06
Ah, so. Honestly didn't realize what you'd done (you obviously added the <!-- language: lang-bsh --> line). Handy! I like it. Thanks! – Craig Sep 15 '14 at 4:29

If you crated a user and forgotten the password this is what you should do and it's easier.

This also happened to me a dozen times while I was doing 10 billion things at once. The best way that I recovered my password was:

stopped the mysql server completely first

issued mysqld_safe --skip-grant-tables &

then I logged in as root without issuing a password, just mysql -u mysql-user (it lets you in after the mysqld_safe)

then go to the mysql > user table and update the password for the user

then go back and restart mysql

see if that works and let us know.

share|improve this answer

Some one-liners:

MySQL (may require you add -u(user) -p):

mysql -NBe "select password('right')"


python -c 'from hashlib import sha1; print "*" + sha1(sha1("right").digest()).hexdigest().upper()'


perl -MDigest::SHA1=sha1_hex -MDigest::SHA1=sha1 -le 'print "*". uc sha1_hex(sha1("right"))'


php -r 'echo "*" . strtoupper(sha1(sha1("right", TRUE))). "\n";'

All output:


share|improve this answer

MySQL 4.1+ uses double SHA1


> SELECT SHA1(UNHEX(SHA1("right")))

sh-3.2# php -r 'echo "*" . sha1(sha1("right", TRUE)). "\n";'

This algorithm can be easily ported to other languages​​. A difference is that password hashes in the "select password" always begin with a “*” character.

share|improve this answer

One more using the shell:

echo -n 'right' | sha1sum | xxd -r -p |\
sha1sum | tr '[a-z]' '[A-Z]' | awk '{printf "*%s", $1}'


  1. echo -n print without linebreak
  2. sha1sum first SHA1
  3. xxd -r -p unhex the hash
  4. sha1sum second SHA1
  5. tr '[a-z]' '[A-Z]' convert to uppercase
  6. awk '{print "*" $1}' add leading *

More details:

Between 2. and 3. an optional awk '{printf "%s", $1}' step could be insterted for newline- and hyphen-removal. But xxd will ignore them anyway (Thanks to dave_thompson_085).

Furthermore step 5 and 6 could be done at once by replacing them with {print "*" toupper($1)} (Thanks to dave_thompson_085).

share|improve this answer
awk can do the uppercase, and outputting an incomplete (last) line to terminal is incovenient: .. | sha1sum | awk '{print "*" toupper($1)}'. And you don't need to worry about the NL and even the hyphen on the xxd -r -p, they're ignored. – dave_thompson_085 Dec 6 '15 at 2:54

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.