In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how to set up and run specific commands in Linux without the need for a sudo password". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to set up and run specific commands in Linux without the need for a sudo password.
I have a Ubuntu system deployed on AWS, and there is a script in it. The original purpose of this script is to check whether a particular service is running at regular intervals (every 1 minute to be exact), and automatically restart the service if the service stops for some reason. But the problem is that I need sudo permission to turn on the service. As you know, when we run commands as sudo, we should provide a password, but I don't want to do that. What I actually want to do is run the service as sudo without providing a password. If you have ever experienced such a situation, then I know an easy way to do it. Today, in this short guide, I'll teach you how to run specific commands on a Unix-like operating system without a sudo password.
Let's take a look at the following example.
$sudo mkdir / ostechnix [sudo] password for sk:
As you can see in the screenshot above, when I create a directory called ostechnix in the root directory (/), I need to provide the sudo password. Whenever we try to execute a command with sudo privileges, we must enter a password. In my expectation, I don't want to provide the sudo password. The following is how I run a sudo command on my Linux machine without entering a password.
Run specific commands in Linux without a sudo password
For some reason, if you want to allow a user to run a specific command without providing a sudo password, you need to add this command to the sudoers file.
If I want a user named sk to execute mkdir without providing a sudo password, let's see how to do this.
Use the following command to edit the sudoers file:
$sudo visudo
Add the following command to the * * of this file.
Sk ALL=NOPASSWD:/bin/mkdir
Where sk is the user name. According to the contents of the above line, the user sk can execute the mkdir command from any terminal without entering the sudo password.
You can add additional commands (such as chmod) with comma-separated values, as shown below.
Sk ALL=NOPASSWD:/bin/mkdir,/bin/chmod
Save and close this file, and then log out (or restart) your system. Now log in as a normal user sk, and then try running these commands using sudo to see what happens.
$sudo mkdir / dir1
You see that? Even if I run the mkdir command with sudo privileges, it won't prompt me for a password. From now on, when the user sk runs mkdir, you don't have to enter the sudo password.
When running commands other than those added to the sudoers file, you will be prompted for your sudo password.
Let's use sudo to run another command.
$sudo apt update
You see that? This command will prompt me for the sudo password.
If you don't want this command to prompt you for your sudo password, edit the sudoers file:
$sudo visudo
Add the apt command to the sudoers file as follows:
Sk ALL=NOPASSWD:/bin/mkdir,/usr/bin/apt
Have you noticed that the path of the apt binary execution file in the above command is different from that of mkdir? Yes, you must provide a correct executable path. To find the executable path to any command, such as apt here, use the whichis command to view it as follows:
$whereis aptapt: / usr/bin/apt / usr/lib/apt / etc/apt / usr/share/man/man8/apt.8.gz
As you can see, the executable path of the apt command is / usr/bin/apt, so I added this path to the sudoers file.
As I mentioned earlier, you can add as many commands as you want separated by commas. Once you have finished adding, save and close your sudoers file, log out, and log back into your system.
Check now to see if you can run commands that start with sudo without using a password:
$sudo apt update
You see that? The apt command didn't ask me to enter my sudo password, even though I used sudo to run it.
Here is another example. If you want to run a specific service, such as apache2, add the following command to the sudoers file:
Sk ALL=NOPASSWD:/bin/mkdir,/usr/bin/apt,/bin/systemctl restart apache2
Now the user sk can run the sudo systemctl restart apache command without having to enter the sudo password.
Can I ask a special command to remind me to enter the sudo password again? Yes, of course! Just delete the added command, log out and log in again.
In addition to this method, you can also add a PASSWD: instruction in front of the command. Let's look at the following example:
Add or modify the following line in the sudoers file:
Sk ALL=NOPASSWD:/bin/mkdir,/bin/chmod,PASSWD:/usr/bin/apt
In this case, the user sk can run the mkdir and chmod commands without entering the sudo password. However, when he runs the apt command, he must provide the sudo password.
At this point, I believe you have a deeper understanding of "how to set up and run specific commands in Linux without the need for a sudo password". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.