Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of running sudo command without entering password in Linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

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

Typically, to grant sudo access, add the user to the sudo group defined in the sudoers file. On Debian,Ubuntu and its derivatives, members of the sudo group are granted sudo permissions on RedHat-based distributions such as CentOS and Fedora, and the name of the sudo group is wheel.

Before running the sudo command, each member of the group is prompted for a password. This adds an additional layer of security and is the preferred way to grant sudo permissions to users.

As shown below:

However, in some cases, such as running automatic scripts, you may need to configure the sudoers file and allow some users to run sudo commands without requiring a password.

Add users to the Sudoers file

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

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

Before making any changes, it is a good idea to back up the current file:

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

The date command appends the current date to the backup file name.

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

Sudo visudo

Always use the visudo command to check edits when making changes to the sudoers file, and if there are syntax errors, the changes are not saved. If you open a file using a text editor, syntax errors will result in the loss of sudo access.

On most systems, the visudo command opens the / etc/sudoers file using a vim text editor. If you have no experience with vim, you can use another text editor. For example, to change the editor to GNU nano, you will 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 commands using sudo without starting to ask for a password:

/ etc/sudoers

Linuxidc ALL= (ALL) NOPASSWD:ALL

As shown below:

Don't forget to change "linuxidc" with the user name you want to grant access to.

If you want to allow users 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 the / etc/sudoers file

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

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

Use / etc/sudoers.d

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

Open a text editor and create a file:

Sudo nano / etc/sudoers.d/linuxidc

You can name the file as needed, but it is usually best to use the user name as the file name.

/ etc/sudoers.d/linuxidc

Add the same rules as you added to the sudoers file:

Linuxidc ALL= (ALL) NOPASSWD:ALL

Finally, save the file and close the editor.

Summary

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

The above is the method of running the sudo command without entering a password in Linux introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to the website!

If you think this article is helpful to you, you are welcome to reprint it, please indicate the source, thank you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report