What is the su Command in Linux?


Understanding the su Command in Linux

The su command, standing for “substitute user,” is an essential tool in Linux for switching the current user context to another user, including the superuser (root). Mastering this command can significantly enhance your control over the Linux operating system.

Basic Usage

Switching to the Superuser

To switch to the superuser account, which is often required for administrative tasks, you simply use:

su

You’ll be prompted to enter the root password. This command grants you access to perform operations that require elevated privileges.

Switching to Another User

If you want to switch to a specific user’s account, use the following syntax:

su username

Replace username with the actual username of the target account. You’ll need to enter that user’s password to gain access.

Simulating a Full Login Shell

To simulate a complete login environment for a user, including executing their login scripts, use:

su - username

This command ensures that all environment variables are set as if the user had logged in directly.

Executing a Command as Another User

You can also execute a command as another user without switching your entire shell context. For this purpose, the syntax is:

su - username [-c|--command] "command"

This allows you to run a specific command as the target user, which can be very useful for running scripts or commands that require different permissions.

Conclusion

The su command is a powerful utility that enables flexible user switching and management in Linux. Familiarizing yourself with its various options can streamline your workflow and enhance your administrative capabilities. For further details, consult the manual page with the command:

man su

By mastering su, you can effectively manage user permissions and maintain a secure and organized system.

See Also