Please Log in as Root Useer and Run Script Again

The sudo command is the most powerful command in Linux. Information technology provides a mechanism for granting administrator privileges, using ambassador privileges a normal user can do whatever he wants with the file arrangement of the installed Linux system. Using sudo we tin modify our system.

Method  1: Calculation to Root Grouping using usermod

usermod command in Linux is used to alter the user account. Using usermod command we can assign groups, permissions to a specific user. In that location are mainly 7 groups in the Linux file arrangement, 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 post-obit command in the terminal.

usermod -K root user

Here, the –Chiliad flag stands for Group and the root is the group for new user1.

To add together an existing user to the root group, follow the following command:

usermod -g 0 -o user

Here, the -thousand flag stands for Group id, and 0 represents the root grouping id, and the -o flag is for the existing user.

Method  2: Calculation to Root Group using useradd Control

useradd command can be used to create a new user or update default new user information. It is a low-level utility for adding users. To add a user to the grouping using the useradd command, simply run the following command in your last.

useradd -m -G root user

Hither, the –m flag is used to create the user'due south dwelling house directory if it does non exist, and the -Chiliad flag represents the grouping.

Method  3: Editing /etc/passwd file

Open up the passwd file using any text editor, and alter the group user id to 0which represents root permission.

Run the following command in the concluding :

nano /etc/passwd

And then change the post-obit permission for the user whom y'all want to give root admission to.

Before giving root permissions:

root:x:0:0:root:/root:/bin/bash user:128:128:user

After giving root permissions

root:ten:0:0:root:/root:/bin/bash user:0:0:user

Here, if you lot encounter clearly we accept modified line ii that has the user whom we want to give root admission, previously it has a value of 128 which is not a grouping ID for root. So we modified that line and replace the value with the 0, which represents the root group. After that, save the file and reboot your system.

Method 4: Setting as Sudo User

To add a user to sudo user, we tin modify the sudoers file located at /etc/sudoers. Open 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) ALL

Hither, ALL represents that we are giving all(full permissions) to the user i.e. the user tin can run whatsoever control and the user simply has to authenticate.

  • The offset ALL is all hosts. i.due east. if you take shared this sudoers file to many computers
  • The second ALL is the user as you are running the command
  • The third ALL is that user tin 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 vanquish script, we tin can use the sudo bash command with the shebang. This volition run the trounce script every bit a root user.

Case:

#!/usr/bin/sudo fustigate  ....  The rest of the shell script goes hither  ....

Shell Script to brand directory using root privileges:

#!/usr/bin/sudo fustigate echo "Enter a directory name" read newdir `mkdir $newdir`

Save the higher up script every bit geeks.sh, to run the script blazon the following control in the terminal:

sh geeks.sh

Output:

Created using root

Method six: Using an interactive dialog box

Nosotros can utilise a tool called whiptail to create an interactive dialog box. To install this tool, run the following control in your final:

sudo apt install whiptail

Script:

#!/usr/bin/bash  # saving user's name in me variable me="$(whoami)"  # Checking that the script is running as root. # entering in if case if [ "$(id -nu)" != "root" ]; and so      # resetting cached credentials     sudo --reset-timestamp      # creating a dialog box to enter user password      pswd=$(whiptail --title "GeeksforGeeks Authentication Script" \     --passwordbox "To run this script, authoritative privilege is \     required. \north[sudo] Password for user $me:" 14 52 3>&2 2>&1 1>&3-)          # executing the script with the password entered by user     exec sudo --stdin --prompt '' "$0" "$@" <<< "$pswd"      # if countersign is wrong it will render the condition code ane     exit 1  # exiting from if condition fi  # Here, nosotros volition do the stuff that only sudo user can do   # creating a folder under the user's directory path="/home/amninder/new_folder_Geeks"  mkdir $path  repeat "Binder Created!!"

Output:

using dialog box

In this script firstly nosotros have created an interactive dialog prompt box for the user to enter the sudo password, and 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 control with the if statement we're checking if the running script is using the root permission, if non nosotros are entering the if argument. Using sudo –reset-timestamp will articulate the previously saved credentials and ask for the root password again. And so we have used the whiptail command to add together a championship, a password box in the interactive dialog box. Here 14 and 52 are the height and width of the dialog box respectively.

"3>&ii 2>&1 1>&three-", nosotros are swapping stdout and stderr.

Hither,

0 – stdin

1 – stdout

2 – stderr

3 – stdout

 The three>&two in the script will create a new file descriptor and redirect information technology to 2 which is stderr. Now 2>&one will redirect the file descriptor 2 to stdout and 1>&3 will redirect file descriptor one to three which is stdout. And salve the countersign into pswd variable.

If the user enters the wrong countersign 3 times, nosotros are exiting the script and displaying leave code 1. And after finishing the if block, we can practise the stuff that needs root permission. In this script, nosotros are creating a folder afterward getting root permission.

juhlhicas1978.blogspot.com

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

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel