add functions
distro_sudo_group change_user_password user_group_id add_recovery_user rename_usermaster
parent
3ea7ae75d1
commit
a1b3781c40
|
@ -1,5 +1,7 @@
|
|||
#!/bin/bash
|
||||
|
||||
|
||||
|
||||
user_id () {
|
||||
local id;local user
|
||||
echo -n $(id -u ${1:-$USER} 2> /dev/null)
|
||||
|
@ -13,15 +15,35 @@ is_user () {
|
|||
user_id $1
|
||||
}
|
||||
|
||||
change_user_password () {
|
||||
local user=${1:-$USER}
|
||||
[[ ! $(is_user $user) ]] && echo $user is not a system user && return 1
|
||||
module_load confirm
|
||||
confirm you are about the change the password for $user, continue? && sudo /bin/passwd $user
|
||||
}
|
||||
|
||||
#group name plus optional username or id
|
||||
is_user_group () {
|
||||
# echo id -nG ${2:-$USER}
|
||||
# id -nG ${2:-$USER} | grep -w "sudo"
|
||||
if id -nG ${2:-$USER} | grep -qw "$1"; then echo ${2:-$USER} in $1; fi
|
||||
if id -nG ${2:-$USER} | grep -qw "$1"; then ${2:-$USER} in echo $1; fi
|
||||
}
|
||||
|
||||
is_user_sudo () {
|
||||
is_user_group sudo $1
|
||||
is_user_group wheel $1
|
||||
}
|
||||
|
||||
distro_sudo_group () {
|
||||
if getent group | grep sudo &> /dev/null ; then
|
||||
echo sudo
|
||||
return 0
|
||||
elif getent group | grep wheel &> /dev/null; then
|
||||
echo wheel
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
user_group_add() {
|
||||
|
@ -30,6 +52,23 @@ user_group_add() {
|
|||
sudo usermod -a -G $1 $USER
|
||||
}
|
||||
|
||||
user_group_id_match () {
|
||||
module_load confirm
|
||||
local user=${1:-$USER}
|
||||
local gid=$(id -g $user)
|
||||
local uid=$(id -u $user)
|
||||
[[ $uid -eq $gid ]] && echo "ids match nothing to do" && return 0
|
||||
[[ ! $user == $(id -gn $user) ]] && echo user $user default group name is NOT the same name && return 1
|
||||
echo DANGER ZONE! changing current default group id for $user from $gid to $uid
|
||||
if confirm -s continue; then
|
||||
sudo /bin/find /home/$user -gid $gid -exec chgrp -v $uid '{}' \;
|
||||
sudo groupmod -g $uid $user
|
||||
sudo usermod -g $uid $user
|
||||
echo "only /home/$user directroy group was changed to $uid"
|
||||
echo "may have to change group from $gid to $uid elsewhere, e.g., sudo chown -R :$user <some dir>"
|
||||
fi
|
||||
}
|
||||
|
||||
# TODO change a user name
|
||||
# sudo usermod -d /home/joedoe -m johndoe
|
||||
# sudo groupmod -n johndoe student1
|
||||
|
@ -57,13 +96,26 @@ echo $name, $uid, $gid
|
|||
[[ $(getent group $name) ]] && echo group name $name already exists, aborting && return 3
|
||||
[[ $(getent passwd $uid) ]] && echo user id $uid already exists, aborting && return 2
|
||||
[[ $(getent passwd $name) ]] && echo user name $name already exists, aborting && return 2
|
||||
|
||||
|
||||
# todo use useradd and groupadd
|
||||
addgroup --gid $gid $name
|
||||
adduser --uid $uid --ingroup $name --gecos "" --disabled-password --no-create-home $name
|
||||
# cat /etc/group | grep $name && cat /etc/passwd | grep $name
|
||||
|
||||
}
|
||||
|
||||
add_recovery_user () {
|
||||
user=${1:-recovery}
|
||||
if [[ $(is_user 2000) ]] ; then
|
||||
echo "a recovery user with uid of 2000 already exists"
|
||||
else
|
||||
sudo useradd -u 2000 -N -s /bin/bash -G $(distro_sudo_group) $user
|
||||
[[ $? -gt 0 ]] && echo error creating recovery user: $user
|
||||
echo setting the password for recovery user: $user
|
||||
sudo passwd recovery
|
||||
fi
|
||||
}
|
||||
|
||||
function clone_user_ () {
|
||||
|
||||
module_load confirm
|
||||
|
@ -167,7 +219,7 @@ clone_user () {
|
|||
fi
|
||||
}
|
||||
|
||||
rename_user () {
|
||||
rename_user_ () {
|
||||
|
||||
module_load confirm
|
||||
local force; local name; local newname; local newhome
|
||||
|
@ -204,3 +256,16 @@ rename_user () {
|
|||
echo sudo groupmod -n $newname $name
|
||||
echo sudo usermod --d $newhome --m $name
|
||||
}
|
||||
|
||||
|
||||
rename_user () {
|
||||
if [[ $EUID != 0 ]]; then
|
||||
|
||||
rename=$(declare -f rename_user_)
|
||||
module_load confirm
|
||||
confirm=$(declare -f confirm)
|
||||
sudo bash -c "$confirm; $rename; rename_user_"
|
||||
else
|
||||
echo run as regular user with sudo privliges and it will elevate
|
||||
fi
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue