Since you want to allow multiple users to be able to sudo as spock
, I'd recommend adding User_Alias
in you sudoers file (/etc/sudoers
) for all users who can become spock
Please note, I'm using a dummy file sudoers.test
in place of my /etc/sudoers
for this example, it should work fine with /etc/sudoers
as well.
$ cat sudoers.test
User_Alias USERS_SPOCK = kirk,bob
USERS_SPOCK ALL=(spock) NOPASSWD: ALL
Once that is done you can use something like the add_users
bash script below to add users to this file.
$ cat add_users
#!/bin/env bash
#
# Usage:
# add_users User_Alias users [users]
#
# change it to /etc/sudoers
SUDOERS_FILE=sudoers.test
# read args to an array
IFS=', ' read -r -a arg_array <<< "$@"
# alias to add users to
alias_name=${arg_array[0]}
# array of users to add to the list
new_users=${arg_array[@]:1}
# Find the specified alias and the users associated with that alias
IFS=', ' read -r -a current_users <<< $(grep "User_Alias $alias_name" $SUDOERS_FILE | sed -e "s/User_Alias $alias_name = //g")
[ "${#current_users[@]}" = "0" ] && echo "ERR: No Users found for User_Alias: $alias_name" && exit 1
echo "== LOG.INFO Users currently in User_Alias $alias_name: ${current_users[@]} -- (${#current_users[@]} users)"
echo "== LOG.INFO New Users to be added to User_Alias $alias_name: ${new_users[@]} -- (${#new_users[@]} users)"
# All only users not currenltly there in sudoers file.
for user in ${new_users[@]}; do
if [[ ! " ${current_users[@]} " =~ " $user " ]]; then
current_users+=($user)
echo "== LOG.DEBUG Adding user '$user' to User_Alias $alias_name"
else
echo "== LOG.WARN Skipping user '$user': already part of User_Alias $alias_name"
fi
done
echo "== LOG.INFO After update Users in User_Alias $alias_name: ${current_users[@]} -- (${#current_users[@]} users)"
all_users=$(echo ${current_users[@]} | tr ' ' ',')
# Update the current sudoers file
sed -i "s/User_Alias $alias_name = .*$/User_Alias $alias_name = $all_users/g" $SUDOERS_FILE
[ "$?" = "0" ] && echo "== LOG.INFO Sudoers updated successfully." || echo "== LOG.ERR Some error occured while updating Sudoers."
echo "== LOG.INFO Verifying sudoers file post edit"
visudo -c $SUDOERS_FILE
$ ./add_users USERS_SPOCK alice andrew
== LOG.INFO Users currently in User_Alias USERS_SPOCK: kirk bob -- (2 users)
== LOG.INFO New Users to be added to User_Alias USERS_SPOCK: alice andrew -- (1 users)
== LOG.DEBUG Adding user 'alice' to User_Alias USERS_SPOCK
== LOG.DEBUG Adding user 'andrew' to User_Alias USERS_SPOCK
== LOG.INFO After update Users in User_Alias USERS_SPOCK: kirk bob alice andrew -- (4 users)
== LOG.INFO Sudoers updated successfully.
== LOG.INFO Verifying sudoers file post edit
sudoers.test: parsed OK
$
$ cat sudoers.test
User_Alias USERS_SPOCK = kirk,bob,alice,andrew
USERS_SPOCK ALL=(spock) NOPASSWD: ALL
$
After these changes are made in /etc/sudoers
as root
.
Any of the member of User Alias USERS_SPOCK
can run any command as spock
alice$ sudo -u spock <random command>
alice$ sudo -u spock <my awesome script>