Linux install sudo command

How to install sudo on Debian or Ubuntu Linux

The command sudo, we used before other program calls to execute them. The key purpose of it is to authorize the users to run the program on behalf of and with the rights of another user to perform tasks that are reserved for administrators.

When we run the command with sudo, it asks for the password of the current user before executing the program. This checks whether the user that entered the command is the group of authorized users defined in the /etc/sudoers file or not. The target user is root assumed by default.

Under Debian, in /etc/sudoers all members of the group are sudo and allowed by default to become the user root and thus to receive root rights. The user or users created during installation belong to the group sudo by default.

In GUI desktop environments such as GNOME or KDE, root rights are sometimes automatically requested for a certain program for certain tasks by giving a graphical dialogue box. For example, you are automatically asked for your password for a program installation or upgrading system graphically, so that APT receives root rights and can install the program. In many cases, sudo no longer needs to be called manually.

Easy way to understand what sudo is for-

What is sudo in Linux

Command to Install sudo on Debian Linux Minimal server

Well, sudo is already in the Debian system most of the time; however, in minimal Debian installation such as Debian on Docker, the sudo will be missing. Hence, to create any other user and use that to install some software without giving root access for all the commands, we have to install sudo manually using the command line. Here are the steps to follow.

This article assumes that you already have root user access because without that we cannot install “sudo” or any other packages.

1. Apt update cache

First, run the system update command to refresh the APT repositories cache. This is necessary because many times after installing a fresh minimal Debian or Ubuntu Linux, it won’t recognize any packages to install. It is because there is no list of packages in the cache, that the system can identify to install.

Читайте также:  Porting application to linux

2. Command to install sudo on Debian or Ubuntu server

You have the updated minimal Debian or Ubuntu server, now we can easily use the APT package manager command to install the “sudo” without the help of any additional repository as it is already available in the base repo of Debian.

command to install sudo debian

3. Create a new user

Once the sudo is installed, let’s create a new user to use later with the sudo command.

adduser your-new-user

Change “your-new-user” with whatever name you want to give to your user.

For example, here we area adding a user called – h2smedia, the command will be like this:

Set password when it asks.

Add new user

4. Add a new user to the sudo group in Debian

This is an important step otherwise you won’t be able to use your new user with sudo rights to install new programs.

usermod -aG sudo h2smedia

Again replace h2smedia with your user created above.

Add user to sudo group in Debian 11

5. Switch to new user

Now, let’s switch to the newly created user from root to run a non-root user but with sudo rights.

su your-new-user

Run sudo commands, to test it:

Enter the password you have created for your user.

Add user to sudo group in Debian 11

Now to log back again to the root user, you can simply type:

In this way, anybody who has the root user rights can install and add a new user with sudo rights. However, make sure you are using a strong password.

Other Articles:

2 thoughts on “How to install sudo on Debian or Ubuntu Linux”

I have Ubuntu 18.04 but cannot do the following install:
/home/fred/Downloads/hotspot shield)f=hotspotshield_1.0.7_amd64.deb /home/fred/Downloads/hotspot shield)sudo apt install ./$f
Reading package lists… Done
Building dependency tree
Reading state information… Done
Note, selecting ‘hotspotshield:amd64’ instead of ‘./hotspotshield_1.0.7_amd64.deb’
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation: The following packages have unmet dependencies:
hotspotshield:amd64 : Depends: libdbus-1-3:amd64 (>= 1.10) but it is not installable
Depends: libnl-3-200:amd64 (>= 3.2.0) but it is not installable
Depends: libnl-route-3-200:amd64 (>= 3.2.0) but it is not installable
Depends: network-manager:amd64 (>= 1.10) but it is not installable
E: Unable to correct problems, you have held broken packages. How can I fix this? Reply

Источник

10+ sudo command examples in Linux [Cheat Sheet]

sudo command in Linux stands for Super User DO. It allows you to execute a command as another user, including the superuser. sudo is an efficient way to access the root privileges and execute the command as the root user. With sudo, you can access the root privileges without knowing the root password or logging as the root user. The user needs to have the sudo privileges to run the sudo command.

How to install sudo

In most of the Linux distributions, sudo is pre-installed on the system. However, if you do not find the sudo package on the system, you can install it from the default package management repositories.

To install sudo on CentOS, Fedora, and RHEL

To install sudo on Ubuntu and Debian

Syntax for sudo command

The security policy determines whether or not a user has sudo privileges. The default security policy is sudoers configured in the file /etc/sudoers.

The general syntax for sudo command is as follows:

Читайте также:  Linux how to install kde

Some of the options available in sudo command are:

  • -V: display version information of sudo
  • -h: display help message and exit
  • -l: list user’s privileges or check a specific command
  • -k: reset the user’s timestamp
  • -u: run the command as a different user

Different examples to use sudo command

1. Use sudo to run command as a root user

By default, when no user is specified, the sudo runs a command as a root user. It is mostly used to run the command which needs root permission.

Sample Output:

