How to change Wordpress admin password from cli or shell

Author: Murtaza Nooruddin
featured image

A common question asked often is how to reset an admin password from shell on Linux if you have access to shell on your hosting server. Surprisingly this is easy and there is more than one way of achieving this. The most common method is directly from database using PHPAdmin, which I will not cover as I feel it’s longer and not as fast as the poweruser methods below.

A quick and simple way to do this is using wordpress CLI. This is usually installed on most hosting servers like Siteground, wpengine, bluehost etc.

Verify that you have WP CLI installed by running

wp cli version

If it outputs something like “WP-CLI 2.4.0”, you are good to go. Else you can install globally as root or in your home folder, download and install WP CLI using the instructions for your operating system. You don’t need root access, you can download and run from your home folder.

Next, simply go to your wordpress installation directory:

This assumes your user name is “admin” and installation directory is “/var/www/html/mysite/public”. Feel free to adapt.

cd /var/www/html/mysite/public 
wp user update admin --user_pass=mynewpassword

Following are a few bonus features, you can skip, but the CLI gives a lot of raw power and I feel it’s noteworthy to mention a few:

wp user update admin --user_email=mynewemail@noorix.com --role=admin

How to add a new wordpress admin user using wordpress CLI

…or if you prefer just create a new admin user instead of modifying existing password

wp user create adminuser2 noorix@example.com --role=admin --user_pass=userpassword --display_name="All new powerful admin"

METHOD 2 - Modify using mysql (the hardcore way)

One way is to use PHPAdmin which is almost always installed on your hosting provider which offers cpanel or siteground type admin interface. I am not covering that, as heaps of tutorials are available on that. However, if you have shell and want to do it via mysql the following queries take care of it:

Fetch the database credentials from wp-config.php file in your wordpress directory. Then simply use mysql commands below substituting your host, user and password.

mysql -u dbuser -h localhost -p
password: <enter your db password>

mysql> use wordpress_database;

mysql [wordpress_database]> select id, user_login, user_pass from wp_users;

Note that wp_users table might be different as some hosting providers and security plugins rename tables by adding a prefix. From the list of user get the ID and execute password reset command below:

+----+------------+------------------------------------+
| id | user_login | user_pass                          |
+----+------------+------------------------------------+
|  1 | admin1     | $P$BPZa97bVMEl3vlRNDsfKWxVocUgmFK0 |
|  2 | user12     | $P$BPZa97bVMEl3vlRNDsfKWxVocUgmFK0 |
+----+------------+------------------------------------+

mysql [wordpress_database]> update wp_users set user_pass=MD5('MynewPassWord') where id=1

The MD5 function is the secret in the query to setting the hashed password.

How to add a new wordpress admin user using mysql

user wordress_database;

insert into wp_users (user_login,user_pass, user_nicename,user_email,user_url,user_registered,user_activation_key,user_status,display_name) values ('username','','username','your@emailaddress.com','','2022-04-05 09:43','',0,'Username');
SELECT * FROM wp_users; 

Get the ID of the newly created user, and add it to wp_usermeta for admin capability

insert into wp_usermeta (user_id, meta_key, meta_value) values (1,'wp_capabilities','a:1:{s:13:"administrator";b:1;} ');

That’s it, you have created a new user you can safely login with

Leave your feedback

Love to hear your feedback or suggestions for improvements.