Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

The above question can also be rephrased to, what is the use of the sudo command?

man pages for sudo in the bash shell has the following description. sudo, sudoedit - execute a command as another user.

This doesn't make any sense to me. Don't I have to be the root user to install from terminal? How does using sudo before the yum install command help?

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

sudo command has setuid bit set which means that it is always granted privileges of the user who owns the file (it's always root unless you messed up something yourself). So even if you don't have root privileges, sudo will get them anyway. All programs with setuid are written in especially careful way to prevent vulnerabilities.

sudo reads sudoers file to determine if you are allowed to execute selected command as root and if you should be prompted for your password. If you are allowed to run the command and the password is correct (if needed), since sudo has root privileges, all of its children (yum and install scripts maybe) also gain those privileges.

It was especially relevant years ago when mainframes were used by big number of people and users with root access wanted to allow some trusted users to execute some often used and not very dangerous commands. Nowadays sudo access is usually granted for all commands (on home desktops at least).

share|improve this answer
    
I wish I could understand what you said, but as you can see from my question, I'm a newbie. So, when you say it is always granted privileges of the user who owns the file, what if user x was the owner. Does that mean that the file will not get installed? Will the sudo command only work if root is the owner of the file? –  Thomas Jul 23 '11 at 9:26
    
I think I understood the bit where you said sudo reads sudoers file to determine if you are allowed to execute selected command as root. So there's a file called sudoers which has information about which users (not root) can run which commands that require root priviledges as normal users. Right? –  Thomas Jul 23 '11 at 9:26
1  
1. Yes, sudo will work only if its owner is root (which is always the case in all distributions unless you messed up something yourself) 2. Yes (it was especially relevant years ago when mainframes were used by big number of people and users with root access wanted to allow some trusted users to execute some often used and not very dangerous commands). I'd suggest you to google about setuid, sudo and sudoers. –  Anton Barkovsky Jul 23 '11 at 9:33
    
Thank you Anton. Big help. –  Thomas Jul 23 '11 at 9:37
add comment

If you're already in a shell with root privileges, then you're correct; there's no point in using sudo. But sudo allows you to run a command as root without being logged in as root.

The original su (switch user) command allows you to run a program as a different user. It takes an all-or-nothing approach. If you know the password of that user (or you're already root), you can do whatever you want. If you don't know the password, you can't do anything.

The newer sudo command is smarter. It can be configured to allow only certain commands. It can also be configured to ask for your password, instead of the password of the other user (like su asks for). (In fact, that's sudo's normal configuration.)

share|improve this answer
    
Ah! So you can install packages in Linux without having to be the root user. And the sudo command lets you do that because it asks for your user password (when logged in as a user in shell), not the root password. Correct? –  Thomas Jul 23 '11 at 9:33
1  
@Thomas, yes. Most current Linux distros set up sudo to allow your primary user (the one you created during installation) to run any command you want with root privileges by using sudo. –  cjm Jul 23 '11 at 10:55
    
It's generally recommended you use sudo instead of a root shell, because it means one extra "are you sure about this?" check, and it significantly reduces the risk of disasters caused by not paying close attention to which terminal window had focus when you were typing... –  Shadur Jul 23 '11 at 11:33
    
@Thomas - sudo let's you do root-level stuff without being root, but... it must be configured by root to give access to groups or individuals. This is often done by giving sudo priveleges to the group wheel, and then adding users to this group. So you can't just use sudo to get around not having root access. As mentioned by cjm, the install process, which is run as root, will usually set up the primary user... but not always. It depends on the distro/OS... they don't all force you to add a non-root user at install... so you would configure it manually via root account. –  Joe Internet Jul 23 '11 at 14:18
add comment

If you can run yum install [...] without using sudo it means you are running as the root user. In that case using sudo is pointless.

What sudo allows you to do is to run other things with escalated privileges from normal user accounts using the normal user passwords, not root's.

You should seriously consider always using your system as normal user and using sudo whenever you need to run a specific command with more privileges.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.