You have two choices about how to log in as another user at the command prompt, without being prompted for a password.
Use sux
You could use sux. Add the following to /etc/sudoers:
user1 ALL=NOPASSWD: /usr/bin/sux - user2
user1
will now be able to log in as user2
by issuing the following command:
sudo sux - user2
Using sudo
Alternatively, use pure sudo. Add the following to /etc/sudoers (assuming you’re using bash):
user1 ALL=(user2) NOPASSWD: /bin/bash
Now user1
can log in as user2
by issuing the following command:
sudo -u bn -i
The -i
flag will simulate initial login. Alternatively use -s
to run the shell specified by the SHELL
environment variable.
You can of course create an alias for either of these by adding the command to e.g. ~/.bash_aliases, provided that it’s sourced from ~/.bashrc as usual.