Sudo command

If you’ve had issues running a command on Linux only to be granted “Access Denied,” you’ve come to the right place.

This is where the Sudo command comes in.

It’s the all-powerful magic word you can use that grants you the “power” to carry certain tasks, which would otherwise be limited or restricted to the user.

What exactly can this newfound power grant you and your system? Read on to find out!

What is Sudo For?

Sudo stands for “super user do.”The word su-do is pronounced the way you would say, “Sue Dough.”

Basically, the Sudo command grants permission to a user to execute certain commands with elevated administrative privileges.

You might need to run a command to modify a few configuration files for example. Without Sudo, it’s not as easy as opening and closing a door.

It’s possible that the changes you’ve made won’t save after you’ve modified it. The command you just typed might also refuse to run. Or, the famous “System Error” prompts up leaving you stuck.

Running into these roadblocks happen, and Sudo will grant you elevated access to carry out these tasks.

Normally, you can always log into your root user account and carry out these commands; however, that also makes your system more susceptible to malware and corrupted data.

How does Sudo work?

With Sudo, it’s like saying please or “Open Sesame.”

Imagine asking someone next to you to pass the ketchup bottle by saying please. The word “please” sends a message that you’re asking for permission to carry out a certain command. Once the system recognizes your Sudo/permission, the system will hand you the ketchup bottle with ease.

To use Sudo, let’s say you want to restart your system. If you try to run a command using the reboot command, the command will fail. You’ll see a message telling you: “must be superuser.”

If you add the command Sudo to the reboot command, the system will recognize the “sudo reboot” command and ask for your password (this password is not the same as your root password).

Once you type in your password, the reboot will initiate and everything will be smooth sailing from there.

Using Sudo is as simple as typing in “sudo” followed by the command. If you want to know the commands you can execute with Sudo, simply type “sudo -l”

You’ll find a general guide of the different sudos or sudo commands to use. For example, if you want to execute a command logged in as another user, you can type “sudo -u username” followed by typing the password.

Why Using Sudo is Important

In a Linux system, asking for permission is important if you don’t want to run into an accident of losing all your saved files.

If you want to run a LAMP server, edit packages, create another user or group, or update and install your system, using the command Sudo is the way to go.

As the system administrator or root user, you can also grant access to other users using the Sudo command.

Using Sudo is also more secure than logging in as the root user because you can grant another user certain administrative privileges while keeping your account safe.

Alternatives to the Sudo Command

Using Sudo allows a user to execute important or sensitive commands in the system. Alternatively, you can use the switch user command “Su.”

Although, I don’t recommend using “Su”, especially if you’re the forgetful type.

Using the “Su” command will prompt the system to ask for your root password and provide the # symbol, which warns you you’re logged into your root account.

Inputting the password will grant you full access to make any changes you deem fit. This isn’t a problem until you forget to log out as the root user.

All it takes is one slip or typo and it’s downhill from there.

You could accidentally type a command that deletes 5 years worth of data, files, and saved gems of hard work. That being said, if you use the Sudo command, you don’t have to worry about logging out as the root user.

With Sudo, you’ll have fewer accidents and protect any sensitive file you have with better security & less risk.

How to Install Sudo

Having Sudo access is usually found in most Linux installations. If; however, you don’t have Sudo access you can always check the system by opening your console or PC, then type Sudo and hit “Enter.”

If you don’t have Sudo, you’ll be prompted with a message that says “Sudo command not found.” If you do have Sudo, your system will prompt you with a short display message.

To install Sudo, it’s a simple process that won’t take even 5 mins.

If you have Ubuntu/Debian follow these steps to install and use the Sudo command:

  1. Open your terminal and log in as the root user using the “Su” command
  2. Type in your password
  3. Afterward, type “apt-get update”
  4. Once you have the update file, type “apt-get install sudo.” The Sudo file installation should take between 5 to 10 seconds
  5. With the Sudo file installed and your Sudo command now in place, you won’t be able to run Sudo command just yet. You still have to add the user (you) to the sudoers file.
  6. To add a user to the sudoers file, you’ll have to repeat Steps 1 and 2.
  7. Type “usermod -aG sudo” followed by your username. For example, if your username is Dave then type, usermod -aG sudo Dave
  8. Don’t forget to exit out of your root account.
  9. Restart your system. You can do this manually or type in, “su -c shutdown -r now”
  10. You now have Sudo installed!

If you have CentOS or Fedora, instead of “apt-get install sudo”, type in “yum install sudo”

To add another user in CentOS or Fedora in the sudoers file, type in “usermod -aG wheel username”

What is Sudoers File?

The /etc/sudoers file is what grants access or permission to another user so he/she can run certain commands or have root account privileges.

Inside your /etc/sudoers, you’ll find different aliases or group members and user specifications. User specification in the /etc/sudoers file specifies the parameters like who can control or has access to certain commands.

If you plan to edit this file, the safest way is to use the visudo command. This will help you specify which members can perform certain commands. The steps below will show you how to use the visudo command:

  1. Open the terminal and type in visudo. You should be logged into your root account.
  2. Look for “## Allow root to run any commands anywhere” You’ll see 3 columns. The 1st column shows the account name, the 2nd column shows the terminals a user is allowed to use, and the 3rd column is the command a user is allowed to use.
  3. If the user’s name you want to add is Dave, for example, add that to the 1st column. In the 2nd column, type in “ALL=(ALL)”.
  4. In the 3rd column, this depends on the command you want the user to run. If you want to grant user access for the clock command for example, in the 3rd column, type in “/sbin/clock”. When Dave logs in the terminal, he’ll have to type “sudo clock” and provide a password to run this command.
  5. If you want to remove the password, type in, “NOPASSWD: /sbin/clock”. If you want to grant user access for all commands, you can easily type in “NOPASSWD: ALL”

Should I Use Sudo?

Using the Sudo command is not only safe & secure but it’s also extremely useful for granting elevated access so a user can run or execute essential commands.

Granting Sudo access to other users also protects the privacy of your root account, minimizes the risk of hacking, and allows you to manage different accounts safely & effectively.

If you’re ready to use Sudo and want to master your proficiency using Sudo commands, you can also read more about the Linux command line to give you an even more powerful way of delivering productivity in your system.