What is Sudo And why would you use it?. What is Sudo in Linux command?.
Consider the above comic from the excellent XKCD web comic. The lazy guy in the chair wants their friend to make them a sandwich, but he has no authority to make them do it. That is, until it invokes the powerful Sudo command. After that, one way or another, a sandwich will be prepared.
What is Sudo in Linux command?
While attempting sudo on humans in real life probably won’t be as effective, it is a magical 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 is a Creative Commons non-commercial credit attributed to XKCD
Understanding Linux User Permissions
Linux is considered a secure operating system because of the way it handles permissions. While operating systems like macOS (which shared a common ancestor with Linux) and Windows are now more like Linux in this regard, the open source operating system is still unique to some extent.
Understanding how Linux handles permissions makes the sudo command much easier to understand. All modern operating systems have administrator or root access rights. If you have an administrator or root account, you can change any settings, delete any data, and generally do whatever you want with the computer.
This includes things you probably shouldn’t do that could result in data loss or the need for a complete wipe and reinstall.
Linux does not make root user level permissions by default. Instead, your account cannot access the really important parts of the system without raising your permissions. This means that the system will ask you for an administrator password if you want to do something unusual using the graphical interface.
However, if you want to use the terminal command line to perform tasks, sudo is the safest and most efficient way to do it.
Sudo and the Terminal
There are two ways to give yourself elevated privileges on Linux. One of them is to be permanently logged in as root user. The problem is that anyone who can access your computer can cause damage, and even you can do it by accident. Sudo only boosts your permissions for a short time to execute certain commands that follow.
The Sudo syntax (command format) is simple. Just enter “sudo” followed by the command you want to execute.
For example, “sudo apt-get update” will update all application repositories listed in the corresponding file. If you try to run it without sudo, you will get an error stating that you do not have permission. By the way, this is usually the first sudo command you want to run after a fresh install of your favorite Linux distribution.
The “Su” in Sudo
“Su” in sudo is short for “superuser” and is a separate command. The “su” command allows you to change the user privileges that sudo elevates you to.
Although sudo temporarily promotes you to root, su changes you to another user with appropriate privileges. This may seem like an insignificant difference, but there are good reasons to change the account that sudo elevates the user to.
First of all, changing the account means that regular users do not know the root password. Secondly, there is a log of all sudo commands, which means that the system administrator (root) can find out who executed the su commands.
The su syntax is essentially the same as sudo:
Su USER NAME -c COMMAND
Replace USERNAME with the desired user to execute the command, and COMMAND with the Linux command you want to execute.
If you want to run multiple commands as a different user, just use:
Replace USER with the desired user account ID.
If you use su alone, Linux will switch to a different user account until you use the “exit” command. It is important to remember that this or the next user to access the terminal in this session will still have elevated permissions. This is why it is usually better to use sudo than su.
The Sudo Time Limit
The first time you use the sudo command, you will be prompted for a password. This password will then be valid for 15 minutes. You can change this default by running the sudo visudo command and changing “timestamp_timeout =” to a longer or shorter value. However, we do not recommend doing this unless you have a compelling reason to extend or shorten the sudo password.
Sudo Option Switches
While the sudo syntax is straightforward, there are some switches to be aware of. These commands provide additional information or help manage the sudo session:
- -h shows the syntax and command information for sudo.
- -V displays the current version for sudo on your machine.
- -v updates the sudo time limit by restarting the clock.
- -l lists user rights.
- -k ends the current sudo session immediately, removing elevated privileges.
There are many more options built into sudo, and you can see them all using the first -h switch above.
The above screenshot shows the results when using the help option.
Useful Sudo Commands
So what commands supported by sudo should every Linux user know? We have already covered sudo apt-get update, but note this too:
- Sudo apt-get upgrade will update all installed packages.
- Sudo apt -get install installs software of your choice; just change the package name to whatever you want to install.
- If you don’t know the package name, use dpkg –list.
- If you want to remove an installed package from the terminal use sudo apt-get remove
These are most likely the first sudo commands you will need to use, but as you learned above, any command can follow sudo, but you should only use those that require higher privileges.
What is Sudo in Linux command?
What is Sudo And why would you use it?