How To Use 'Sudo' And 'Su' Commands In Linux : An Introduction

xx How To Use 'Sudo' And 'Su' Commands In Linux : An Introduction
January 11, 2015, 09:42:26 am by MohdSohail
How To Use 'Sudo' And 'Su' Commands In Linux : An Introduction

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'

In 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 -

Code: [Select]
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-

Code: [Select]
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' -

Code: [Select]
sudo apt-get install {package-name}

 This command will install packages with the root privileges.

Code: [Select]
sudo apt-get remove {package-name}

 This command will remove packages with the root privileges.

Code: [Select]
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.

Code: [Select]
$ su linuxandubuntu
password:
linuxandubuntu@sandy:~$

Users can also use it to switch to root account. If user ty
pes only 'su' without any option then It will be considered as root and user will be prompted to enter root user password.



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 -

Code: [Select]
$ 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!

You can learn more about these commands through man pages.

Code: [Select]
$ 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


Write a Comment:

Sorry, you must be logged in to post a comment!

shortcuts: hit alt+s to submit/post or alt+p to preview

Comments:

[1]
xx Re: How To Use 'Sudo' And 'Su' Commands In Linux : An Introduction (Reply 1)
January 11, 2015, 11:12:24 am by Mark Greaves (PCNetSpec)
Quote
In 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'.

You're going to draw a lot of flack from non Ubuntu users with statements like that ;)

Ubuntu's way of doing things is not necessarily universal in "Linux" (in fact more and more Ubuntu are diverging from the "norm" and that in itself rightly or wrongly draws Canonical a lot of flack) .. Truth is most non Ubuntu based distros *do* have a root account configured by default, including Ubuntu's upstream Debian.

My advice would be to leave that statement out altogether .. it's not necessary for the article to work.
xx Re: How To Use 'Sudo' And 'Su' Commands In Linux : An Introduction (Reply 2)
January 11, 2015, 04:33:24 pm by MohdSohail
Yeah!! There is a confusion actually, I's specifically talking about Ubuntu not other distros. So take linux as ubuntu Because that's true other distributions do have that configured by default. Ubuntu is obviously not followed as linux, Linux has so many distributions that have their own ways of working.
[1]
Powered by SMFBlog by CreateAForum.com

SimplePortal 2.3.3 © 2008-2010, SimplePortal