What is the difference between the two commands? Which one is better to emulate another user when wanting to do something as that user?
sudo -H -iu USER command
and
su -l USER -c command
What is the difference between the two commands? Which one is better to emulate another user when wanting to do something as that user?
sudo -H -iu USER command
and
su -l USER -c command
If you are trying to run a command as another user, sudo
will ask for your password, su
will ask for the other user's password.
Sudo is designed to let unprivileged users run specific commands as a different user, and so has a configuration file /etc/sudoers
which lets system administrators decide which users can run which commands as which other users. IE it has fine grained permissions. These permissions can be granted and later revoked.
Su also lets you run a command as another user but isn't designed with the same fine grained permissions. If you have the other user's password you can do anything as that user. The only way to revoke that access is to change the other user's password.
Opinion alert
Generally I would use sudo
not su
for most things. It's easier to give someone a little access without just handing them the keys to everything (your password)
End opinion
There's not much difference in effect when trying to "simulate" another user. Both run your command as that user.