There may be a time where you are locked out of your Magento admin panel and only have access to a secure shell to the server that controls your installation. This guide will show you how to create and manage your Magento 2 admin users from the command line interface (CLI).
Opening a secure shell connection
In order to manage the Magento admin users from the command line, you will first need to connect to the server through a secure shell (SSH).
$ ssh {{{ssh-user:username}}}@{{{ssh-hn:hostname}}} -p {{{ssh-port:22}}}
Add Magento 2 bin directory to your PATH variable
If interested in running the Magento 2 commands from anywhere within your file system, modify the following example by adding the bin
directory to your PATH variable. This can be done by appending the following snippet to your .bashrc
or .bash_profile
file:
export PATH=$PATH:{{{magento-path:/home/user/example.com/html}}}/bin
How to create and edit a Magento 2 admin user
Create or edit your Magento 2 admin user. Depending on the version of Magento 2 that is running, parameters are not required to be passed with the admin:user:create
command.
$ php {{{magento-path:/home/user/example.com/html}}}/bin/magento admin:user:create
Admin user: example-user
Admin password: b8A3hJhuhh
Admin email: [email protected]
Admin first name: Jane
Admin last name: Smith
Created Magento administrator user named example-user
Here is an example of the complete command necessary for any installations requiring parameters to be passed. This command can be modified to fit your requirements.
$ php {{{magento-path:/home/user/example.com/html}}}/bin/magento admin:user:create \
--admin-user='example-user' \
--admin-password='b8A3hJhuhh' \
--admin-email='[email protected]' \
--admin-firstname='Jane' \
--admin-lastname='Smith'
Possible parameters with Magento 2
Parameter | Description |
---|---|
--admin-user | Admin username |
--admin-password | Admin user’s password |
--admin-firstname | Admin’s first name |
--admin-lastname | Admin’s last name |
--admin-email | Admin’s e-mail address |
$ php {{{magento-path:/home/user/example.com/html}}}/bin/magento admin:user:create --help
Usage:
admin:user:create [options]
Options:
--admin-user=ADMIN-USER (Required) Admin user
--admin-password=ADMIN-PASSWORD (Required) Admin password
--admin-email=ADMIN-EMAIL (Required) Admin email
--admin-firstname=ADMIN-FIRSTNAME (Required) Admin first name
--admin-lastname=ADMIN-LASTNAME (Required) Admin last name
--magento-init-params=MAGENTO-INIT-PARAMS Add to any command to customize Magento initialization parameters
For example: "MAGE_MODE=developer&MAGE_DIRS[base][path]=/var/www/example.com&MAGE_DIRS[cache][path]=/var/tmp/cache"
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Help:
Creates an administrator
Unlock an admin user
If an admin account has been locked, the below command can be used to unlock the admin user.
$ php {{{magento-path:/home/user/example.com/html}}}/bin/magento admin:user:unlock {{{m2-user:example-user}}}
If the admin user’s account failed to unlock, the following output will appear.
$ php {{{magento-path:/home/user/example.com/html}}}/bin/magento admin:user:unlock {{{m2-user:example-user}}}
The user account "{{{m2-user:example-user}}}" was not locked or could not be unlocked