The su
command in Bash allows a user to switch to another user account within the terminal. By default, it switches to the root user, enabling administrative privileges for executing commands that require elevated permissions.
The basic syntax of the su
command is as follows:
su [options] [username]
If no username is specified, su
defaults to the root user.
-l
or --login
: Start the shell as a login shell, which initializes the environment as if the user had logged in directly.-c
: Execute a command as the specified user and then exit.-s
: Specify a different shell to use instead of the default shell.-m
or -p
: Preserve the environment variables of the current user.su
This command prompts for the root password and switches to the root user.
su john
This command switches to the user account named “john”.
su -c 'ls /root' john
This command runs the ls /root
command as user “john”.
su -l john
This command switches to user “john” and starts a login shell, loading their environment.
su -m john
This command switches to user “john” while keeping the current user’s environment variables.
su
with caution, especially when switching to the root user, as it grants full control over the system.sudo
for executing single commands with elevated privileges instead of switching users entirely.su
command requires the password of the target user, so ensure you have the necessary credentials.