Psychz - Harish
Votes: 0Posted On: Jun 25, 2021 11:37:49
The 'su' is short for substitute user or commonly known as switch user utility that allows you to run commands with another user’s privileges. When executed it invokes a shell without changing the current working directory or the user environment. When the command is used without specifying the new user id as a command line argument, it defaults to using the root of the system.
Using su is the simplest way to switch to the administrative account in the current login session. This is especially handy when the root user is not allowed to log in to the system through ssh or using the GUI display manager. When run from the command line, su asks for the target user's password, and if authenticated, grants the operator access to that account and the files and directories that account is permitted to access.
Let us see how to use the su command. The general syntax for the su command is as follows:
# su [options] [-] [ [...]]
Following are some of the Options that you can use with the command
-m, -p, --preserve-environment do not reset environment variables
-g, --group specify the primary group
-G, --supp-group specify a supplemental group
-, -l, --login make the shell a login shell
-c, --command pass a single command to the shell with -c
--session-command pass a single command to the shell with -c and do not create a new session
-f, --fast pass -f to the shell (for csh or tcsh)
-s, --shell run if /etc/shells allows it
-P, --pty create a new pseudo-terminal
-h, --help display this help
-V, --version display version
Note: When invoked without any option, the default behavior of su is to run an interactive shell as root:
Use a Different User in the Same Environment
You can keep the environment of the current user account with the –p ( -P, --pty create a new pseudo-terminal)
# su –p [user]
Replace [user] with the actual username you want to switch to.
Example
jack@localhost:~$ su -p jones
Password:
jones@localhost:~$ echo $home
/home/jack
The user account will switch, but you’ll keep the same home directory. This is useful if you need to run a command as a different user, but you need access to the current user’s data.
Use a Different Shell
To use a different shell, or operating environment, enter the following:
# su –s /usr/bin/sh
This command opens a root user account in shell.
su is a command that allows you to temporarily become another user and execute commands as a substitute user. We hope that you find this response useful. Please feel free to leave a comment and leave us a up vote.