How to su as another user without requiring a password

You can use sudo with su to login as another user without requiring a password. Add the following to /etc/sudoers:

from_user ALL=NOPASSWD: /bin/su - to_user

This allows from_user to login as to_user by issuing this command:

sudo su - to_user

Be warned that the ALL=NOPASSWD part allows from_user to run /bin/su - to_user on any host that uses the sudoers file without requiring a password. So be careful - you don’t want to create a security hole now do you?

TODO

sudo -u to_user -i

from_user ALL=(to_user) NOPASSWD: /bin/bash

X Window System (X11)

While most environment variables will be updated for to_user, e.g. HOME will be something like /home/to_user (rather than /home/from_user), the XAUTHORITY environment variable probably won’t have been updated which means you’ll get the following error if you try to run an X application:

No protocol specified
... can't open display :0.0

or

... cannot open display: :0.0

To fix this I installed sux and used that instead of su.

Last modified: 17/05/2012 Tags: ,

This website is a personal resource. Nothing here is guaranteed correct or complete, so use at your own risk and try not to delete the Internet. -Stephan

Site Info

Privacy policy

Go to top