How to create admin user in Magento 2 via Command Line

Managing users in Magento 2 is an essential task for store owners and developers. One common requirement is creating an admin user without accessing the Magento backend. This is especially useful when you are locked out of the admin panel or setting up a fresh Magento installation. In this guide, we will show you how to create an admin user in Magento 2 using the command line, step by step.

Why Use Command Line to Create Admin Users?

Using the command line offers several advantages:

  • Quick and Efficient: No need to navigate through the admin interface.
  • Secure: Reduces exposure to potential backend vulnerabilities.
  • Automatable: Useful for scripting and automation in deployments.
  • Essential for Developers: Often required in local development or staging environments.

Prerequisites

Before you start, make sure you have:

  1. Magento 2 installed on your server.
  2. Access to the command line (SSH) on your hosting or local environment.
  3. Proper file permissions for your Magento installation.

Step-by-Step Guide to Creating an Admin User

Step 1: Open Command Line Interface

Connect to your server using SSH, or open a terminal if working locally.


ssh user@your-server-ip
cd /path/to/magento2


Step 2: Use the Magento CLI Command

Magento provides a built-in command to create admin users. The syntax is:

php bin/magento admin:user:create --admin-user="adminusername" --admin-password="Admin@1234" --admin-email="email@example.com" --admin-firstname="FirstName" --admin-lastname="LastName"

Explanation of Parameters:

  • --admin-user: The username for your new admin account.
  • --admin-password: A secure password following Magento’s password requirements.
  • --admin-email: Admin email address.
  • --admin-firstname and --admin-lastname: Admin’s first and last names.

Tip: Make sure your password meets Magento 2’s complexity requirements: at least 8 characters with uppercase, lowercase, number, and special character.

Step 3: Verify the Admin User Creation

After executing the command, you should see a success message like:


Created Magento administrator user named adminusername

You can now log in to the Magento 2 admin panel with the new credentials.

Step 4: Troubleshooting Common Issues

Permission Denied: Run the command with sudo if necessary.
Password Not Accepted: Ensure your password meets Magento’s complexity rules.
Command Not Found: Make sure you are in the Magento 2 root directory and have PHP installed.

Step 5: Optional – Assign Roles or Customize Permissions

If you need to modify the role of the newly created admin user, you can do this in the backend under System > Permissions > User Roles, or via CLI scripts for advanced setups.

Benefits of Using CLI for Admin Management

Faster User Management: No page reloads or browser limitations.
Ideal for Automation: Can be included in deployment scripts.
Safe and Reliable: Minimizes direct interaction with the database.


Creating an admin user in Magento 2 via command line is a quick, secure, and efficient way to manage your store’s administrative access. Whether you’re a developer, store owner, or system administrator, mastering this CLI command ensures you can regain access or set up new admin accounts without hassle.