Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1

The syntax for the su command is as follows:


Click here to view code image
matthew@seymour:~$ su option username arguments


The man page for su gives more details, but some highlights of the su
command are as follows:


Click here to view code image
-c, --command
pass a single COMMAND to the shell with –c


-m, --preserve-environment
do not reset environment variables

-l          a   full    login   simulation  for the substituted user,
the same as specifying the dash alone

You can invoke the su command in different ways that yield diverse results.
By using su alone, you can become root, but you keep your regular user
environment. This can be verified by using the printenv command before
and after the change. Note that the working directory (you can execute pwd at
the command line to print the current working directory) has not changed. By
executing the following, you become root and inherit root’s environment:


Click here to view code image
matthew@seymour:~$ su –


By executing the following, you become that user and inherit the super user’s
environment—a pretty handy tool. (Remember: Inheriting the environment
comes from using the dash in the command; omit that, and you keep your
“old” environment.) To become another user, specify a different user’s name
on the command line:


Click here to view code image
matthew@seymour:~$ su - other_user


When leaving an identity to return to your usual user identity, use the exit
command. For example, while logged on as a regular user, use the following:


Click here to view code image
matthew@seymour:~$ su – root


The system prompts for a password:


Password:

When the password is entered correctly, the root user’s prompt appears:

Free download pdf