Skip to content

Latest commit

ย 

History

History
147 lines (117 loc) ยท 3.4 KB

File metadata and controls

147 lines (117 loc) ยท 3.4 KB

๐Ÿ” Linux su and sg Commands โ€“ User and Group Management

The su (substitute user) command allows switching to another user account, typically used to gain root privileges or perform tasks as another user.
The sg (switch group) command lets you execute commands as a specific group, which is useful for group-based access control.


๐Ÿ“Œ 1. su Command โ€“ Switch User

โœ… Basic Usage โ€“ Switch to Another User

su nikhil

โœ… Output

Password:

๐Ÿ”น After entering the correct password for nikhil, the shell switches to nikhil's environment.


โœ… 1.1 Switch to the Root User

su -

๐Ÿ”น Switches to root user, loading root's environment.


โœ… 1.2 Run a Command as Another User (-c)

su -c 'whoami' nikhil

โœ… Output

nikhil

๐Ÿ”น Runs whoami as nikhil without switching to their shell.


โœ… 1.3 Preserve Environment Variables (-m or -p)

export MY_VAR="Hello"
su -m nikhil -c 'echo $MY_VAR'

โœ… Output

Hello

๐Ÿ”น Keeps the current environment when switching users.


โœ… 1.4 Use a Different Shell (-s)

su -s /bin/bash nikhil

๐Ÿ”น Switches to nikhil using /bin/bash as the shell.


โœ… 1.5 Login as Another User (-l or --login)

su -l nikhil

๐Ÿ”น Loads nikhilโ€™s complete login environment.


โœ… 1.6 Display Help (-h)

su --help

โœ… Output

Usage: su [OPTION]... [USER] [ARGUMENTS]
Switch to another user account.

Options:
  -c, --command=COMMAND   pass COMMAND to the invoked shell
  -m, --preserve-environment  do not reset environment variables
  -s, --shell=SHELL       use SHELL instead of the default
  -l, --login             make the shell a login shell
  -h, --help              display this help message and exit
  -V, --version           output version information and exit

๐Ÿ“Œ 2. sg Command โ€“ Run Commands with Group Privileges

The sg (switch group) command allows executing commands as a different group, useful for group-based access control.


โœ… 2.1 Run a Command with Group Privileges

sg developers -c 'whoami'

โœ… Output

nikhil

๐Ÿ”น Runs whoami with the developers group privileges.


โœ… 2.2 Execute a Multi-word Command

sg admin -c 'echo "Hello, Admins!"'

โœ… Output

Hello, Admins!

๐Ÿ”น Runs the echo command with admin group permissions.


โœ… 2.3 Check Group Membership

sg users -c 'groups'

โœ… Output

nikhil : nikhil users

๐Ÿ”น Lists all groups the user nikhil belongs to.


๐Ÿ“Š Summary of su and sg Commands

Command Description
su nikhil Switch to user nikhil
su - Switch to root with full environment
su -c 'whoami' nikhil Run whoami as nikhil
su -m nikhil -c 'echo $MY_VAR' Preserve environment when switching users
su -s /bin/bash nikhil Use /bin/bash as the shell for nikhil
su --help Display help information
sg developers -c 'whoami' Run whoami as developers group
sg admin -c 'echo "Hello!"' Run a command with admin group privileges
sg users -c 'groups' Show groups the user belongs to

โšก