How to run sudo command without entering password in Linux

How to run sudo command without entering password in Linux

The sudo command allows a trusted user to run a program as another user, which by default is the root user. If you spend a lot of time on the command line, sudo is one of the commands you use all the time.

Normally, to grant sudo access, you add the user to the sudo group defined in the sudoers file. On Debian, Ubuntu and their derivatives, members of the sudo group are given sudo privileges. On RedHat based distributions like CentOS and Fedora, the name of the sudo group is wheel.

Each member of the group will be prompted for their password before running the sudo command. This adds an extra layer of security and is the preferred way to grant sudo privileges to users.

As shown below:

However, in some cases, such as running automated scripts, you may want to configure the sudoers file and allow certain users to run sudo commands without asking for a password.

Add User to Sudoers File

The sudoers file contains information that determines sudo permissions for users and groups.

You can configure user sudo access by modifying the sudoers file or adding configuration files to the /etc/sudoers.d directory. All files in this directory are included in the sudoers file.

Before making any changes, it is a good idea to back up your current files:

linuxidc@linuxidc:~/www.linuxidc.com$ sudo cp /etc/sudoers{,.backup_$(date +%Y%m%d)}

The date command will append the current date to the backup file name.

Use the visudo command to open the /etc/sudoers file:

sudo visudo

When making changes to the sudoers file always use visudo this command checks after editing and if there are syntax errors the changes will not be saved. If you open the file with a text editor, syntax errors will result in losing sudo access.

On most systems, the visudo command opens the /etc/sudoers file using the vim text editor. If you don't have experience with vim, you can use another text editor. For example, to change the editor to GNU nano, you would run:

sudo EDITOR=nano visudo

Scroll down to the end of the file and add the following line, which allows the user "linuxidc" to run any command with sudo without asking for a password at the beginning:

/etc/sudoers

linuxidc ALL=(ALL) NOPASSWD:ALL

As shown below:

Don’t forget to change “linuxidc” with the username you want to grant access to.

If you want to allow the user to run only specific commands without entering a password, specify the command after the NOPASSWD keyword.

For example, to allow only the mkdir and mv commands:

In /etc/sudoers file

linuxidc ALL=(ALL) NOPASSWD:/bin/mkdir,/bin/mv

When you are finished, save the file and exit the editor.

Using /etc/sudoers.d

Instead of editing the sudoers file, you can add new files to the /etc/sudoers.d directory with authorization rules. This approach will make management of sudo permissions more maintainable.

Open a text editor and create the file:

sudo nano /etc/sudoers.d/linuxidc

You can name the file whatever you want, but it's usually best to use your username as the filename.

/etc/sudoers.d/linuxidc

Add the same rules you added to the sudoers file:

linuxidc ALL=(ALL) NOPASSWD:ALL

Finally, save the file and close the editor.

Summarize

Running sudo without a password is useful when you have scripts that a non-root user needs to perform administrative tasks.

The above is the method that I introduced to you to run sudo command in Linux without entering a password. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • Linux exposes Sudo privilege escalation vulnerability, any user can also run root commands
  • Delegating Privileges in Linux Using Sudo
  • Summary of the Differences between sudo, su and su - Commands in Linux
  • Summary of ten tips for sudo command in Linux system
  • How to use sudo su to add root permissions to ordinary users in Linux
  • Detailed explanation of the detailed configuration of sudo and its configuration file /etc/sudoers under Linux
  • Detailed explanation of sudo command in Linux system
  • How to add sudo permissions to a user in Linux environment

<<:  Sharing the detailed process of setting up Mysql5.6 to allow external network access

>>:  An example of installing MySQL on Linux and configuring external network access

Recommend

How to set up automatic daily database backup in Linux

This article takes Centos7.6 system and Oracle11g...

Linux configuration SSH password-free login "ssh-keygen" basic usage

Table of contents 1 What is SSH 2 Configure SSH p...

The implementation of Youda's new petite-vue

Table of contents Preface Introduction Live Easy ...

How to use regular expression query in MySql

Regular expressions are often used to search and ...

ftp remotely connect to Linux via SSH

First install ssh in Linux, taking centos as an e...

A brief discussion on two methods to solve space-evenly compatibility issues

Since its launch in 2009, flex has been supported...

Specific use of Bootstrap5 breakpoints and containers

Table of contents 1. Bootstrap5 breakpoints 1.1 M...

40 web page designs with super large fonts

Today's web designs tend to display very larg...

Detailed explanation of the use of find_in_set() function in MySQL

First, let’s take an example: There is a type fie...

Detailed usage of kubernetes object Volume

Overview Volume is the abstraction and virtualiza...

How to use CSS to achieve two columns fixed in the middle and adaptive

1. Use absolute positioning and margin The princi...

Vue SPA first screen optimization solution

Table of contents Preface optimization SSR Import...

Linux remote control windows system program (three methods)

Sometimes we need to remotely run programs on the...

Nexus uses nginx proxy to support HTTPS protocol

background All company websites need to support t...