sudo vs su

Su vs Sudo in Linux

Su and sudo are two commands almost every Linux user needs to use in order to execute commands that require administrative privileges. Since they both look similar, it can be confusing to decide which command to use when. In this article, we will look at the difference between su vs sudo in Linux.


Su vs Sudo in Linux

Both su and sudo command provide more privileges to user account, allowing them to run commands with administrative or root privileges.

When you run su command, you will need to provide the password of target user account that you wish to work as. When you run sudo command, you will need to provide your own account password.

Also, if you need to run commands as root user, then use sudo command as it will grant root privileges to you only for a specific amount of time. On the other hand, when you use su command, it will switch you to root user account until you log out or switch back. This can be potentially dangerous, since there is nothing to stop you from accidentally modifying the system.


Su command

Su command stands for substitute user and allows you to basically switch from one user to another. Here is the syntax of su command.

su [user_name]
OR
su - [user_name]

If you do not provide a target username, then su command will ask you for root password, and switch you to root account.

For example, if you want to switch to user ubuntu on your system, then enter the following command. You will be prompted for password for that user. On entering the right password, you will be switched to that account.

$ su ubuntu

In this case, although the user account is changed, the environment remains the same as before.

If you want to switch to ubuntu account as well as use that user’s environment, use – after su command.

$ su - ubuntu

If you want to execute command using su, then use -c option as shown below. In this case, it works like sudo command.

$ su -c [command]

Please note, su command does not work by default in Ubuntu systems, since root account is disabled for security purposes. If you want to switch to root account, you need to activate it first using sudo command.

$ sudo passwd root

Enter root password above, and you will be able to activate root user. Once root user is active, then you can use su command to switch to it.


Sudo Command

Sudo command allows you to execute commands that require elevated privileges without actually switching account. Here is the syntax for sudo command.

$ sudo [command]

Before executing the command it will ask you for your password. Please note, even if you enter the right password, you will be able to use sudo command only if you are added to sudoers user group on your system.

Your system administrator will be able to add you to the sudoers groups using the usermod command.

usermod -aG sudo [user_name]

Here is the command to add user ubuntu to sudoers group.

$ usermod -aG sudo ubuntu

Here is the command to see the list of all users in sudoers group.

$ sudo getent group sudo

Sudo command can also be used to switch to root user, using -i option.

$ sudo -i

In this article, we have learnt the different between su and sudo command. It is advisable to use sudo command, if you only need elevated privileges to run commands. sudo command provides elevated privileges only for limited amount of time, and also does not give you root privileges, thereby preventing accidental modifications to system.

Also read:

How to Parse Command Line Arguments in Bash
How to Install Varnish on CentOS 7 for NGINX
How to Convert JPG to PDF in Linux
How to Install & Configure ElasticSearch in Linux
How to Use Strace Command in Linux

Leave a Reply

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