Get started with 33% off your first certification using code: 33OFFNEW

How to reset WordPress admin password via command line

2 min read
Published on 12th January 2024

Forgetting the admin password to your WordPress site can be frustrating. While there are various methods to reset it, using the command line is often the fastest way, especially if you have access to the server where your WordPress site is hosted. This guide will walk you through resetting your WordPress admin password using WP-CLI, a powerful command-line tool for managing WordPress sites.

Understanding WP-CLI

WP-CLI is a command-line interface for WordPress, allowing you to manage almost every aspect of your site via the command line. From updating plugins and themes to managing users and configurations, WP-CLI is a robust tool for WordPress administration.

Prerequisites

  • Access to your server via the command line.

  • WP-CLI installed on your server. If it's not installed, you can download and install it following the instructions on the WP-CLI website.

Step 1: Access Your Server

Log into your server using SSH or any other method you usually use. You need to have command-line access to the server where your WordPress site is hosted.

Step 2: Navigate to Your WordPress Directory

Change your directory to where your WordPress site is installed. This is typically within a directory like public_html, www, or htdocs.

cd /path/to/your/wordpress

Step 3: Identify the Admin User

If you remember your admin username, you can skip this step. If not, list all users to find the admin username:

wp user list

Look for the user with 'administrator' in the 'roles' column.

Step 4: Reset the Password

Use the following WP-CLI command to reset the password. Replace 'adminusername' with your actual admin username:

wp user update adminusername --user_pass=newpassword

Replace newpassword with your new desired password.

Step 5: Verify the Change

You can now try logging into your WordPress dashboard using the new password.

Alternative Method: Resetting via MySQL

If you don't have WP-CLI installed and can't install it for some reason, you can reset the password directly via MySQL. However, this method is more complex and requires caution.

1. Log into MySQL:

   bash    mysql -u username -p    

2. Select your WordPress database:

   sql    USE wordpress_database;    

3. Update the password:

   sql    UPDATE wp_users SET user_pass = MD5('newpassword') WHERE user_login = 'adminusername';    

Replace wordpress_database, newpassword, and adminusername with your actual database name, new password, and admin username.

Being locked out of your WordPress admin area can disrupt your website management. Knowing how to reset your admin password via the command line is a valuable skill, ensuring you can quickly regain access to your site in such situations. With WP-CLI, this process is straightforward and efficient, making it a preferred method for many WordPress administrators.