sudo

Introduction

sudo is a short form of

substitute user and do

sudo is a program for system administration of UNIX systems that allows delegating certain privileged resources to users while keeping an operations log.

The main idea is to give users as few rights as possible, while still enough to solve the tasks at hand.

The program is supplied for most UNIX and UNIX-like operating systems.

The sudo command gives users the ability to run commands as the root superuser or as other users.

The rules sudo uses to decide whether to grant access are in the file /etc/sudoers (to edit the file you can use the special editor visudo, started from the command line without parameters, including without specifying the path to the file); the language they are written in and usage examples are described in detail in man sudoers(5).

In most cases, competent sudo configuration makes unsafe work as the superuser unnecessary.

All actions turn out to be feasible from the account of a user who is allowed to use sudo without restrictions.

It is possible to forbid and allow specific users or groups to run a particular set of programs, and also to allow running certain programs without having to enter one's password.

How to set up sudo

For a user to be able to run the sudo command successfully, they must be added to the sudo group (Debian, Ubuntu…) or wheel (CentOS, RedHat …)

You can check whether the user andrei is in sudo with the command

groups

If the result is something like this

andrei cdrom floppy audio dip video plugdev netdev bluetooth lpadmin scanner

That is, sudo is not in the list. You need to add it with the command

adduser andrei sudo

Or for .rpm

usermod -aG wheel $(whoami)

If everything went well, you can check that sudo is now in the list of groups

groups

andrei cdrom floppy sudo audio dip video plugdev netdev bluetooth lpadmin scanner

If something went wrong - read the notes.

Note 1

To add a user to the sudo group, you will most likely need to log in as root.

Starting from version 10 this is not so trivial, I advise you to read the previous paragraph bash: adduser: command not found

Note 2

If in one terminal you see that the user is already in the sudo group but in another terminal they cannot execute a sudo command

exit

And log in again.

Note 3

The description of who can use sudo and how is in the file /etc/sudoers

You do not need to edit it directly, you can go into /etc/sudoers.d or invoke the utility as root

visudo

And adjust something there at your own risk.

Allow a user not to enter a password when running sudo

Make sure the user is added to the sudo group (wheel for .rpm)

Run

sudo visudo

Add to the end of the file

username ALL=(ALL) NOPASSWD:ALL

Now the user username will be able to run all sudo commands without entering a password. This is very convenient for working with local virtual machines

The command sudo itself still has to be written

Examples

Allow users in the (English) group «admin» to run commands with the rights of any user (with a password prompt):

%admin ALL=(ALL) ALL

Allow the user «user1» to run commands with the rights of «user2» or «user3» without asking for a password:

user1 ALL=(user2, user3)NOPASSWD: ALL

Allow the user «backup» to run the command /usr/bin/rsync without a password prompt:

backup ALL=NOPASSWD: /usr/bin/rsync

How to add a Russian keyboard layout

Open the file /etc/default/keyboard with the command

sudo vi /etc/default/keyboard

Find the needed lines

And add ,ru to XKBLAYOUT="us" grp:ctrl_shift_toggle to XKBOPTIONS=""

The result is a piece like this

XKBLAYOUT="us,ru" XKBVARIANT="" XKBOPTIONS="grp:ctrl_shift_toggle"

Note 1

If editing files in the editor gives you trouble vi Read the article

«An example of using VI to change the language in Linux»

usermod: group 'sudo' does not exist

If you tried to add a user to the sudo group with usermod and got

usermod: group 'sudo' does not exist

Most likely you are using an .rpm distribution, for example CentOS

In .rpm the group is called wheel and not sudo

Run

usermod -aG wheel $(whoami)

Article author: Andrei Olegovich