Skip to content

Files

Latest commit

 

History

History
35 lines (21 loc) · 954 Bytes

SC2117.md

File metadata and controls

35 lines (21 loc) · 954 Bytes

Pattern: Use of su in interactive mode

Issue: -

Description

It's commonly believed that su makes a session run as another user. In reality, it starts an entirely new shell, independent of the one currently running your script.

su; whoami will start a root shell and wait for it to exit before running whoami. It will not start a root shell and then proceed to run whoami in it.

To run commands as another user, use sudo command or su -c 'command'. sudo is preferred when available, as it doesn't require additional quoting and can be configured to run passwordless if desired.

Example of incorrect code:

whoami
su
whoami

Example of correct code:

whoami
sudo whoami

Exceptions

If you're aware of the above and want to e.g. start an interactive shell for a user, feel free to ignore this message.

Further Reading