sudo
can do just that for you :)
It needs a bit of configuration though, but once done you would only do this:
sudo -u user2 -s
And you would be logged in as user2 without entering a password.
Configuration
To configure sudo, you must edit its configuration file via: visudo
. Note: this command will open the configuration using the vi
text editor, if you are unconfortable with that, you need to set another editor (using export EDITOR=<command>
) before executing the following line. Another command line editor sometimes regarded as easier is nano
, so you would do export EDITOR=/usr/bin/nano
. You usually need super user privilege for visudo
:
sudo visudo
This file is structured in different section, the aliases, then defaults and finally at the end you have the rules. This is where you need to add the new line. So you navigate at the end of the file and add this:
user1 ALL=(user2) NOPASSWD: /bin/bash
You can replace also /bin/bash
by ALL
and then you could launch any command as user2 without a password: sudo -u user2 <command>
.
If you want to be able to switch to any user just use
user1 ALL=(ALL) NOPASSWD: /bin/bash
Update
I have just seen your comment regarding Skype. You could consider adding Skype directly to the sudo's configuration file. I assume you have Skype installed in your Applications folder:
user1 ALL=(user2) NOPASSWD: /Applications/Skype.app/Contents/MacOS/Skype
Then you would call from the terminal:
sudo -u user2 /Applications/Skype.app/Contents/MacOS/Skype
I found out that the I was able to modify /etc/sudoers
when I logged in as work
using ssh
. Not sure if I had made some mistake with my previous attempt with su - work
.
The following steps worked:
- Logged in as
local
, start with ssh work@localhost
sudo visudo
- Add entry
local ALL=(ALL) ALL
- Save and exist vi
- Exist ssh session
- Try
sudo ls /
Best Answer
This is expected behaviour.
The purpose of su is to switch user. It's called the substitute user identity tool. su takes the other user's password since you are switching to that user.
Source: Man page
The purpose of sudo is to execute a command as another user. The
-u
or--user
option for sudo specifies that user. You're not logging in as that user, just running a command. sudo takes your password to verify your identity for performing such a task.