• Home
  • Ubuntu
  • Debian
  • Linux
  • Commands
No Result
View All Result
TECH DHEE
  • Home
  • Ubuntu
  • Debian
  • Linux
  • Commands
No Result
View All Result
TECH DHEE
No Result
View All Result
Home Commands

Su Command in Linux (Switch User)

techdhee by techdhee
February 9, 2023
0 0
0

In this article, we will learn about the su command in Linux. su (short for substitute or switch user) utility that allows you to run commands with another user’s privileges, by default the root user.

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.

Contents hide
1 How to Use the su Command
2 Sudo vs. Su
3 Conclusion

How to Use the su Command

The general syntax for the  su  command is as follows:

su [OPTIONS] [USER [ARGUMENT...]]

By default behavior of  su  is to run an interactive shell as root:

su

You will be prompted to enter the root password, and if authenticated, the user running the command temporarily becomes root.

The session shell (SHELL) and home (HOME) environment variables are set from the substitute user’s /etc/passwd entry, and the current directory is not changed.

The most commonly used option when invoking  su  is – (-l, –login). This makes the shell a login shell with an environment very similar to a real login and changes the current directory :

su-

If you want to run another shell instead of the one defined in the pa passwd file, use the -s, –shell option. For example, to switch to root and to run the zsh shell, you would type:

su -s /usr/bin/zsh

To preserve the entire environment (HOME, SHELL, USER, and LOGNAME) of the calling user, invoke the command with the -p, –preserve-environment option.

su -p

When the – option is used, -p is ignored.

If you want to run a command as the substitute user without starting an interactive shell, use the -c, –command option. For example, to invoke the  ps command as root, you would type:

su -c ps

To switch to another user account, pass the user name as an argument to  su . For example, to switch to the user tyrion you would type:

su tyrion

Sudo vs. Su

On some Linux distributions like Ubuntu, the root user account is disabled by default for security reasons. This means that no password is set for root, and you cannot use  su to switch to root.

One option to change to root would be to prepend the  su command with sudo and enter the currently logged-in user password:

sudo su -

The sudo command allows you to run programs as another user, by default the root user.

If the user is granted with  sudo assess, the  su command is invoked as root. Running sudo su – and then typing the user password has the same effect the same as running su – and typing the root password.

When used with the -i option, sudo run an interactive login shell with the root user’s environment:

sudo -i

sudo -i is basically the same as running su -.

The advantage of using  sudo over  su is that the root password doesn’t need to be shared among multiple administrative user accounts.

With  sudo you can also allow users to run only specific programs with root privileges.

Conclusion

So this is thw su command in Linux. su is a command-line utility that allows you to temporarily become another user and execute commands with the substitute user.

If you have any questions or feedback, feel free to leave a comment.

ShareTweetSendShareSend
Previous Post

How to Create a File in Linux

Next Post

How to install VSCode on Ubuntu 22.04

techdhee

techdhee

Related Posts

How to Install Arch Linux
Linux

How to Install Arch Linux [Step by Step Guide]

August 6, 2023
Terminal Emulators for Linux
Linux

Top 6 Terminal Emulators for Linux

August 5, 2023
add user to sudoers in Debian
Commands

How to Add User to Sudoers in Debian 11

February 27, 2023
Install Nano Text Editor in Linux
Commands

How to Install Nano Text Editor in Linux

February 24, 2023
Commands

Whoami command in Linux

February 23, 2023
Install ZSH on Ubuntu 22.04
Commands

How to Install ZSH on Ubuntu 22.04 LTS

February 22, 2023

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You might also like

How-to-Check-Windows-10-is-Genuine-or-Not-Genuine

How to Check Windows 10 is Genuine or Not Genuine

August 6, 2023
How to Install Arch Linux

How to Install Arch Linux [Step by Step Guide]

August 6, 2023
Terminal Emulators for Linux

Top 6 Terminal Emulators for Linux

August 5, 2023
How to Install Tor Browser in Kali Linux 2023.1

How to Install Tor Browser in Kali Linux 2023.1

April 2, 2023

How to install Tweak Tool on Ubuntu 22.04 LTS

March 3, 2023
Install Ngrok on Ubuntu 22.04 LTS

How to Install Ngrok on Ubuntu 22.04 LTS

March 2, 2023

© 2024 tecdhee.com - Tech Tips, Tricks and Tutorials.

  • Home
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy
No Result
View All Result
  • About Us
  • Contact
  • Disclaimer
  • Home
  • Privacy Policy

© 2024 tecdhee.com - Linux Tips, Tricks and Tutorials.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In