Please Log in as Root Useer and Run Script Again
The sudo command is the most powerful command in Linux. It provides a mechanism for granting ambassador privileges, using administrator privileges a normal user can practice any he wants with the file system of the installed Linux system. Using sudo we can modify our system.
Method 1: Adding to Root Group using usermod
usermod command in Linux is used to modify the user business relationship. Using usermod control nosotros tin can assign groups, permissions to a specific user. In that location are mainly vii groups in the Linux file system, these are as follows:
- root
- bin
- daemon
- sys
- adm
- deejay
- wheel
To add together a user to root using usermod, we have to run the following control in the terminal.
usermod -G root user
Here, the –K flag stands for Group and the root is the grouping for new user1.
To add an existing user to the root group, follow the following control:
usermod -g 0 -o user
Hither, the -g flag stands for Group id, and 0 represents the root group id, and the -o flag is for the existing user.
Method 2: Adding to Root Group using useradd Command
useradd command can be used to create a new user or update default new user information. It is a depression-level utility for adding users. To add together a user to the group using the useradd command, simply run the following command in your terminal.
useradd -one thousand -Yard root user
Here, the –thousand flag is used to create the user'south dwelling house directory if it does not exist, and the -G flag represents the grouping.
Method 3: Editing /etc/passwd file
Open up the passwd file using any text editor, and change the grouping user id to 0which represents root permission.
Run the following command in the concluding :
nano /etc/passwd
And so modify the following permission for the user whom you desire to give root access to.
Before giving root permissions:
root:x:0:0:root:/root:/bin/bash user:128:128:u/pre>Afterward giving root permissions
root:10:0:0:root:/root:/bin/fustigate user:0:0:userHere, if you see clearly we take modified line two that has the user whom we want to give root admission, previously it has a value of 128 which is non a group ID for root. So nosotros modified that line and supplant the value with the 0, which represents the root group. Afterwards that, relieve the file and reboot your system.
Method 4: Setting as Sudo User
To add a user to sudo user, we can change the sudoers file located at /etc/sudoers. Open up upwardly the sudoers file using any text editor and add the following line at the end of the file to add a user to the sudo user.
user ALL=(ALL) ALLHere, ALL represents that we are giving all(full permissions) to the user i.e. the user tin can run any command and the user just has to cosign.
- The first ALL is all hosts. i.e. if you lot accept shared this sudoers file to many computers
- The second ALL is the user as y'all are running the command
- The third ALL is that user can run the command.
Method v: To give root privileges to a user using a script
To give root privileges to a user while executing a shell script, we tin use the sudo bash command with the shebang. This volition run the shell script as a root user.
Instance:
#!/usr/bin/sudo bash .... The rest of the beat out script goes here ....
Shell Script to brand directory using root privileges:
#!/usr/bin/sudo bash echo "Enter a directory name" read newdir `mkdir $newdir`
Save the in a higher place script every bit geeks.sh, to run the script type the following control in the terminal:
sh geeks.sh
Output:
Created using root
Method 6: Using an interactive dialog box
We can employ a tool called whiptail to create an interactive dialog box. To install this tool, run the following control in your terminal:
sudo apt install tail
Script:
#!/usr/bin/bash # saving user's proper noun in me variable me="$(whoami)" # Checking that the script is running as root. # entering in if case if [ "$(id -nu)" != "root" ]; then # resetting cached credentials sudo --reset-timestamp # creating a dialog box to enter user password pswd=$(whiptail --title "GeeksforGeeks Hallmark Script" \ --passwordbox "To run this script, administrative privilege is \ required. \n[sudo] Countersign for user $me:" 14 52 3>&2 two>&1 1>&3-) # executing the script with the password entered by user exec sudo --stdin --prompt '' "$0" "$@" <<< "$pswd" # if countersign is wrong information technology will return the status code one exit 1 # exiting from if condition fi # Hither, we volition practise the stuff that merely sudo user can practice # creating a folder under the user'southward directory path="/home/amninder/new_folder_Geeks" mkdir $path repeat "Binder Created!!"
Output:
using dialog box
In this script firstly nosotros take created an interactive dialog prompt box for the user to enter the sudo password, then running the script with sudo permissions. Firstly, we are saving the user's proper noun in me variable for displaying the name in the interactive dialog box. Then using the id command with the if statement we're checking if the running script is using the root permission, if not nosotros are entering the if statement. Using sudo –reset-timestamp will clear the previously saved credentials and ask for the root password again. Then we have used the whiptail control to add a championship, a password box in the interactive dialog box. Here fourteen and 52 are the elevation and width of the dialog box respectively.
"3>&2 2>&1 1>&3-", we are swapping stdout and stderr.
Here,
0 – stdin
1 – stdout
2 – stderr
3 – stdout
The three>&2 in the script volition create a new file descriptor and redirect it to 2 which is stderr. Now two>&ane will redirect the file descriptor 2 to stdout and 1>&3 will redirect file descriptor ane to three which is stdout. And save the countersign into pswd variable.
If the user enters the incorrect password 3 times, we are exiting the script and displaying leave code i. And later finishing the if block, we can do the stuff that needs root permission. In this script, we are creating a binder later getting root permission.
Source: https://www.geeksforgeeks.org/shell-script-to-give-root-privileges-to-a-user/
0 Response to "Please Log in as Root Useer and Run Script Again"
Post a Comment