In the process, the user is often the problem arises to run the command with different privileges. For example, when he wants to install a new program, upgrade, configure firewall etc. In this case, he comes to the aid of the su command in Linux.
This command replaces the user shell on the specified. Actually starting a new shell instance with the specified parameters. This makes it possible, not leaving the system, is completely painless to enhance the management operating system, or rather to limit them.
The su command Linux
The command has the following syntax:
su [options] [-] [user]
If a call command without arguments, it changes the user’s shell on the root. You’ll get a password prompt if the password is correct, then the current user will be root.
- options , some extra commands. We will consider them below:
- [-] — change the execution context of the shell in the context of the specified user. Variables $PATH, $HOME, $SHELL, $USER, $LOGNAME contain values specific to the specified user. Home folder of a user is changed to another.
- user — the user name under which will continue to work the command shell.
The main options of the su command are:
- -c, –command=command — starts the application under a specified account;
- -s, –shell=shell — startup occurs for a given user said shell;
- -, -l, –login — change execution context to a context of the specified user, similar to the change of user of the system to the shell;
- -g, –group=group call user, consisting in a specific group. Used only for user root;
- -h, –help displays help for the command.
Next, let’s look at examples of work with the utility su.
Examples of using su
Change the user to superuser root, without changing the environment of the shell:
Please note that here you need to type the password is not your user, namely the one in whose name you want to log in. In this case, a root user. If you get the error failed to authenticate su linux, it might mean that either you entered incorrect password or the password for this user is not specified. This can be found at the root user in Ubuntu and Linux Mint. Information on how to set a password you can find in this article. Switch user to root with the change of environment settings of the shell:
Changing user to user user01:
Switch user, per-user user01 with the change of environment:
su - user01
Run shell zsh for user user01:
su -s /usr/bin/zsh user01
Run shell bash shell zsh for user user01:
su -s /usr/bin/bash user01
Launching the file Manager Midnight Commander for user user01:
su-c ‘mc’ user01
Viewing the contents of a directory on the boot root to root:
su-c ‘ls /boot’
To exit the shell, open the su command you can use the built-in command exit or the key combination Ctrl+d.
Now you know what is the linux command su, which allows you to launch shell on behalf of another user, as with the change of environment, and without. Learned how to get out of this shell.
Considered various options for the use of this command: to get superuser rights root to change the shell to execute a specific command or package on behalf of the specified user.