The su
(substitute user) command allows a user to switch to another user account or execute commands as another user, commonly root
.
- Requires authentication using the password of the target user.
- Enables complete user switching or the execution of commands under a different user.
su
: Switches user without changing the environment variables.su -
: Starts a new shell session with the full login environment of the target user.
su [OPTIONS] [USERNAME]
-
: Loads the target user’s environment variables.-c "command"
: Executes a single command as the target user.-s /bin/bash
: Changes the shell when switching users.
-
Switch to the
root
user:su
This prompts for the
root
user password. -
Switch to a specific user (e.g.,
john
):su john
This switches to
john
’s account and requires authentication. -
Run a command as another user:
su -c "whoami" username
Example:
su -c "ls /root" root
Runs
ls /root
asroot
while staying in the current shell. -
Start a full login session for a user:
su - username
This loads the user’s full environment as if they had logged in directly.
The sg
(substitute group) command allows users to execute commands as a member of a specified group. This is useful when working with group-based permissions without changing user ownership.
- Runs commands under a specific group context.
- Does not require password authentication.
- The user must already be a member of the target group.
- Limited to command execution and does not open a full session.
sg groupname -c "command"
-
Run a command as a member of a group:
sg developers -c "touch file.txt"
This creates a file under the
developers
group context. -
Start a new shell as a member of a group:
sg groupname
This opens a shell where commands execute under the specified group.
-
Check your current group membership:
groups
This displays all groups you belong to.
Feature | su (Switch User) |
sg (Switch Group) |
---|---|---|
Purpose | Switch to another user account. | Temporarily switch to another group. |
Authentication | Requires the target user’s password. | Requires membership in the target group (no password needed). |
Scope | Switch user entirely or run commands as them. | Execute commands as a specific group member. |
Use Case | For administrative tasks or user switching. | For group-based permissions and tasks. |
- When you need to work on files or execute commands with specific group-based permissions.
- Example Scenario:
- You are part of the
developers
andadmins
groups. - A file is owned by the
admins
group. - Instead of switching users, you can use
sg admins
to work on the file without changing ownership.
- You are part of the