What Is Sudo in Linux and How To Use It

What Is Sudo in Linux and How To Use It:

Consider the above comic from the excellent XKCD webcomic. The lazy man sitting on the chair wants his friend to make him a sandwich, but he does not have the authority to do so. That is, until it invokes the powerful sudo command. After which the sandwich will be made in some way or the other.

Trying sudo on a person in real life might not be as effective, it’s the magic command that overcomes every obstacle in the Linux world. What is sudo? Why does it even exist? The answer lies in how Linux handles permissions.

this cartoon Creative Commons Attribution – by XKCD is uncommercial

Understanding Linux User Permissions

Linux is considered a secure operating system because of how it handles permissions. While operating systems such as macOS (which share an ancestor with Linux) and Windows are now similar to Linux in this regard, open source operating systems still have something unique.

Understanding how Linux handles permissions makes it a lot easier to get your head around the sudo command. All modern operating systems have an “administrator” or “root” user permission level. If you have an administrator or root account, you can change any settings, delete any data, and generally do whatever you like with the computer.

This includes things you probably shouldn’t do that could lead to data loss or require a complete wipe and restore.

Linux does not default to root user level permissions. Instead, your account cannot access really sensitive parts of the system without increasing your permission level. This means that the system will ask you to enter the administrator password when you want to do something different than usual using the graphical interface.

However, when you want to use terminal Command line to get the job done, sudo is the safest and most efficient way to go about it.

sudo and terminal

There are two ways to give yourself elevated permissions in Linux. To be logged in permanently as a root user. The problem with this is that someone else who might be using the computer can wreak havoc, and even you can do it by accident. Sudo elevates your permissions for only a short period of time so that specific commands that follow it can be executed.

sudo syntax

The syntax (format of the command) of sudo is simple. Simply type “sudo” followed by the command you want to execute.

For example, “sudo apt-get update” will update all app repositories listed in the relevant file. If you tried to run it without sudo, you would get an error message telling you that you do not have permissions. Incidentally, this is usually the first sudo command you want to run after a fresh install of your favorite Linux distro.

“su” in sudo

The “su” in sudo is short for “superuser” and is a standalone command. The “su” command lets you change which user privileges sudo elevates to you.

Although sudo elevates you to root temporarily, su changes you to another user with the appropriate privileges. This may seem like an insignificant difference, but there are good reasons to change the account that elevates the sudo user.

First, changing the account means regular users don’t know the root password. Second, there is a log of all sudo commands, which means that the system administrator (root) can see who issued the su command.

The syntax for su is essentially the same as for sudo:

su username -c command

Replace USERNAME with the desired user to run the command and replace command with the Linux command you want to execute.

If you want to run multiple commands as another user, just use:

su user

Replace USER with the desired user account identity.

If you use su yourself, Linux will switch to another user account unless you use the “exit” command. This is important to remember or the next user who accesses the terminal in that session will still have elevated permissions. This is why it is usually better to use sudo instead of su.

sudo time limit

The first time you use the sudo command, you will need to enter a password. Then, that password will be valid for 15 minutes. You can change this default by running the command sudo visudo and changing “timestamp_timeout=” to a longer or shorter value. However, we don’t recommend doing so unless you have a good reason to extend or shorten the length of time the sudo password is valid.

sudo option switch

Although the sudo syntax is simple, there are several switches worth knowing about. These commands open additional information or help you control the sudo session:

  • h Shows you the syntax and command information for sudo.
  • -V Displays the current version for sudo on your machine.
  • -V sudo refreshes the time limit, restarts the clock.
  • -l Lists user privileges.
  • -K Kills the current sudo session immediately, removing elevated privileges.

There are many more options built into sudo, and you can see them all using the -h switch first listed above.

What is the result the above screenshot gives when you use the help option.

Useful sudo commands

So what commands should every Linux user know empowered by sudo? We’ve already covered sudo apt-get update, but take a look at:

  • sudo apt-get upgrade Will upgrade all installed packages.
  • sudo apt-get install <पैकेज-नाम> Installs the software of your choice; Simply change the name of the package you want to install.
  • If you don’t know the package name, use dpkg list.
  • If you want to remove installed package from terminal, use sudo apt-get remove (again substituting the specific package name in question).

These are probably the first sudo commands you’ll need to use, but as you learned above, any command can follow sudo, but you should only use those that require elevated privileges with it.