Whats the difference between SUDO and Changing to root [duplicate]

If I issue a

sudo /bin/bash`  
[sudo] password for mark:
cmp -bl /dev/blah1 /dev/blah2

and using

sudo cmp -bl /dev/blah1 /dev/blah2

then logging in as root?

I don't see whats different, if issuing multiple command lines then rooting IMHO seems the logical option over typing sudo prefix to command lines.

I have seen sudo -i being used to log in to root, that used to work but the last week its issuing an error.

** EDIT **

I logged in as root, issued the command

mark@mark-zotac:~$ sudo cmp -bl /dev/sda "/media/mark/Seagate Expansion Drive/SSD/ssd.img"
[sudo] password for mark: 
mark@mark-zotac:~$

It ran, took about 30 minutes to run but produced zero as in nothing output. I am left with no indication of failure or success... So how do I know if the compare result was a success?



The following has been extracted directly from the community wiki page on root sudo.

sudo allows user to act as root without root login; it is more secure to use sudo instead of logging in as root.

Advantages and Disadvantages

Benefits of using sudo

There are a number of benefits to Ubuntu leaving root logins disabled by default, including:

  1. The installer has fewer questions to ask.
  2. Users don't have to remember an extra password for occasional use (i.e. the root password). If they did, they'd be likely to forget it (or record it unsafely, allowing anyone to easily crack into their system).
  3. It avoids the "I can do anything" interactive login by default. You will be prompted for a password before any major changes can happen, which should make you think about the consequences of what you are doing.
  4. sudo adds a log entry of the command(s) run (in /var/log/auth.log). If you mess up, you can go back and see what commands were run.
  5. On a server, every cracker trying to brute-force their way in will know it has an account named root and will try that first. What they don't know is what the usernames of your other users are. Since the root account password is locked, this attack becomes essentially meaningless, since there is no password to crack or guess in the first place.
  6. Allows easy transfer for admin rights by adding and removing users from groups. When you use a single root password, the only way to de-authorize users is to change the root password.
  7. sudo can be setup with a much more fine-grained security policy.
  8. The root account password does not need to be shared with everybody who needs to perform some type of administrative task(s) on the system (see the previous bullet).
  9. The authentication automatically expires after a short time (which can be set to as little as desired or 0); so if you walk away from the terminal after running commands as root using sudo, you will not be leaving a root terminal open indefinitely.

Downsides of using sudo

Although for desktops the benefits of using sudo are great, there are possible issues which need to be noted:

  1. Redirecting the output of commands run with sudo requires a different approach. For instance consider sudo ls > /root/somefile will not work since it is the shell that sets up the redirections that tries to write to that file, not the command being run with root permission. You can use

    ls | sudo tee -a /root/somefile
    

    to append, or

    ls | sudo tee /root/somefile
    

    to overwrite contents. You could also pass the whole command to a shell process run under sudo to have the file written to with root permissions, such as

    sudo sh -c "ls > /root/somefile"
    
  2. In a lot of office environments the ONLY local user on a system is root. All other users are imported using NSS techniques such as nss-ldap. To setup a workstation, or fix it, in the case of a network failure where nss-ldap is broken, root is required. This tends to leave the system unusable unless cracked. An extra local user, or an enabled root password is needed here. The local user account should have its $HOME on a local disk, not on NFS (or any other networked filesystem), and a .profile / .bashrc that doesn't reference any files on NFS mounts. This is usually the case for root, but if adding a non-root rescue account, you will have to take these precautions manually. However the advantage of using a local user with sudo is that commands can be easily tracked, as mentioned in the benefits above.

Misconceptions about root login

  1. Isn't sudo less secure than su?

    The basic security model is the same, and therefore these two systems share their primary weaknesses. Any user who uses su or sudo must be considered to be a privileged user. If that user's account is compromised by an attacker, the attacker can also gain root privileges the next time the user does so. The user account is the weak link in this chain, and so must be protected with the same care as root.

    On a more esoteric level, sudo provides some features which encourage different work habits, which can positively impact the security of the system. sudo is commonly used to execute only a single command, while su is generally used to open a shell and execute multiple commands. The sudo approach reduces the likelihood of a root shell being left open indefinitely, and encourages the user to minimize their use of root privileges.

  2. I won't be able to enter single-user mode!

    The sulogin program in Ubuntu is patched to handle the default case of a locked root password.

  3. I can get a root shell from the console without entering a password!

    You have to enter your password.

    Console users have access to the boot loader, and can gain administrative privileges in various ways during the boot process. For example, by specifying an alternate init(8) program. Linux systems are not typically configured to be secure at the console, and additional steps (for example, setting a root password, a boot loader password and a BIOS password) are necessary in order to make them so. Note that console users usually have physical access to the machine and so can manipulate it in other ways as well.

Special notes on sudo and shells

None of the methods below are suggested or supported by the designers of Ubuntu.

Please do not suggest this to others unless you personally are available 24/7 to support the user if they have issues as a result of running a shell as root.

To start a root shell (i.e. a command window where you can run root commands), starting root's environment and login scripts, use:

  • sudo -i (similar to sudo su - , gives you roots environment configuration)

To start a root shell, but keep the current shell's environment, use:

  • sudo -s (similar to sudo su)

Here's a brief overview of some of the differences between su, su -, and sudo -{i,s}

                HOME=/root      uses root's PATH     env vars
sudo -i         Y               Y[2]                 N
sudo -s         N               Y[2]                 Y
sudo bash       N               Y[2]                 Y
sudo su         Y               N[1]                 Y

[1] PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games  (probably set by /etc/environment)

[2] PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin

The question has nothing related to difference between sudo and su.

The man page of cmp reads:

The cmp utility compares two files of any type and writes the results to the standard output. By default, cmp is silent if the files are the same; if they differ, the byte and line number at which the first difference occurred is reported.

So no output means that the two files are the same.