-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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~" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#!/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