How to Create an Admin User via Command Line in Magento 2.4.7

How to Create an Admin User via Command Line in Magento 2.4.7
Creating an admin user from the Magento 2 admin panel is common, but in some cases—like being locked out of the admin area or while automating setup—it’s faster to create a new admin user using the command line (CLI).
Magento 2 provides a built-in CLI command admin:user:create to do this quickly and securely.
Table Of Content
When to Use CLI for Creating Admin Users in Magento
In certain situations, using the Command Line Interface (CLI) to create admin users can be a more efficient and secure solution compared to relying on the Magento Admin panel. Below are some key scenarios where utilizing the CLI to create admin users is particularly beneficial.
1. Regaining Backend Access
If you've lost access to the backend due to forgotten credentials or a compromised admin account, the CLI offers a direct method to reset or create a new admin user without needing access to the Magento Admin panel.
- Scenario: An admin user forgot their password, and the password reset email isn't working.
- Solution: Use the CLI to create a new admin account that has full access to the backend.
2. Creating Admin Users for Staging or Local Environments
When working with different environments (staging, development, or local), you may need to quickly create admin users for testing, development, or other purposes. Using the CLI enables you to generate admin credentials without the overhead of manually creating users through the Admin panel.
- Scenario: You need to spin up a new admin user on a fresh staging environment.
- Solution: Use the CLI to automate the creation of admin users, allowing you to test new features or configurations without accessing the Magento backend.
3. Automating Admin Setup During Deployments or Testing
In environments that are frequently deployed or reset (such as in Continuous Integration/Continuous Deployment (CI/CD) pipelines), creating admin users through the CLI can be part of an automated setup process. This ensures that the appropriate admin credentials are always available after each deployment or system reset.
- Scenario: After a deployment or system reset, the admin user needs to be recreated automatically.
- Solution: Incorporate the CLI command into your deployment process to automatically generate the necessary admin user account after each deployment.
Benefits of Using CLI for Admin User Creation
Efficiency and Speed:
CLI commands are faster than manually creating admin users through the Magento Admin panel. This is especially useful in environments with frequent resets or new installations.
Automated Deployments:
By integrating the CLI into your deployment pipeline, you can ensure that admin users are created automatically, saving time and minimizing human error.
Remote Access:
The CLI allows you to create admin users even if you do not have direct access to the Magento Admin panel, making it useful when working in remote or headless environments.
Security:
CLI-generated admin accounts can be created with unique and secure credentials, avoiding the potential for weak passwords or manual mistakes during the account creation process.
Key CLI Commands for Admin User Creation
Here are the essential CLI commands for creating and managing admin users in Magento:
Create a new admin user:
php bin/magento admin:user:create --username <username> --password <password> --email <email> --firstname >firstname< --lastname <lastname> --role <role>
Change admin password:
php bin/magento admin:user:password <username> --password <new_password>
List all admin users:
php bin/magento admin:user:list
Troubleshooting Common CLI Admin User Creation Issues
Permission Denied Errors:
If you encounter permission-related issues while using CLI commands, ensure that your user has appropriate permissions to execute Magento commands and access necessary directories.
Invalid Command Error:
Double-check that the correct syntax is used, and ensure that your Magento installation is up-to-date and the CLI commands are properly installed.
Password Complexity Issues:
Magento has specific password complexity requirements for admin accounts. If you're setting the password via the CLI and it doesn't meet the requirements, you'll need to adjust it to meet the password policy.
Role Assignment:
Ensure that you assign the correct role when creating a new admin user. If the wrong role is assigned, the user may not have the necessary permissions to perform required actions.
Use Cases in Development and Production
Development Environments:
Developers often use the CLI to create admin users for testing purposes. This can be automated in local development environments or used in containers like Docker for isolated environments.
Production Systems:
In some cases, if an admin account is accidentally deleted or corrupted, using the CLI to quickly recreate the account can help minimize downtime and ensure business continuity.
By leveraging the power of CLI in these scenarios, you can streamline your workflow and ensure that admin users are always available when needed, all while maintaining security and efficiency across your Magento environments.
Prerequisites for Admin User Creation via CLI
Before proceeding with the creation of admin users through the command line interface (CLI), it is important to ensure that your environment is properly set up. Below are the key prerequisites to check before running any commands.
1. Terminal/SSH Access
To execute CLI commands, you need access to a terminal or SSH client that allows you to connect to your server where Magento is hosted. This is necessary for running commands directly on the server where Magento is installed.
- Action: Ensure you can SSH into the server or have terminal access to the project root directory. This typically requires an SSH client (such as PuTTY, or terminal on macOS/Linux).
2. Executable bin/magento Command
Magento's bin/magento
is a command-line utility that facilitates various administrative and development tasks. The bin/magento
file must be executable to run commands successfully.
- Action: Verify that the
bin/magento
command is executable. If it's not, you can update its permissions using the following command:
chmod +x bin/magento
3. Magento Installation and Configuration
Magento must be installed and properly configured before you can interact with the system using the CLI. A correctly configured Magento installation will ensure smooth execution of commands like creating admin users.
- Action: Verify that Magento is installed and operational by checking if the following files and directories exist in your project root:
app/
(Magento core files)bin/magento
(Magento CLI tool)var/
(Magento's var directory for runtime data)pub/
(Magento's publicly accessible files)
If any issues arise related to the installation, running php bin/magento setup:install
or consulting Magento’s installation guides may be necessary.
4. Magento Environment Configuration
Magento must be correctly configured for the environment, including database and web server settings. Configuration issues, such as incorrect database credentials or misconfigured file permissions, can prevent CLI commands from executing as expected.
- Action: Check the following configuration files to ensure correct setup:
app/etc/env.php
(Contains database and environment settings)app/etc/di.xml
(Dependency injection configuration).htaccess or nginx.conf
(Web server configuration)
Ensure your database is accessible and that the file system has proper read/write permissions for the Magento directories.
5. PHP Version Compatibility
Magento requires a compatible version of PHP to run commands and other operations. An incompatible PHP version can result in errors or failures when trying to execute CLI commands.
- Action: Verify that your PHP version meets the Magento system requirements for your version of Magento. You can check the installed PHP version using:
php -v
For example, Magento 2.4.x requires PHP 7.4.x or later.
6. Proper Database Connection
To successfully create admin users or run other commands that interact with the database, ensure that Magento has a working connection to its database.
- Action: Test the database connection by checking the app/etc/env.php file for accurate database credentials (host, username, password, etc.). Ensure the database is accessible from the server hosting the Magento application.
7. Sufficient Permissions and User Roles
To run administrative commands, you need to have appropriate permissions for the bin/magento
command and the file system. Ensure that the user executing these commands has the necessary privileges.
- Action: If needed, adjust user permissions or execute commands with superuser privileges (e.g.,
sudo
on Linux or macOS).
Checklist for Executing Admin User CLI Commands
Component | Action | Verify |
---|---|---|
Terminal/SSH Access | Ensure access to the server via SSH or terminal | Can you SSH into the server? |
Executable bin/magento | Ensure the bin/magento file is executable | Run chmod +x bin/magento to make the file executable |
Magento Installation | Ensure Magento is installed and configured properly | Check if Magento files and directories (e.g., app/, bin/, pub/) are present in the project root |
Environment Configuration | Confirm Magento environment files (e.g., env.php, di.xml) are properly configured | Check database settings in app/etc/env.php and web server configuration files (e.g., .htaccess, nginx.conf) |
PHP Version Compatibility | Ensure PHP version matches Magento requirements | Run php -v and check if it meets the version needed by your Magento version |
Database Connectivity | Ensure Magento has a working connection to the database | Verify credentials in app/etc/env.php and test connectivity from the server |
Permissions and Roles | Ensure the executing user has proper permissions for the necessary directories and commands | Check user permissions, and if needed, use sudo to execute commands |
By ensuring these prerequisites are met, you can avoid errors and complications when running Magento CLI commands, especially when creating admin users or performing other administrative tasks.
CLI Command Syntax Overview
When using the CLI to create an admin user, you can follow this command structure:
php bin/magento admin:user:create \
--admin-user="your-username" \
--admin-password="your-password" \
--admin-email="[email protected]" \
--admin-firstname="FirstName" \
--admin-lastname="LastName"
Example CLI Command
For creating a new admin user, you would run:
php bin/magento admin:user:create \
--admin-user="new-admin" \
--admin-password="Admin@123" \
--admin-email="[email protected]" \
--admin-firstname="Admin" \
--admin-lastname="User"
Expected Output
After running the command successfully, the system will display the following:
Created Magento administrator user named new-admin
Table: Admin User Command Parameters
Parameter | Description | Required |
---|---|---|
--admin-user | Username for the admin account | Yes |
--admin-password | Password (must meet Magento's password policy) | Yes |
--admin-email | Valid email address for the admin user | Yes |
--admin-firstname | First name of the admin user | Yes |
--admin-lastname | Last name of the admin user | Yes |
Additional Notes for Using the Command
Password Policy Considerations:
- Ensure the password you provide adheres to Magento’s password policy, which typically requires a mix of upper and lowercase letters, numbers, and special characters.
- You can refer to Magento's documentation for the exact password requirements.
Error Handling:
- If any of the parameters are missing or incorrect, the system will return an error indicating which parameter needs correction.
- Common issues may include an invalid email format or not meeting the required password complexity.
Automation and Scripting:
- If you need to automate this process, especially in a staging or production environment, you can integrate this CLI command into deployment scripts or cron jobs.
Post-Creation Configuration:
- After creating the user, ensure the role assigned to the admin user has the appropriate permissions.
- You can assign roles via the admin panel or by using the command line for more advanced setups.
Security Recommendations:
- Always use a strong and unique password for the admin user, especially in production environments.
- Limit the use of admin accounts for specific tasks to reduce potential security risks.
Magento Admin Password Policy Guidelines
Magento 2 includes a strict password enforcement mechanism to ensure the security of administrative access. Understanding and complying with these rules is essential to successfully create admin users via CLI and protect the backend from unauthorized access.
Minimum Password Requirements
The platform validates password complexity using the following default rules:
- Minimum Length: Password must contain at least 7 characters.
- Character Composition:
- At least one uppercase letter (A–Z)
- At least one lowercase letter (a–z)
- At least one digit (0–9)
- At least one special character (such as @, #, $, %, !, etc.)
Passwords failing to meet any of these criteria will cause the admin:user:create
CLI command to fail, returning a descriptive error message about the failed validation.
Additional Password Considerations
- Avoid Common Patterns: Passwords like Admin123, Password@1, or Qwerty#9 may meet technical complexity but are discouraged due to high predictability.
- Do Not Reuse Credentials: Avoid reusing passwords from other systems or older Magento environments.
- Password Length Customization: The minimum length and pattern requirements can be customized in the Magento backend under:
- Stores > Configuration > Advanced > Admin > Security > Password Options
Security Best Practices for Admin Passwords
To enhance platform security in all environments:
- Use a Password Generator: Prefer randomly generated passwords that meet the complexity requirements.
- Change Default Credentials Immediately: After installation or automated admin creation, replace any default or temporary credentials.
- Enable Two-Factor Authentication (2FA): For additional protection, Magento supports built-in 2FA, which should be enabled in production environments.
- Password Rotation Policy: Encourage regular password changes and enforce expiration policies where appropriate.
CLI Error Handling for Invalid Passwords
When executing the CLI command, Magento performs immediate validation:
Error Message Example:
Password must be at least 7 characters long and include both numeric and alphabetic characters. Please try again.
To resolve:
- Double-check the password format.
- Ensure all four character types are included.
- Avoid whitespace or unsupported symbols that may break CLI parsing.
Summary Table: Password Policy Enforcement
Rule | Requirement | Customizable? |
---|---|---|
Minimum Length | 7 characters | Yes |
Uppercase Letter | At least one (A–Z) | Yes |
Lowercase Letter | At least one (a–z) | Yes |
Number | At least one (0–9) | Yes |
Special Character | At least one (e.g., !, @, #, $, %) | Yes |
CLI Error on Invalid Password | Command will fail with descriptive message | Not Applicable |
Creating Secure Admin Users in Magento 2: Best Practices & Common Errors
When managing admin accounts in Magento 2, it's crucial to follow proper security protocols and error handling strategies to avoid system misconfigurations or vulnerabilities. Below is a refined guide to help you set up secure admin users effectively.
Key Security Recommendations for Admin Accounts
Use Strong and Unique Passwords
Avoid using weak or guessable passwords like admin123 or password1. Instead:
- Use at least 12 characters (even though 7 is the Magento minimum).
- Include uppercase, lowercase, numbers, and special characters.
- Avoid reusing passwords across environments.
- Consider using a password manager or strong password generator.
Avoid Shared Admin Credentials
Each team member should have a unique admin account to maintain clear audit trails and reduce shared risk.
Remove Unused Admin Accounts
If you created temporary admin users during development or debugging, make sure to remove them to minimize the attack surface. For example:
php bin/magento admin:user:delete temporary-admin
Automation for Admin Account Setup
Deployment Scripts
Integrate admin user creation into your CI/CD or deployment pipelines, especially for staging or sandbox environments. This ensures that a working admin user is always available for testing or validation.
Example use-case in a shell script:
php bin/magento admin:user:create \
--admin-user="your-username" \
--admin-password="your-password" \
--admin-email="[email protected]" \
--admin-firstname="FirstName" \
--admin-lastname="LastName"
Post-Deployment Tasks
You may also include role assignment and ACL setup via CLI or custom modules to ensure the new user has minimal and specific permissions.
Troubleshooting Common Admin User Issues
Issue | Likely Cause | Suggested Solution |
---|---|---|
Command not found | Not in Magento root directory or PHP not installed | Navigate to the Magento root and ensure PHP/CLI tools are accessible |
Password does not meet policy | Password lacks required complexity | Add uppercase, lowercase, numeric, and special characters |
User already exists | Duplicate username in database | Use a different username or delete the existing one |
Permission denied | Current user lacks execution rights | Use chmod +x bin/magento or execute with elevated privileges |
Invalid email format | Typo or missing domain in email address | Ensure a valid format like [email protected] is used |
Validating Admin User Creation
After running the creation command, verify the account by logging in via the Magento backend or querying the admin_user table directly in the database to ensure the user record exists and is active.
SQL example:
SELECT username, is_active FROM admin_user WHERE username = 'new-admin';
You can also review log files under var/log/
if any backend issues arise after the creation process.
Tip
To enhance your eCommerce store’s performance with Magento, focus on optimizing site speed by utilizing Emmo themes and extensions. These tools are designed for efficiency, ensuring your website loads quickly and provides a smooth user experience. Start leveraging Emmo's powerful solutions today to boost customer satisfaction and drive sales!
Conclusion
Creating a Magento 2 admin user via the command line is a crucial skill for developers and store administrators, especially in recovery scenarios or during setup automation. With Magento 2.4.7, the process remains straightforward and secure using the admin:user:create CLI command. This method not only speeds up user creation but also ensures full control over admin credentials without accessing the web interface. Whether you're setting up a staging environment, recovering access, or automating deployment, mastering this CLI tool adds great flexibility and efficiency to your Magento management workflow. Always ensure you use strong credentials and follow security best practices to protect your store’s backend.
FAQs
How can I create a new Magento 2 admin user via the command line?
You can use the command php bin/magento admin:user:create
followed by required flags like username, password, email, first name, and last name.
What is the purpose of the admin:user:create CLI command?
It creates a new administrator account without needing access to the Magento backend.
What Magento version is this guide compatible with?
This guide is updated and tested for Magento 2.4.7 as of 2025.
Can I define custom values for the admin user?
Yes, you can set a custom username, password, email, first name, and last name during the command execution.
Where should I run the CLI command to create an admin user?
You must run it from your Magento project root directory in the terminal.
What is the default response for a successful user creation?
You will see a message: "Created Magento administrator user named [your username]" in green text.
What if I forget the admin login password?
You can either reset it via CLI or create a new admin user using the admin:user:create
command.
Is this method safe for production environments?
Yes, but ensure the credentials are strong and change them later for added security.
Can this command be used in automated deployment scripts?
Absolutely. It's ideal for CI/CD and other automation tasks during store setup or staging deployment.
How many admin users can I create using CLI?
There’s no limit from Magento's side. You can create as many admin users as needed with unique usernames and emails.
What are best practices when creating admin users via CLI?
Use secure passwords, unique usernames, and avoid exposing credentials in shared environments.
Can I delete or disable an admin user created via CLI?
Yes, you can manage admin users via the backend or through database and CLI scripts.
Does creating a user via CLI bypass any Magento security checks?
No, Magento still enforces password rules and unique email/user constraints when using CLI.
What should I do if the command doesn’t create the user?
Ensure all flags are properly set, the database connection is active, and Magento is not in maintenance mode.
Can I use this CLI method on a hosted server with limited access?
Yes, as long as you have terminal access and permissions to run Magento CLI commands.