Skip to content

Instantly share code, notes, and snippets.

@designeng
Forked from teocci/add-new-user.sh
Last active January 30, 2024 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save designeng/7b3371279b7df29d1ba9ebf6f550b74e to your computer and use it in GitHub Desktop.
Save designeng/7b3371279b7df29d1ba9ebf6f550b74e to your computer and use it in GitHub Desktop.
A simple bash shell script to create a linux user and optionally make them a sudoer
#!/bin/bash
ROOT_UID=0 # Only users with $UID 0 have root privileges.
E_NOTROOT=87 # Non-root exit error.
# Run as root only (sudo counts)
if [ "$UID" -ne "$ROOT_UID" ]
then
echo "You need root priveledges to run this script"
exit $E_NOTROOT
fi
echo -n "### Enter new user name: "
read NEW_USER
adduser $NEW_USER
echo -n "### make new user a sudoer? (y/n) "
read YES
case ${YES} in
y* )
adduser $NEW_USER sudo
;;
* )
continue
;;
esac
su $NEW_USER
echo "Done."
exit 0
#!/bin/bash
echo "Start to initialize user for this system."
echo -n "1. Check executor's permission..."
if [ `whoami` == "root" ];then
echo "OK!"
else
echo "ERROR."
exit 1
fi
echo -e "2. New user name: \c"
read new_user
adduser $new_user
if [ $? -eq 0 ];then
echo "3. Create user [$new_user]...OK!"
else
echo "3. Create user [$new_user]...ERROR."
exit 1
fi
password=$(head -c 32 /dev/urandom | base64 | tr -d '+/=')
service_chars="!@#$%^&*"
insert_random_char() {
local str=$1
local char=$2
local position=$((RANDOM % ( ${#str} + 1 )))
echo "${str:0:position}${char}${str:position}"
}
for ((i = 0; i < ${#service_chars}; i++)); do
password=$(insert_random_char "$password" "${service_chars:i:1}")
done
echo $password | passwd $new_user --stdin
echo "4. Set [$new_user] password, COPY it: $password"
read -p "5. Paste [$new_user] public ssh_key here: "
ssh_key=$REPLY
echo "------------------------------------------------------"
echo "--------------- ssh key print begin ------------------"
echo "------------------------------------------------------"
echo "ssh_key: $ssh_key"
echo "------------------------------------------------------"
echo "---------------- ssh key print end -------------------"
echo "------------------------------------------------------"
su - $new_user <<END_USER
cd ~
mkdir .ssh
cd .ssh/
echo $ssh_key >> authorized_keys
chmod 600 authorized_keys
chmod 700 ~/.ssh
END_USER
echo -n "6. Does he needs the root permissions? (y/n) "
read root_permission
echo "$root_permission"
if [ $root_permission = "y" ];then
usermod -g wheel $new_user
echo "${new_user} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/${new_user}
chmod 440 /etc/sudoers.d/${new_user}
# echo "" >> /etc/sudoers
# echo "# user: $new_user config start" >> /etc/sudoers
# echo "$new_user ALL=(ALL) ALL" >> /etc/sudoers
# echo "$new_user ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
# echo "# user: $new_user config end" >> /etc/sudoers
echo "User [$new_user] already has root permission."
elif [ $root_permission = "n" ];then
echo "Fine. No need to add root permission."
else
echo "What?? I can't understand.."
exit 1
fi
echo "User $new_user created! Bye~"
@designeng
Copy link
Author

#!/bin/bash

ROOT_UID=0 # Only users with $UID 0 have root privileges.
E_NOTROOT=87 # Non-root exit error.

Run as root only (sudo counts)

if [ "$UID" -ne "$ROOT_UID" ]; then
echo "You need root privileges to run this script."
exit $E_NOTROOT
fi

echo -n "Enter new user name: "
read NEW_USER

Create user with home directory and shell

adduser --create-home --shell /bin/bash $NEW_USER

echo -n "Make new user a sudoer? (y/n): "
read YES
case ${YES} in
[Yy]*)
usermod -aG sudo $NEW_USER
;;
*)
echo "User was not added to sudo group."
;;
esac

echo "Done."

exit 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment