|
Today We’re going to discuss sudo and su, the very important and mostly used commands in Linux. It is very important for a Linux user to understand these two to increase security and prevent unexpected things that a user may have to go through. Firstly we will see what these commands do then we’ll know the difference between both of them. So let’s get started.
Introduction to Linux command ‘sudo’
Introduction to Linux command ‘sudo’In Ubuntu Linux there is not root account configured by default. If users want root account password then they can manually set it up oo can use ‘sudo’. As we all know, Linux in many ways protects users’ computer being used for bad purposes by some nasty people around us. Using sudo is one of those good ways. Whenever a user tries to install, remove and change any piece of software, the user has to have the root privileges to perform such tasks. sudo, linux command is used to give such permissions to any particular command that a user wants to execute. sudo requires the user to enter user password to give system based permissions. For example user wants to update the operating system by passing command –
apt-get update
The above command will give following error-
This error is due to not having root privileges to the user ‘sandy’. The root privileges can be required by passing sudo at the very beginning, like below-
sudo apt-get update
The above command will execute command and the operating system will update-
As you can see when I used apt-get update that is a packaging management tool and through that I tried to update my system but it failed because to make this command work for me, I must have root privileges. So the next time I used the same command along with ‘sudo’ and this time sudo command asked user password to have root privileges to update system. After entering user password it system updated.
But there may not be all the user accounts able to use sudo. As a system administrator, he has to give the rights whether any particular user can sudoer to do particulars admin tasks. To read that in description jump over here on the official page. Some more examples of ‘sudo’ –
sudo apt-get install {package-name}
This command will install packages with the root privileges.
sudo apt-get remove {package-name}
This command will remove packages with the root privileges.
sudo apt-get update {package-name}
This command will update packages with the root privileges.
Introduction to Linux command ‘su’The Linux command ‘su’ is used to switch from one account to another. User will be prompted for the password of the user switching to.
Users can also use it to switch to root account. If user types only ‘su’ without any option then It will be considered as root and user will be prompted to enter root user password.
As you can see su asked me root password and gave error ‘Authentication failure’ because I have not setup root password. I mentioned above in most distributions root password is not configured by default. Once the root password is setup then you can use it here to switch to root account very quickly.
There is one more benefit of ‘su’ command that you can swith to any of the user account without user password. No need to remember different passwords for different user account; just user ‘su’. How to switch to root user without configuring root passwordSwitching to root user without configuring root password seems to be confusing because above I said to switch to root user the normal user needs to configure root password manually. But stop! Here is a way to do that without configuring root password; just use ‘sudo’.
We can use sudo and enter normal user password to switch to root user. Here see how we can do that with the power of sudo – Using ‘su’ command to have functionality similar to ‘sudo’If user only uses ‘su’ command and want to use ‘su’ as ‘sudo’ then it can be done. (here root password is assumed to have been configured because user is familiar with ‘su’.)
To achieve same sudo functionality to execute any single command user has to use ‘-c’ option of ‘su’. Here is how to do it – $ su -c apt-get install vlc
After hitting enter user will be prompted for password and obviously it’s for root password because we’re using ‘su’ command.
Using ‘sudo’ command to have functionality similar to ‘su’Above we have seen ‘su’ having similar functionality as sudo and it’s time to see how we can do same with the command ‘sudo’ and achieve same ‘su’ functionality.
To achieve same ‘su’ functionality in ‘sudo’ just use ‘-i’ option of ‘sudo’. Here is how we can do it – When user hits enter, it will ask password its the user password not the root password.
It’s all done! $ man sudo
$ man su Congratulation! You now have the basic knowledge the mostly used commands in Linux.
We’ll learn more about these commands in our another post, like to create/configure root password manually etc. Also read article APT Packaging Management Tool In Detail; Linux |