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
su -l USER -c command
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
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.
Generally I would use
su for most things. It's easier to give someone a little access without just handing them the keys to everything (your password)
There's not much difference in effect when trying to "simulate" another user. Both run your command as that user.