Magento 2 lets you create admin users directly from the command line using a single CLI command, which is especially useful for developers setting up test environments or automating initial store configurations. This method skips the manual admin panel UI and runs in seconds, provided you have the necessary permissions and dependencies met.

What You'll Need

  • A running Magento 2 installation (version 2.3.x or later, depending on command availability)
  • Access to the command line (Terminal on macOS/Linux or Git Bash/Cmd on Windows)
  • Admin credentials with sufficient permissions to execute CLI commands
  • PHP (version 7.4 or higher, depending on Magento version) installed globally
  • Composer installed and configured (for dependency management)
  • The Magento CLI tool must be included in your system PATH (typically via composer's global install)
  • Your store's database credentials (username/password) if the command requires them
  • An existing Magento admin user with sufficient privileges to run the command (usually the default admin account)

Step-by-Step

  1. Check your Magento 2 version: Open your terminal and run: php bin/magento --version Note the version number (e.g., 2.4.3). This determines which exact command syntax you'll use. If you get an error about missing CLI: Run composer global require --no-root php-magento2-cli/php-magento2-cli and update your PATH accordingly.
  2. Navigate to your Magento root directory: Use the cd command to navigate to your Magento installation's root folder: cd /path/to/your/magento/installation Replace path/to/your/magento/installation with the actual path (e.g., /var/www/html/your-store).
  3. Create the admin user via CLI: For Magento 2.3.x: php bin/magento admin:user:create --name=YourUsername --email=admin@example.com --password=YourSecurePassword --first-name=First --last-name=Last --is-admin=true --store=admin For Magento 2.4.x+: php bin/magento admin:user:create --user=admin --password=YourSecurePassword --email=admin@example.com --first-name=First --last-name=Last --is-admin Key parameters explained:
    • --name or --user: Display name for the user
    • --email: Unique admin email address
    • --password: Secure password (must meet complexity requirements)
    • --is-admin: Boolean flag for administrator role
    • --store or --store-code: Store code (defaults to admin)
  4. Verify the user was created: Run: php bin/magento admin:user:list You should see your newly created user listed with their details.
  5. Assign role if needed: By default Magento creates admin users with basic permissions. To assign specific roles: php bin/magento admin:user:role:assign --user=admin --role=admin --store=admin (Replace admin with your new username and admin with desired role)
  6. Enable the user's account: If the user appears as inactive, enable them: php bin/magento admin:user:status:set --user=admin --status=active

Common Mistakes

  • Missing CLI permissions: If you get "Command not found" errors, verify your CLI is properly installed and accessible. Run which php to check PHP path. For Windows users, ensure Git Bash or WSL is properly configured.
  • Password complexity requirements: Magento enforces password policies. Common errors include:
    • Missing uppercase letters
    • Missing numbers
    • Missing special characters
    • Passwords shorter than 8 characters
    Use a password manager or complex, memorable phrase that meets these requirements.
  • Database connection issues: If commands fail with database errors, verify: php bin/magento --version shows your configuration is correct Your database credentials match the app/etc/env.php file The database server is running and accessible
  • Case sensitivity in command parameters: Magento CLI is case-sensitive. Ensure all parameter names match exactly (e.g., --is-admin, not --IsAdmin).
  • Version compatibility issues: Different Magento versions have slightly different CLI syntax. Always check: php bin/magento --version before running commands Upgrade to the latest supported version if needed Refer to your specific Magento version's documentation for exact syntax

Tips

  • Batch creation: For multiple users, store the command template in an external file (e.g., create_admin.sh) with variables, then source it: source create_admin.sh Example template: #!/bin/bash USERNAME="admin_$1" PASSWORD="SuperSecure$1" EMAIL="admin_$1@example.com" php bin/magento admin:user:create --user=$USERNAME --password=$PASSWORD --email=$EMAIL --first-name=Admin --last-name=User --is-admin --store=admin Then run with: ./create_admin.sh testuser
  • Default admin account management: To modify the default admin account (usually 'admin'): php bin/magento admin:user:edit --user=admin --email=newadmin@example.com --password=newpassword Then update your env.php to match the new credentials.
  • Secure sensitive data: Never commit passwords to version control. Use environment variables or a secure secrets manager for production environments.
  • Role-based access: Use the CLI to assign roles comprehensively: php bin/magento admin:user:role:assign --user=admin --role=manager --store=admin List available roles with: php bin/magento admin:user:role:list
  • Backup first: Always back up your database before running mass user creation commands, especially in production environments.
  • Database connection pooling: For large installations, add these environment variables before running CLI: export MAGENTO_DB_POOL_MAX_CONNECTIONS=50 export MAGENTO_DB_POOL_MIN_CONNECTIONS=20

Troubleshooting

If the command fails at any point, start with these verification steps:

  1. Verify CLI accessibility: Run php -m | grep magento to check if Magento CLI is loaded. If not, reinstall it: composer global require php-magento2-cli/php-magento2-cli --no-root
  2. Check database connectivity: Test with a simple query: php bin/magento db:status If this fails, verify your database credentials in app/etc/env.php:
        'backend' => [
            'host' => '127.0.0.1',
            'dbname' => 'magento_db',
            'username' => 'magento_user',
            'password' => 'secure_password'
        ],
        
  3. Clear cache and config: If permissions are correct but commands fail: php bin/magento cache:clean php bin/magento setup:upgrade Then retry the user creation.
  4. Check for Magento CLI errors: Run with verbose output for detailed error messages: php bin/magento admin:user:create --verbose Look for specific error codes that indicate what went wrong (e.g., 404 for invalid role).
  5. Alternative approach for older versions: If your version lacks the admin:user:create command, use this alternative:
    1. First create the user via database: mysql -u admin_user -p -e "INSERT INTO core_user (username, email, password, first_name, last_name, is_active) VALUES ('newuser', 'newuser@example.com', PASSWORD('SecureP@ssword'), 'New', 'User', 1);"
    2. Then assign admin role: mysql -u admin_user -p -e "UPDATE core_user_role SET user_id = 123 WHERE user_id = 123;"

For persistent issues, check Magento's official documentation for your specific version at devdocs.magento.com and look for "CLI reference" or "admin user management" sections.

Frequently Asked Questions

How do I create a test admin user for development without touching production?

Use a setup:upgradedb command before creating users, then restrict access via firewall rules or environment variables. For development, create users in a separate database instance and mount it to your deployment.

What if I get "This is not a valid Magento 2 command" error?

This typically means: 1. Your Magento version lacks this specific command (check with php bin/magento --version) 2. The CLI isn't properly installed (reinstall with composer global require php-magento2-cli/php-magento2-cli) 3. Your PATH isn't set correctly (add /usr/local/bin or equivalent to PATH)

Can I create a super admin user with full system access via CLI?

No. Magento 2's CLI commands only manage standard admin roles. To create a full system administrator, you must: 1. Create a user with admin privileges via CLI 2. Manually edit their role via database (not the CLI) 3. Assign all available roles (use admin:user:role:list to see available roles)

What happens if I don't specify a store code in the CLI command?

By default, Magento creates users for the admin store code. If you want to create users for other stores, specify the store code using --store or --store-code. For multi-store setups, you may need to run the command separately for each store.

Is there a way to create users programmatically via PHP?

Yes. Create a custom module with the following service class: public function createUser($username, $email, $password, $firstName, $lastName, $isAdmin = false) { try { $user = $this->userRepository->createUser(); $user->setUsername($username); $user->setEmail($email); $user->setPassword($password); $user->setFirstName($firstName); $user->setLastName($lastName); $user->setIsActive(true); $user->setIsAdmin($isAdmin); $this->userRepository->save($user); return true; } catch (Exception $e) { return false; } } Then inject the UserRepository service. This gives you more control over the user creation process.

Why does my newly created admin user still show as inactive?

There are several possible reasons: 1. The is_active flag wasn't set to true during creation 2. You're checking the wrong database table (should be core_user) 3. The user was created but not properly assigned to the admin role 4. You're viewing the user in the wrong store context To verify: SELECT * FROM core_user WHERE username = 'yourusername' Check the is_active column. If false, enable with: UPDATE core_user SET is_active = 1 WHERE username = 'yourusername'