Difference Between Linux sudo su, su, s, i, bin/bash Privilege Commands

Difference Linux sudo su, su-, -s, -i Privilege Command

Do you know, you can get superuser privilege on your Linux system to just execute two commands but surely you don’t know about what is difference between Linux super privilege commands su, su -, Sudo su, sudo su-, sudo -s, sudo -i or sudo /bin/bash so on.

Today i will explain which command you should use to get right privilege at right time to execute your task.

Linux Super privilege commands

Lets explain the difference between following Linux privilege commands:

  1. su
  2. su –
  3. sudo
  4. sudo su
  5. sudo su –
  6. sudo -s
  7. sudo -i
  8. sudo /bin/bash

Type of Linux Login Shell

Before going to Privilege command need to understand the type of login shell in Linux system

  1. login shell: A login shell logs you into the system as a specified user, necessary for this is a username and password. When you hit ctrl+alt+F1 to login into a virtual terminal you get after successful login a login shell.
  2. non-login shell: A shell that is executed without logging in, necessary for this is a currently logged-in user. When you open a graphic terminal in gnome it is a non-login shell.
  3. interactive shell: A shell (login or non-login) where you can interactively type or interrupt commands. For example a gnome terminal.
  4. non-interactive shell: A (sub)shell that is probably run from an automated process. You will see neither input nor output.

1. Linux su Privilege Command

Linux su privilege command opens an “interactive non login shell” that is used to switch user ( root or any standard user ) but do not change the current user home directory. See example below:

I am logged in as user1 in terminal

user1@pc$ pwd
user1@pc$ /home/user1

Now run su command to switch user

user1@pc$ su user2
Password:
user2@pc$pwd
user2@pc$ /home/user1  --> User directory does not change because it opens an interactive non login shell

Note: If you don’t mention username after “su” then Linux system will assume that you want to switch to root account and will ask root password.

2. Linux “Su -” Privilege Command

Linux “su -“privilege command opens an “interactive login shell” that is used to switch user ( root or any standard user ) and change the current user home directory. See the example below:

I am logged in as user1 in terminal

user1@pc$ pwd
user1@pc$ /home/user1

Now run “su -” command to switch user

user1@pc$ su - user2
Password:
user2@pc$pwd
user2@pc$ /home/user2  --> User directory changed because it opens an interactive login shell

Note: If you don’t mention username after “su -” then Linux system will assume that you want to switch to root account and will ask root password.

3. Linux sudo Privilege Command

sudo is used to run any privilege command with root privilege and asks current user logged-in password (and also checks if you’re allowed to run commands as root, which is configured through /etc/sudoers — by default all user accounts that belong to the “root” or “sudo” groups are allowed to use “sudo command”).

In Linux system there is most of the commands need sudo privilege to execute the commands. Example

any task which related to user ID like add user, delete user in Linux system need sudo privilege etc. Example

I want to update the Linux system like Ubuntu and run follwoing command without sudo privilege.

user1@pc$ apt-get update
Reading package lists… Done
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
W: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied) --> Permissioned denied

Result- “apt-get update” needs sudo privilege to execute that’s why i encounter permission denied message.

Let execute the “apt-get update” command with sudo privilege command

user1@pc$ sudo apt-get update
[sudo] password for user1:
Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [109 kB]
Get:2 http://in.archive.ubuntu.com/ubuntu focal InRelease [265 kB]
Get:3 http://security.ubuntu.com/ubuntu focal-security/main amd64 DEP-11 Metadata [24.3 kB]
Get:4 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Fetched 3,633 kB in 10s (364 kB/s)
Reading package lists… Done --> Command executed successfully.

Note: sudo asks current logged-in user’ password to execute the command with sudo privilege, if the user member of sudo group / mentioned sudoers file.

4. Linux “Sudo su” Privilege Command

Linux sudo su privilege command is similar to “su” command that opens an “interactive non login shell” that is used to switch user ( root or any standard user ) but do not change the current user home directory. See the example below:

I am logged in as user1 in terminal

user1@pc$ pwd
user1@pc$ /home/user1

Now run sudo su command to switch user

user1@pc$ sudo su user2
Password:
user2@pc$pwd
user2@pc$ /home/user1  --> User directory does not change because it opens an interactive non login shell

5. Linux “Sudo su -” Privilege Command

Linux “sudo su -” privilege command is similar to “su-” command that opens an “interactive login shell” that is used to switch user ( root or any standard user ) and change the current user home directory. See the example below:

I am logged in as user1 in terminal

user1@pc$ pwd
user1@pc$ /home/user1

Now run “sudo su -” command to switch user

user1@pc$ sudo su - user2
Password:
user2@pc$pwd
user2@pc$ /home/user2  --> User directory changed because it opens an interactive login shell

Note: If you don’t mention user name after “sudo su –” then you will be switched in root directory with root privileges.

6. Linux “Sudo -s” Privilege Command

“sudo -s” command runs a $shell /bin/bash/ with root privileges and gives you the current user’s environment, so your ~/.bashrc is respected.

The -s (shell) option runs the shell specified by the SHELL environment variable if it is set and re-direct to the $shell /bin/bash for execution. Otherwise, an interactive shell is executed.

user1@pc$ pwd
user1@pc$ /home/user1

Now run “sudo -s” command to switch user

user1@pc$ sudo -s
Password:
root@pc:/home/user1#pwd
root@pc:/home/user1# /home/user1  --> User directory does not change but you will be now root user because it opens an interactive login shell.

7. Linux “Sudo -i” Privilege Command

Linux “sudo -i” privilege command is similar to “sudo su –” command that opens an “interactive login shell” that is used to switch user ( root or any standard user ) and change the current user home directory to root directory that gives you the root environment, i.e. your ~/.bashrc is ignored.. See the example below:

I am logged in as user1 in terminal

user1@pc$ pwd
user1@pc$ /home/user1

Now run “sudo -i” command to switch user

user1@pc$ sudo -i
Password:
root@pc$pwd
root@pc$ /root  --> User directory changed to root user because it opens an interactive login shell.

It is recommend the use of “sudo -i" instead of "sudo -s" for two reasons:

  1. The visual reminder that you are in a ‘root’ session.
  2. The root environment is far less likely to be poisoned with malware, such as a rogue line in .bashrc.

8. Linux “Sudo /bin/bash” Privilege Command

Sudo /bin/bash is similar to “sudo -s” this means that you call sudo with the command /bin/bash. /bin/bash is started as an interactive non-login shell so all the dot-files are not executed, but bash itself reads .bashrc of the calling user.

Your environment stays the same. Your home will not be root’s home. So you are root, but in the environment of the calling user.

Conclusion

Let understand the summary of above privilege commands

  1. su means lets you switch user so that you’re actually logged in as root or standard user without changing home directory.
  2. “su -” means lets you switch user so that you’re actually logged in as root or or standard user along with change home directory.
  3. sudo means lets you run commands in your own user account with root privileges without changing home directory.
  4. “sudo su” means allows you to become another user and permits user to execute a command as the superuser or another user, only changes the current user to root, without changing home directory
  5. sudo su –” means allows you to become another user and permits user to execute a command as the superuser or another user, only changes the current user to root, along with change home directory.
  6. “sudo -s” means runs a shell with root privileges.
  7. “sudo -i” means creates a fresh environment as root and change to root directory that login-specific resource files such as .profile or .login will be read by the shell.
  8. sudo /bin/bash is similar to “sudo -s” to runs a shell with root privileges.

Hopefully, from now onwards you will not be confused in the above Linux privilege commands.

Thank to being with HyperHCI.com to learn something new something different and empower your knowledge.