Su Command
Switch User with the Su Command
The su command (short for "substitute user" or "switch
user") is a fundamental utility in Unix-like operating systems,
including Linux. It allows a user to execute commands with the
privileges of another user, most commonly the superuser (root). This
is essential for performing administrative tasks that require
elevated permissions.
Basic Su Command Usage
To switch to another user account, you typically use the following syntax:
su <username>
If you omit the <username>, the
su command defaults to switching to the root user. You
will be prompted to enter the password for the target user.
Executing Commands as Root
A common use case is to temporarily gain root privileges to run a
specific command. You can do this by using the - or
-l (login) option, which simulates a full login for the
target user, setting up their environment variables:
su - -c "<command>"
For example, to update package lists as root:
su - -c "apt update"
Understanding Su vs. Sudo
While su is powerful, the sudo command is
often preferred for executing commands with elevated privileges.
sudo allows administrators to grant specific users the
ability to run certain commands as root (or another user) without
sharing the root password. This provides better security and
auditing capabilities.
Key Su Command Options
-
-or-l: Simulate a full login, setting up the target user's environment. -
-c <command>: Execute a single command and then exit. -s <shell>: Specify the shell to use.
Further Resources
- su(1) - Linux man page
- GNU Coreutils Manual - id (related to user identification)
- Understanding the Linux su Command