fbpx sudo in Linux: Run Commands as Root Safely | Certificação Linux

sudo in Linux: Safely Run Commands as Root

📅 01/01/2019 ⏱ 5 min ✍️ Uira Ribeiro
Linuxcommands
sudo in Linux: Safely Run Commands as Root
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

The sudo command on Linux executes a given command as if it were another user. It is used on a daily basis in system administration tasks, mainly to escalate privileges from the root account.

If the user who executed sudo is a regular user, they will need to know the password of the user they want to execute the command as.

If the user who executed sudo is root, sudo will not ask for any user's password, since root has superpowers.

Example:

# sudo -u uira /bin/mail [email protected] Subject: testeEstou mandando uma mensagem como se eu fosse o prof. Uirá.EOT

In this example, the root user is sending an email as if they were the user "uira".

Note that, if another user tries to make sudo execute a program with another user's permissions, sudo will request that user's password:

uira@linux-7rxb:~> sudo -u carla /bin/mailcarla’s password:

In this example, the user "uira" is trying to send an email as if they were the user "carla".

sudo is useful when you want a certain process to run as a specific user, so that file permissions are correct for proper operation.

The su command

The su command executes the shell as if it were another user.

Example:

uira@linux-7rxb:~> su -Password:

In this example, the user "uira" requests that the Shell be executed as the user "root". This way, the user "uira" will gain all the superuser account permissions on the system.

The "-" signal causes the shell login scripts of the "root" account to be read, as if "root" itself logged into the system.

You can also specify a user to execute the shell as:

# su uira -

In this example, the root user will execute the shell as if they were the user "uira".

In more secure systems, the "root" user is never authorized to log directly into the system. This way, users must log in with their access accounts and, if needed, they should execute superuser commands using "sudo" or access the root account with "su".

To log in as root, you can use the command:

$ su root -

In this case, you need to know the password of the root user.

For this reason, it is common for a user who wants to gain root privileges to use the command:

$ sudo su -

This way, if your user is in the group that has permissions to execute sudo in the /etc/sudoers file, you only need to re-authenticate with your own password, not the root user's password. This authentication is only to confirm that you are yourself. Depending on the configuration, this authentication may not even be required.

The sudoedit command

The sudoedit command is useful for editing files with another user's permissions, especially root.

There are two situations where using sudoedit is worthwhile:

The first is when the system administrator wants to give permissions to a certain user to edit a file to which this user does not have write (w) permissions, and the administrator does not want to give full "sudo" permissions to the user.

The trick is to create a group of users that can do sudoedit with the groupadd command:

# groupadd gruposudoedit

And create a line in the /etc/sudoers file for the group, allowing the gruposudoedit group to execute the sudoedit command for the specific file:

%gruposudoedit ALL = sudoedit /diretorio/arquivo

Once this is done, any user who is part of the gruposudoedit group can edit the file with the sudoedit command:

$ sudoedit /diretorio/arquivo

The second more common situation is when the administrator needs to edit a system file, especially configuration files in the /etc directory.

The administrator can use the "sudo vi" command to execute the "vi" text editor as root, and edit the file:

$ sudo vi /etc/services

The problem is that this way, "vi" will lose all the colored text markings:

Vi with colored highlighting

The best thing to do in these cases is to use sudoedit, since it edits the file, saves a temporary copy, and only then copies the temporary copy to the original file.

As an IT professional, support technician, or developer, have you thought about learning Linux for real?

And as a bonus, also get an LPI Linux Essentials certification?

And even better, with the best Online course for Linux?

[RICH_REVIEWS_SHOW category="CURSO" num="all"]

Do what these people did and create your account now.

Here you have everything you need to succeed in the LPI exams

Want to know if a certification is worth it? Learn why salaries are good, and why companies are looking for Linux professionals.

You will understand that the benefits far outweigh the sacrifice.

And if you have already decided, check out our Certification Guide.

And if you want to know where to take the exams, check the city list.

If you want to know the exam language, see this post.

Learn much more about Linux in our online course. You can enroll here. If you already have an account, you can access it here.

Liked it? Share

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp