blog-banner

How To Login As Any User in Drupal Site?

  • Drupal
  • Drush
  • Planet
  • Security

Drupal Login Page Admin and User

It is no surprise that production sites at times may work differently from the dev/stage due to various factors. And in such circumstances, there is a need to log in as a real site user to be able to reproduce the issue. Of course, it is not easy to fix a bug unless it is really reproduced by the developer.

In one of my recent works, I found a simple trick using Drush to get access to a real user account without disturbing his/her account credentials like login name, email, password, or similar.

Drush Uli for Specific User

The trick I used counts on drush user-login command, alias uli (which I learned from my earlier blog post's comments).

Assume sivaji (login name) is a real user in your Drupal site, and you really in need to get access to this user account, then issue the below command,

$ drush uli sivaji

The output of the command will be a link, which you need to open in a browser to get access to the mentioned user account. The sample URL could be as below,

https://example.com/user/reset/135/1382091955/aINSi4tQ5XOSiYNE8JIc0qqIiIVhxngQuUr5P-2Negw/login

Make sure you are opening as an anonymous user. If you are in a multisite environment, the above command may not work, check Using Drush to administer multisite for the same.

Check out help for more advanced usage, as the above command can work with uid and email as well.

$ drush help uli
Display a one time login link for the given user account (defaults to uid 1).

Examples:
 drush user-login ryan node/add/blog       Displays and opens default web browser (if configured or detected) for a one-time login link for the user with the username ryan and 
                                           redirect to the path node/add/blog.                                                                                                  
 drush user-login --browser=firefox        Open firefox web browser, login as the user with the e-mail address drush@example.org and redirect to the path                       
 --mail=drush@example.org                  admin/settings/performance.                                                                                                          
 admin/settings/performance                                                                                                                                                     

Arguments:
 user                                      An optional uid, user name, or email address for the user to log in as. Default is to log in as uid 1. The uid/name/mail options take 
                                           priority if specified.                                                                                                                
 path                                      Optional path to redirect to after logging in.                                                                                        

Options:
 --browser                                 Optional value denotes which browser to use (defaults to operating system default). Set to 0 to suppress opening a browser. 
 --mail                                    A user mail address to log in as.                                                                                           
 --name                                    A user name to log in as.                                                                                                   
 --uid                                     A uid to log in as.                                                                                                         

Aliases: uli

Of course, there is a flip side here! This resets the last login time :(

If this is not expected for your site's requirements, be prepared to work around or refrain from using this trick.

Hope this helps. Happy Drupal hacking!

Get awesome tech content in your inbox