The are many cases where you would find this useful, for example, you forgot all of your admin passwords, got locked out of your account or can not contact the person who can give you access.
In this short article we will show you a few ways you can create new admin users using command line interface.
Create a new user using Magento command line
Magento offers a cli command to create a new user. If you have access to CLI and Magento is working normally this should be your preferred way of creating a new user. Simply run the following command:
bin/magento admin:user:create
The command will ask you to input details like username, email and password. Once done the user will be created and if the process ends successfully you will see a message similar to this: Created Magento administrator user named techflarestudio
Create a new user using a PHP script
This approach assumes you have access to Magentos file system and are able to execute php scripts. While it is preferable to use the native cli command there can be cases where it is not possible. For example you may have limited bash access and can not execute the cli commands. Or perhaps the Magento installation if somewhat broken and the commands are not working. In this case we can create a simple script. First you will need to copy paste the following script in a new file in your Magento root. The file name does not matter.
- Firstly we are bootstrapping our script to hook into the Magento application, this will allow us to use object manager and access methods we need
- Then we are defining the user name, password, email, locale and other user specific data. Make sure to use secure password here.
- Finally we are instantiating our user object setting the data and saving our new user.
After you have created this file you need to simply execute the script by running:
php create_user.php
Make sure to use the file name you choice for your script. Also make sure to delete this file because leaving this as is can create a security problem.
That is it. Go to admin panel and you should be able to login.