What is sudo in linux terminal

What is Sudo in Linux [A Detailed Review]

The sudo in Linux allows a user to run a command with elevated privileges. For instance, a hotel has different keys for different rooms. If you are a guest, you will only have the key to your own room. But what if you want to access a restricted area? You need to get the key from the hotel manager as he has access to all the rooms. And by doing so, you are giving yourself some privileges of the hotel manager. This is what sudo does. In this article, I will explain sudo with some practical examples.

What is Sudo in Linux

Linux is a multi-user system where different users enjoy different privileges for security and administrative reasons. If everyone had all privileges, the system would be at risk. For instance, a user could have deleted an important system file or installed harmful programs. However, there is a user who has access to everything and it is called the root user (or superuser). By using sudo, a regular user can grant the privileges of the root user or superuser temporarily. This is the reason the word “sudo” is the short form of “superuser do”.

Note: Even though root user and superuser are used interchangeably, there are slightly different. Superuser can also mean a user with elevated privileges granted by the system administrator.

Sudo vs Su

A. Sudo

  • It allows a user to perform a task that requires special privileges while being in the user’s own account.
  • Provides temporary
  • Limits
  • Requires password or other forms of authentication.
  • Less chance to harm the system.

B. Su

  • It is used to switch to the root user or another user.
  • Provides full
  • Doesn’t limit
  • Only requires the password of the target account.
  • More chance to harm the system.

Basic of the “sudo” Command

The sudo command in Linux is used to grant root privileges. Its syntax goes like the following:

Note: In the above syntax OPTION and COMMAND are enclosed by a square bracket and followed by 3 dots represents that you can utilize multiple options and commands simultaneously.

Options

The sudo command in Linux provides a lot of options. You can check them by using the following command:

Useful options

Читайте также:  Dallas lock astra linux установка

I have listed some useful options here

Examples of “sudo” command in Linux

Here, I will show you a few examples of sudo command in Linux. These examples will help you understand the concept better.

Example 1: Update the system using “sudo”

In this example, I will update the system. As updating a system is a sensitive task, only root user has this privilege. I will use sudo to grant that access. Now to update your system using sudo, do the following:

Steps to Follow:

❶ At first, launch an Ubuntu Terminal.

❷ Insert the following command in the command prompt and hit ENTER:

❸ Provide the password to access root privileges.

❹ Press ENTER from your keyboard.

Explaining

The “apt-get update” command updates the local package database with information of latest software packages and it won’t work without proper privileges and permissions. In the output, I have used sudo to take advantage of root privileges. And as you can see, the system is updating successfully.

Example 2: Execute a Command as a Different User

Now I will execute a command with the privileges of a different user instead of current user. For instance, my current user name is “walid”. However, my system has another user named “jim” and I want to execute a command as “jim”. I can do that by using the “-uoption of command sudo. Now to execute a command as a different user, follow the steps below:

Steps to Follow:

❶ Launch a Terminal in Ubuntu first.

❷ Write the following command in the Terminal and press ENTER:

❸ Provide password of the current user.

❹ Now find the ENTER key from your keyboard and hit it.

Executing a command as a different user using sudo

Here I have listed the contents of the “/vardirectory. You can do anything you want but make sure that the user has proper permissions.

Example 3: List the Permissions and Privileges

If you want to check whether you have root privileges or not, you can use the “-loption of the sudo command. To list the permissions and privileges of a user, do the following:

Steps to Follow:

❶ Press CTRL + ALT + T, to open an Ubuntu Terminal.

❷ Copy the following command in the Terminal and hit ENTER:

❸ Give your password.

❹ Now press ENTER from the keyboard.

In my case, the user “walid” is also the root user. In the output, “(ALL : ALL) ALL” denotes that user “walid” have permissions to execute all commands.

Example 4: Run Command without Password

When you use “sudo”, you are prompted for a password. However, the “-noption gives a way to run commands without a password. You only need to put the password once. Now to run a command without password, do the following:

Steps to Follow:

❶ Open a Terminal in Ubuntu.

❷ Type the command below in the Terminal:

❸ Hit the ENTER button.

Using sudo without password

In Example 1, I have updated the system as well. If you can remember, it was asking for a password back then. But in this case, it isn’t prompting for any password even though I am using sudo.

Читайте также:  Uninstalling windows and installing linux

Conclusion

Learning “what is sudo in Linux” is very important for any type of user. In this article, I have tried to provide a comprehensive view of sudo. Hopefully, you have got everything you need.

Similar Readings

  • What is Root User in Linux? [A Complete Guide]
  • What is Root Partition in Linux? [A Complete Guide]
  • What is Root Directory in Linux? [The Ultimate Guide]
  • What is Root Access in Linux [With Practical Examples]

Источник

How to Use the sudo Command in Linux

Sudo stands for SuperUser DO and is used to access restricted files and operations. By default, Linux restricts access to certain parts of the system preventing sensitive files from being compromised.

