Adding users to sudoers through shell script

bash script add user to sudoers
add user to sudoers centos
add script to sudoers file
raspbian add user to sudoers
add user to sudoers debian
add user to sudoers redhat
how to give sudo access to a user in linux
sudo in shell script without password prompt

Is it possible to add users to the sudoers file through a shell script? I've been looking around, still can't find anything.


You could simply echo (with elevated privileges, of course) directly to the /etc/sudoers file:

sudo -i
echo 'nickw444  ALL=(ALL:ALL) ALL' >> /etc/sudoers
#             ^^
#             tab

(note the tab character between the username and the first ALL)

Or, for a script:

#!/bin/bash
# Run me with superuser privileges
echo 'nickw444  ALL=(ALL:ALL) ALL' >> /etc/sudoers

Then save to somefile.sh, chmod a+rx it, and run sudo ./somefile.sh from a terminal window.

To add multiple users, change the script to this;

#!/bin/bash

while [[ -n $1 ]]; do
    echo "$1    ALL=(ALL:ALL) ALL" >> /etc/sudoers;
    shift # shift all parameters;
done

Then, run the script like this (assuming you saved it as addsudousers.sh):

sudo ./addsudousers.sh bob joe jeff

that is, space-separated.

To read the names from a file:

nickw444@laptop ~ $ sudo ./addsudousers.sh `cat listofusers.txt`

listofusers.txt should also be space-separated.

Edit: Jappie Kirk rightly points out that you can't directly call sudo echo ... >> /etc/sudoers because the >> redirection is handled by the shell, which has by that point dropped the superuser privileges. However, if you run a script that contains echo ... >> /etc/sudoers and the script itself has superuser privileges, everything should work just fine.

shell script to give users sudo access, option is to add the user to the sudo group specified in the sudoers file. By default, on Debian based distributions like Ubuntu and Linux Mint,  For something like this, I usually prefer to drop a file in /etc/sudoers.d (using puppet, for instance), which may not be available in your version of RHEL. But for your use case, you can also use something like tmux or clusterssh to connect to many different computers and perform the same commands (the practicality of that depends on how you connect to the servers).


No, a straight echo won't work, you have to run it in a subshell. Try this instead:

sudo sh -c "echo \"group ALL=(user) NOPASSWD: ALL\" >> /etc/sudoers"

How to Add User to Sudoers in Ubuntu, Addsudoers: A script to add users or groups into /etc/sudoers I really refer to add new groups, users, aliases in the /etc/sudoers file. visudo is addsudoers.sh Linux shell script to add a user with a password. Step 1 – Create an encrypted password. You need to create an encrypted password using Perl crypt() as follows: crypt ( $plain , $salt ) ## perl Step 2 – Shell script to add a user and password on Linux. Step 3 – Change existing Linux user’s


There is also the sudo group, and you could add users to it (for common configurations of /etc/sudoers)

adduser [username] sudo

Addsudoers: A script to add users or groups into /etc/sudoers, Learn how to Sudoers on Ubuntu the simple way. In Linux, Sudo allows privileged users to execute specific commands as a superuser. Login as root to your machine. The root user are the only one who has privilege to add new user. Once you logged-in, you may now try the following commands below: Create a new user. adduser [username] Add password to user. passwd [username] Grant root privileges to user Edit the visudo file by simply typing.


on RedHat Based Distributions use:

su - root

and enter your password, then :

echo 'YOURUSERNAME ALL=(ALL:ALL) ALL' >> /etc/sudoers

to add the user in sudoers file.

How To Add User to Sudoers / Add User to Sudo Group CentOS 7, The sudo command provides a mechanism for granting administrator privileges, ordinarily PostedMarch 28, 2016 4.1m views Linux BasicsUbuntuQuickstart with sudo access on Ubuntu, without having to modify your server's sudoers file. If your user is in the proper group and you entered the password correctly, the  Therefore, to run a shell script or program as root, you need to use sudo command. However, sudo only recognizes and runs commands that exist in directories specified in the secure_path in the /etc/sudoers, unless a command is present in the secure_path,


In order to grant to user sudo permission in shell script (Unix/Linux) use the usermod function:

sudo usermod -aG sudo <userName>

example:

sudo usermod -aG sudo johnDoe

For Verification: use the groups function ( which show the group membership ) and verify the sudo group us under the right user.

groups <userName>

example:

groups johnDoe
#!johnDoe: johnDoe sudo

Explanation from linux documentation:

The usermod command modifies the system account files to reflect the changes that are specified on the command line.

-a, --append

Add the user to the supplementary group(s). Use only with the -G option.

-G, --groups GROUP1[,GROUP2,...[,GROUPN]]]

A list of supplementary groups which the user is also a member of. Each group is ?> separated from the next by a comma, with no intervening whitespace. The groups are subject to the same restrictions as the group given with the -g option. If the user is currently a member of a group which is not listed, the user will be removed from the group. This behaviour can be changed via the -a option, which appends the user to the current supplementary group list.

How to Add User to Sudoers or Sudo Group on Ubuntu, How can I do that via the shell? Finally, can I delete the original user that was created upon initial installation of Ubuntu, or is this user somehow '  I understand the risks. I'm just trying to create a user in a shell script, install yay using that user, and then finally deleting that user. There's no wrong with that. Also I want that specific user to have some sudo privilleges, that's why. Thanks a lot for suggesting that forum to me. Was really helpful


How To Create a Sudo User on Ubuntu [Quickstart], How do I add a new user as sudoer file using the command line option on Ubuntu? In Linux (and Unix in general), there is a SuperUser named  In order to grant administrator privileges to non-root users temporarily by ad-hoc basis, Linux users can use the sudo command. To allow an user to have sudo access in Linux, the user account has to be added to sudoers file. Follow the guide below to configure sudo for an user account in Linux operating system such as CentOS, RedHat Enterprise


sudo, Many Linux commands require the user to be root in order to run. The sudo program checks the /etc/sudoers file and verifies that ruser is  Create a new user account with admin (sudo) access on Ubuntu or Debian Linux. Commands to add or create a sudo user (admin) on an Ubuntu or Debian Linux server: Open the terminal application; For remote Ubuntu/Debian server use the ssh command and log in as the root user using either su or sudo. Create a new user named marlena, run: adduser marlena


How to create a new sudo user on Ubuntu Linux server,