Problem executing command as a different user with sudo -u

susudo

When I'm trying to execute ls as user abc with the following command I get an error:

xyz@host:~/temp$ sudo -u abc ls
[sudo] password for xyz:
Sorry, user xyz is not allowed to execute '/bin/ls' as abc on host.

But if I do su abc and then execute ls I have no problem

Best Answer

You should configure sudo security policy to allow user xyz exec something as user abc. Read 'man sudoers' and use visudo command to configure /etc/sudoers.

For example let's allow user xyz exec /usr/bin/whoami as user abc without password. Add this string into /etc/sudoers (with visudo, don't edit /etc/sudoers directly):

xyz ALL = (abc) NOPASSWD: /usr/bin/whoami

And now test it:

xyz@host:~$ sudo -u abc /usr/bin/whoami
abc
Related Question