

Linux discourages working as root as it may cause unwanted system-wide changes and suggests using sudo instead. It is the traditional way to switch to the root account. Su is an older but more fully-featured command included in all Linux distributions. How sudo and su Work on Different Linux Distributions
#SU VS SUDO HOW TO#
Note: For an in-depth guide, refer to How to Add User to Sudoers Group on Ubuntu or How to Add User to Sudoers Group on CentOS. The output should display the account added in the previous step: To see a list of accounts that belong to the sudoers group run: sudo getent group sudo To add a user to the sudoers group, run the following command (as root or an account that already has sudo privileges): usermod -aG sudo įor instance, to add the account phoenixnap, you would type: sudo usermod -aG sudo phoenixnap Adding a User to the Sudoers Groupįor a user to execute a command that requires the sudo prefix, it has to be part of the sudoers group.
#SU VS SUDO PASSWORD#
In such cases, it is recommended to use sudo.īefore the system executes the command, it asks for the password of the current user.īear in mind that the sudo option can only be used by users who belong to the sudoers group.

Unlike su, the sudo command in Linux requires providing the password for the user running the command.Īll administrative and executable tasks require maximum permission (held by root). Sudo is used as a prefix to Linux commands, which allows the logged in user to execute commands that require root privileges. įor more details about the su command, see our article on How to Use the su Command With Examples. Note: Additional attributes that allow moving to another user and its login interface include: su - l (or -login). If the command is used without the argument, it switches to the superuser ( root) account. It does this by starting a login shell in the current directory and environment ( su) or by completely changing to the setting of the target user ( su -). The su command stands for substitute user, and it is mostly used for switching from one user to another. On the other hand, su switches to the root user completely, exposing the entire system to potential accidental modification. By doing so, the current user is only granted privileged for the specified command. Therefore, it is much safer to use sudo since it doesn’t include exchanging sensitive information.Īdditionally, it is advisable to stick to sudo when performing tasks that require root privileges. The main difference between the two is that su requires the password of the target account, while sudo requires the password of the current user. If you have to use su, then always use it like su - to make sure everything is sourced as it should be and nothing has been compromised with.Both su and sudo elevate privileges assigned to the current user. We can setup our /etc/sudoers file then use something like: sudo -i -u username It's not a good idea to share a single password between users, so it's best to not use su at all, instead we can use sudo, also there is no need to run something like: sudo su - username

You will get a login shell with the target user access.You're using your own password to run the su command as root (if you're permitted to).You will be dropped into a non-login shell (things like.You have to provide the target user's password.The other difference is using - with su it will switches to the user while running a login shell, without the - you will get a non-login shell. First of all using su username you have to provide the password of "username" to be able to switch into its account while with sudo su - username for a second with your own password you will become root, then without using any other password you are running the su - uesrname to switch into the "username".
