How to use the command su (with examples)
The su
command in Linux is used to switch the current user to another user. This command is primarily used to switch to the root user, but it can also be used to switch to any other user on the system. The su
command requires the password of the target user to switch the shell.
Use case 1: Switch to superuser (requires the root password)
Code:
su
Motivation: This use case is used when you want to switch to the superuser or root user. The root user has access to all files and permissions on the system, and this is useful when you need to perform administrative tasks or modify system files.
Explanation: In this use case, the su
command is used without any arguments. This prompts the user to enter the password for the root user. Upon successful authentication, the shell is switched to the root user’s shell.
Example output:
Password:
Use case 2: Switch to a given user (requires the user’s password)
Code:
su username
Motivation: This use case is used when you want to switch to a specific user other than root. It can be useful for testing user-specific configurations or troubleshooting issues that only occur for a particular user.
Explanation: In this use case, the su
command is followed by the username of the target user. The user is prompted to enter the password of the target user. If the password is correct, the shell is switched to the target user’s shell.
Example output:
Password:
Use case 3: Switch to a given user and simulate a full login shell
Code:
su - username
Motivation: This use case is used when you want to switch to a given user and simulate a full login shell. Simulating a full login shell loads the user’s environment variables and configuration files, providing an environment similar to when the user logs in directly.
Explanation: In this use case, the su
command is followed by a hyphen ("-") and the username of the target user. The hyphen indicates that a full login shell should be simulated. The user is prompted to enter the password of the target user. If the password is correct, the shell is switched to the target user’s shell with a full login shell environment.
Example output:
Password:
Use case 4: Execute a command as another user
Code:
su - username -c "command"
Motivation: This use case is used when you want to execute a command as another user, primarily for administrative purposes. It allows you to run a specific command with the privileges and environment of the target user.
Explanation: In this use case, the su
command is followed by a hyphen ("-"), the username of the target user, and the “-c” option. The “-c” option is followed by the command that you want to execute as the target user. The user is prompted to enter the password of the target user. If the password is correct, the command is executed with the privileges and environment of the target user.
Example output:
Password:
Conclusion:
The su
command is a powerful tool in Linux that allows users to switch their shell to another user. It provides versatility in managing user permissions and configurations. By understanding the different use cases of the su
command, you can effectively switch between users and perform various administrative tasks on your Linux system.