LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to give Users SUDO Permission from Bash Script (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-give-users-sudo-permission-from-bash-script-4175693939/)

imtiaza 04-19-2021 09:24 AM

How to give Users SUDO Permission from Bash Script
 
Dear Gurus,

I have found a link (Bash Script) through which we can create Users in Linux. Problem is all the Users created from this script does not have SUDO Permission (root Permissions). Please help, Which and Where I can add the switches/option so that when i execute below Script it create ALL USERS with SUDO permission.

Code:

#!/bin/bash
# NOTE: Be sure to run this script with sudo.

# Read user and password
while read iuser ipasswd; do

# Just print this for debugging.
printf "\tCreating user: %s with password: %s\n" $iuser $ipasswd

# Create the user with adduser (you can add whichever option you like).
useradd -m -s /bin/false $iuser

# Assign the password to the user.
# Password is passed via stdin, *twice* (for confirmation).
passwd $iuser <<< "$ipasswd"$'\n'"$ipasswd"
done < <(paste users.txt passwords.txt)


I just want to know that if i run the above mentioned script it create the users and password from the files (user.txt and passowrd.txt) but did not gave them SUDO permission. I want the script to give users SUDO permission.

Thanks
Malik Adeel Imtiaz

berndbausch 04-19-2021 10:00 AM

Write a line like this to /etc/sudoers.d/$iuser:
Code:

$iuser  ALL=(ALL:ALL) NOPASSWD: ALL

wpeckham 04-19-2021 10:49 AM

Or even better, add a line to give a particular GROUP such permission and make sure your script adds them to that group.

There are clues in the sudoers file you are editing when you use visudo. The man page might also be useful.

michaelk 04-19-2021 11:18 AM

I am not sure what the point is of creating users that can not login but need sudo or root privileges.

Emerson 04-19-2021 11:28 AM

I concur with michaelk, if this is a real word task then it definitely has wrong approach.

wpeckham 04-19-2021 02:34 PM

Good catch. These user cannot log in (shell is set to false) and sudo will only work from a logged in user session.

Perhaps you have not fully explained what you need, or have mis-stated the case. Please expand and explain so that we can understand what you are really trying to do.

Michael Uplawski 04-19-2021 11:37 PM

Quote:

Originally Posted by michaelk (Post 6242759)
I am not sure what the point is of creating users that can not login but need sudo or root privileges.

I am not sure I even want to know... If the case is explained in detail, the context and the motivation of the “project” on a larger scale should be presented.


All times are GMT -5. The time now is 05:42 PM.