When listing the root directory, the permission was denied because only the root user can list the root directory. But, after using the sudo command, we can list the root directory.

sudo command to run command as root user

Similarly, when you want to install, remove, or update the package in the system, you will need the root privilege. You can use sudo command to install the package without logging in as a root user.

2. Use sudo to run command as a different user

The -u or —user option allows you to run a command as the specified user name or user ID. So, you can run the command as a user other than the root.

Sample Output:

golinux@ubuntu-PC:~$ sudo -u deepak whoami deepak

You can also change the password of another user without logging in.

golinux@ubuntu-PC:~$ sudo passwd deepak New password: Retype new password: passwd: password updated successfully

3. List user privileges with sudo command

The -l or —list option is used to list user’s privileges on the output. You can use this option twice for a longer format.

Sample Output:

It shows the user golinux can run all the commands with sudo on the system.

deepak@ubuntu:~$ sudo -l [sudo] password for deepak: Matching Defaults entries for deepak on ubuntu: env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin User deepak may run the following commands on ubuntu: (ALL : ALL) ALL

Use -l twice for longer output:

deepak@ubuntu:~$ sudo -ll Matching Defaults entries for deepak on ubuntu: env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin User deepak may run the following commands on ubuntu: Sudoers entry: RunAsUsers: ALL RunAsGroups: ALL Commands: ALL

It also checks for a specific command in the system. Let’s check the command adduser and nmap using sudo -l .

As you can see, it does not run the command but only checks where it is located in the system.

golinux@ubuntu-PC:~$ sudo -l adduser /usr/sbin/adduser

4. Display privileges for another user with sudo command

You can use -U or —other-user option with -l option to display the user privileges of the given user.

Sample Output:

As you can see, the user deepak does not have sudo privileges but the root user can run all the commands.

sudo command to list user privileges

5. sudo command to add a user to the sudo group

When you add a user to the sudo group, that user will be able to run all commands with sudo. You can add a user to the sudo group with the following command on Ubuntu and Debian.

$ sudo usermod -aG sudo user

Sample Output:

sudo command to add user to sudo group

On RHEL, CentOS, and Fedora, you can use:

$ sudo usermod -aG wheel user

6. sudo command to add users to the sudoers file

The user and group sudo privileges are defined in the /etc/sudoers file. The user who is listed in the sudoers file has sudo privileges and can run sudo command. You can add a user in the sudoers file by using visudo command.

Читайте также:  Альт линукс настройка принтера

Sample Output:

You need to add [username] ALL=(ALL:ALL) ALL on the User privilege specification as shown in the image below.

sudo command to add a user to sudoers

7. Run command in the background with sudo

The -b or —background option can be used to run the given command in the background. When you use this option, you cannot use shell job control to manipulate the process.

8. Update sudoers files using sudo command

Normally visudo is used to add/remove/modify sudoers entry in /etc/sudoers but if you intend to add another file inside /etc/sudoers.d then you can edit files in the terminal using -e or —edit option with sudo command. This is the recommended way when we are updating sudoers entry for any user to avoid any syntax errors.

Sample Output:

You can only edit files on the directory which is non-writable. It opens a nano editor to edit a file.

$ sudo -e /etc/sudoers.d/01-sudo-rule

This will create a new sudo rule inside /etc/sudoers.d based on the content added in this file. Now if you didn’t followed proper syntax while adding sudo rules, then after svaing the file, you won’t be able to access the file again or you may not able to save the file itself. Here is a sample error when I intentionally gave a wrong syntax in my sudoers file

deepak@ubuntu:~$ sudo -e /etc/sudoers.d/01-sudo-rule >>> /etc/sudoers.d/01-sudo-ssh: syntax error near line 2 

9. sudo command to update the user’s cached credentials

The -v or —validate option tells sudo to update the user’s cached credentials. It updates user’s timestamp without running a command.

Sample Output:

It does not print any output but displays an error if the user does not have sudo privileges.

golinux@ubuntu-PC:~$ sudo -v [sudo] password for golinux:

10. sudo command to invalidate user’s cached credentials

The -k or —reset-timestamp option remove the user’s timestamp or cached credentials. So, when you run the sudo command next time, you will require a password. It allows a user to revoke sudo permissions from a .logout file.

Sample Output:

sudo command to reset user timestamp

11. sudo command to remove the user’s timestamp completely

The -K or —remove-timestamp is similar to -k option except that it removes the user’s timestamp or cached credentials completely. Not all security policies support credential caching.

sudo command vs su command

Both sudo and su command are used to access the privileges of other accounts, including the root. su command is used to switch the user account whereas sudo command is used to run the command as the root user or a different user. Generally, with su command, you will need the password of the target user. But, sudo asks for the password of the current user.

For more details on su command, please read 9 su command examples in Linux [Cheat Sheet]

Conclusion

In this tutorial, we discussed the usages and the most common examples of sudo command in Linux. We hope you have learned how to use sudo command and execute the commands as another user with sudo privileges. If you still have any confusion, do let us know in the comment section.

What’s Next

Further Reading

Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Источник

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