Step-by-Step Guide to Resetting WordPress User/Admin Password Using WP-CLI
Step 1: Install WP-CLI
WP-CLI is a command-line interface for managing WordPress installations. To install WP-CLI, follow these steps:
# Download WP-CLI
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
# Make the file executable
chmod +x wp-cli.phar
# Move it to a directory that is in your system's PATH
sudo mv wp-cli.phar /usr/local/bin/wp
# Check if WP-CLI is installed correctly
wp --info
Step 2: Update WP-CLI (Optional)
If you already have WP-CLI installed, you can update it to the latest version:
sudo wp cli update
Step 3: Switch to the Web Server User
To manage WordPress files, you often need to run WP-CLI commands as the web server user. This is common www-data
or a user that your web server runs under. You can switch to this user by using either su
or sudo
:
# Switch to the web server user
su - www-data
# OR
sudo -u www-data -i
Step 4: List WordPress Users
Navigate to the path where your WordPress installation is located. Replace /var/www/example.com
with the path to your WordPress installation.
# List all users in WordPress
wp --path=/var/www/example.com user list
If you prefer to run WP-CLI as the root
user, use the --allow-root
flag:
# List all users with root privileges
wp --allow-root --path=/var/www/example.com user list
Step 5: Filter Admin Users
To filter only the admin users, you can use a combination of wp user list
with the --role
flag:
# List only admin users
wp --path=/var/www/example.com user list --role=administrator
For running as root:
# List only admin users with root privileges
wp --allow-root --path=/var/www/example.com user list --role=administrator
This command will list all users with the administrator
role.
Step 6: Reset the Password for a Specific User
To reset the password for a specific user, use the wp user update
command. Replace USER_ID
with the user ID of the admin whose password you want to reset, and NEW_PASSWORD_HERE
with the new password.
For a regular user:
# Update the password for user ID 1
wp --path=/var/www/example.com user update 1 --user_pass=NEW_PASSWORD_HERE
Or specify the username:
# Update the password for a specific username
wp --path=/var/www/example.com user update username --user_pass=NEW_PASSWORD_HERE
To do this as the root user:
# Update the password for user ID 1 as root
wp --allow-root --path=/var/www/example.com user update 1 --user_pass=NEW_PASSWORD_HERE
Step 7: Return to the Root User
After you have finished resetting the password, you can return to your root shell:
exit
Additional Notes
- Running Commands as Root (
--allow-root
): WP-CLI commands can be executed as the root user by adding the--allow-root
flag. This is sometimes necessary when there are permission issues, or when you are working directly from a root shell. - Security Considerations: Running commands as root should be done with caution to avoid unintended changes or security risks. Always ensure that the commands you are running are safe and well-understood.
Conclusion
Using WP-CLI with either a regular user or the root user is a powerful way to manage WordPress installations from the command line. Always remember to use secure passwords and consider using a password manager to generate and store complex passwords.
Replace example.com
with your actual domain or path where your WordPress is installed. This guide provides flexibility for both regular users and those who need to run commands as root.