The sudo command temporarily elevates privileges allowing users to complete sensitive tasks without logging in as the root user. In this tutorial, learn how to use the sudo command in Linux with examples.

tutorial explains how to use the sudo command in linux

  • A system running Linux
  • Access to a command line/terminal window (Activities > Search > Terminal)
  • A user account with sudo or root privileges

How to use the sudo Command

sudo was developed as a way to temporarily grant a user administrative rights. To make it work, use sudo before a restricted command. The system will prompt for your password. Once provided, the system runs the command.

Syntax

To start using sudo , use the following syntax:

When the sudo command is used, a timestamp is entered in the system logs. The user can run commands with elevated privileges for a short time (default 15 minutes). If a non-sudo user tries to use the sudo command, it is logged as a security event.

Options

sudo can be used with additional options:

  • -h – help; displays syntax and command options
  • -V – version; displays the current version of the sudo application
  • -v – validate; refresh the time limit on sudo without running a command
  • -l – list; lists the user’s privileges, or checks a specific command
  • -k – kill; end the current sudo privileges

Additional options can be found under the -h option.

Options related to the sudo command in Linux.

Note: Staying logged in as an administrator compromises security. In the past, admins would use su (substitute user) to temporarily switch to an administrator account. However, the su command requires a second user account and password, which isn’t always feasible.

Granting sudo Privileges

For most modern Linux distributions, a user must be in the sudo, sudoers, or wheel group to use the sudo command. By default, a single-user system grants sudo privileges to its user. A system or server with multiple user accounts may exclude some users from sudo privileges.

We recommend to only grant privileges that are absolutely necessary for the user to perform daily tasks.

The following sections explain how to add a user to the sudoers group.

RedHat and CentOS

In Redhat/CentOS, the wheel group controls sudo users. Add a user to the wheel group with the following command:

usermod -aG wheel [username]

Replace [username] with an actual username. You may need to log in as an administrator or use the su command.

Читайте также:  Специальные биты защиты файлов astra linux

Debian and Ubuntu

In Debian/Ubuntu, the sudo group controls sudo users. Add a user to the sudo group with the following command:

Replace [username] with an actual username. You may need to log in as an administrator or use the su command.

Using visudo and the sudoers Group

In some modern versions of Linux, users are added to the sudoers file to grant privileges. This is done using the visudo command.

1. Use the visudo command to edit the configuration file:

2. This will open /etc/sudoers for editing. To add a user and grant full sudo privileges, add the following line:

Image of the visudo configuration file in Linux.

Here’s a breakdown of the granted sudo privileges:

[username] [any-hostname]=([run-as-username]:[run-as-groupname]) [commands-allowed]

Note: It’s easier to simply add a user to the sudo or wheel group to grant sudo privileges. If you need to edit the configuration file, only do so using visudo. The visudo application prevents glitches, bugs, and misconfigurations that could break your operating system.

Examples of sudo in Linux

Basic Sudo Usage

1. Open a terminal window, and try the following command:

2. You should see an error message. You do not have the necessary permissions to run the command.

example of an action in Linux that requires elevated permissions

3. Try the same command with sudo :

4. Type your password when prompted. The system executes the command and updates the repositories.

system updating the official repositories when sudo invoked

Run Command as a Different User

1. To run a command as a different user, in the terminal, enter the following command:

2. The system should display your username. Next, run the following command:

sudo -u [different_username] whoami

3. Enter the password for [different_username] , and the whoami command will run and display the different user.

Using the sudo -u command to run a command as another user

Switch to Root User

This command switches your command prompt to the BASH shell as a root user:

Your command line should change to:

The hostname value will be the network name of this system. The username will be the current logged-in username.

Using the sudo bash command to switch root user

Execute Previous Commands with sudo

The Linux command line keeps a record of previously executed commands. These records can be accessed by pressing the up arrow. To repeat the last command with elevated privileges, use:

This also works with older commands. Specify the historical number as follows:

This example repeats the 6th entry in history with the sudo command.

To learn about how to efficiently use history command, check out our article on sudo history command with examples.

Run Multiple Commands in One Line

String multiple commands together, separated by a semicolon:

Running multiple sudo commands.

Add a String of Text to an Existing File

Adding a string of text to a file is often used to add the name of a software repository to the sources file, without opening the file for editing. Use the following syntax with echo, sudo and tee command:

echo ‘string-of-text’ | sudo tee -a [path_to_file]
echo "deb http://nginx.org/packages/debian `lsb_release -cs` nginx" \ | sudo tee /etc/apt/sources.list.d/nginx.list

Invoking the sudo command to add the Nginx repository

Note: This would add the Nginx software repositories to your system.

You should now understand the sudo command, and how to use it. Next, learn the difference between the sudo and su command.

Источник

Оцените статью
Adblock
detector