How to give Users SUDO Permission from Bash Script

imtiaza

New Member
Joined
Apr 19, 2021
Messages
1
Reaction score
0
Credits
15
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.

Bash:
#!/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
 


I want the script to give users SUDO permission.
You need to add
Code:
usermod -aG sudo $iuser
as to where exactly I'm not sure, maybe someone else here can tell you that, however, line #7 reads # Create the user with adduser (you can add whichever option you like). So I think you could try
Code:
useradd -m -s -aG sudo /bin/false $iuser
But I'm not really sure whether that will do what you want, I suggest you try in a VM first to avoid unwanted results.
 
You need to add
Code:
usermod -aG sudo $iuser
Or the wheel group depending which distribution you use and you will probably have to check if the sudo configuration is enabled because not all distributions have it enabled by default.
Code:
usermod -aG wheel $iuser
As for this line.
Code:
useradd -m -s /bin/false $iuser
That gives the user a login shell of false so they won't be able to login, if you want the user to be able to login you will have to change that to bash.
Code:
useradd -m -s /bin/bash $iuser
Or you can actually leave out the -s since most distributions default to bash, so it would then look like this.
Code:
useradd -m $iuser
 
Last edited:

Members online


Top