sudo vs su
Written by Guillermo Garron
Date: 2019-06-08 20:40:00 00:00
With Linux like with most things in life you have different ways to accomplish a specific task, this is the case of su
and sudo
they are different commands with different goals but they are usually used one instead the other.
From the man page:
The su command is used to become another user during a login session. Invoked without a username, su defaults to becoming the superuser. The optional argument - may be used to provide an environment similar to what the user would expect had the user logged in directly
In few words su
command switches user, from your current user to any other one, it may be root or not
su john
will switch your current user to john, provided you know john's password, if you want to get all environment variables that john get when he logs in you can use:
su - john
After that you can run commands in john's environment
su -
or
su - root
Will switch you to root, you will have to know root's password
sudo
is a different beast, if you want to give some users access to perform some tasks as root without given them root's password, you will have to use sudo
command, in this case those users just have to enter their own passwords and run something like this:
sudo ls /
That will ask user's password and then run ls
command as root, you have to configure the sudoers file
As you can see, one can achieve same goal (run privileged commands) with both su
and sudo
they work in different ways and they are made for different things, it is up to you to use the most appropriate command deppending on your needs and general scenario.
su
will switch you to another user environment will open a session of that user, and you will become that user, it can be root or any other, sudo
will upgrade your privileges and let you run commands as root, provided you have the rights, that is you are listed in sudoers file.