Skip to content

Instantly share code, notes, and snippets.

@jacksonporter
Forked from zuckercode/gist:3720747
Last active March 21, 2020 00:57
Show Gist options
  • Save jacksonporter/34e59e05c1df7b7bfc7e29ae9fef8436 to your computer and use it in GitHub Desktop.
Save jacksonporter/34e59e05c1df7b7bfc7e29ae9fef8436 to your computer and use it in GitHub Desktop.
add new user with zsh as default shell, setup of basic SSH keys
#!/usr/bin/env bash
# Re-written by Jackson Porter
# Forked from: https://gist.github.com/zuckercode/3720747
# SSH Key generation/new user reference: https://www.digitalocean.com/community/questions/ubuntu-16-04-creating-new-user-and-adding-ssh-keys
# Color Code obtained from: https://gist.github.com/jacksonporter/73f8e9343e61e280cb178aa6ef4ec64e
set -e
eval "$(curl -fsSL https://gist.github.com/jacksonporter/791145218e977ef9165abd9fdf8fbfd4/raw)" # obtains my common bash utilies script and "sources" it
check_if_root
read -p "please provide a FIRST name for the new user: " FIRST_NAME
if [ "${FIRST_NAME}" == "" ]; then
print_warning "You did not enter a first name, so no user will be added."
exit 1
fi
read -p "please provide a LAST name (surname) for the new user: " LAST_NAME
if [ "${LAST_NAME}" == "" ]; then
print_warning "You did not enter a last name, so no user will be added."
exit 1
fi
SUGGESTED_USERNAME=$(printf "${FIRST_NAME}${LAST_NAME}" | awk '{print tolower($0)}')
read -p "Enter a username [hit enter to use: ${SUGGESTED_USERNAME}]: " NAME
if [ "${NAME}" == "" ]; then
print_warning "You did not enter a user name, ${GREEN_COLOR}${SUGGESTED_USERNAME}${YELLOW_COLOR} will be used"
NAME="${SUGGESTED_USERNAME}"
fi
USER_HOME_DIR="/home/${NAME}"
print_warning "\nAdding user: ${NAME}"
adduser $NAME --disabled-password --shell /bin/zsh --gecos "${FIRST_NAME} ${LAST_NAME},,,,"
chown -R ${NAME}:${NAME} ${USER_HOME_DIR}
print_success "Added user ${NAME}\n"
read -p "Make ${NAME} a sudo user (y/N): " SUDOER
if [ "$SUDOER" == "Y" ] || [ "$SUDOER" == "y" ] || [ "$SUDOER" == "yes" ] || [ "$SUDOER" == "YES" ]; then
usermod -aG sudo ${NAME}
printf "${NAME}\tALL=(ALL) NOPASSWD:ALL\n" >> /etc/sudoers
print_success "${NAME} is a sudo (super) user and was given sudo access without a password"
fi
cd ${USER_HOME_DIR}
print_warning "\nPlacing oh-my-zsh installation script in ${NAME}'s home directory"
FILE_NAME="install-oh-my-zsh.sh"
curl -fsSL https://gist.github.com/jacksonporter/d1b50a34d2252307f6c34379ec78b5f6/raw > "${USER_HOME_DIR}/${FILE_NAME}"
chown jacksonporter ${FILE_NAME}
chmod 500 ${FILE_NAME}
print_warning "Will run the ssh setup as user: ${NAME}"
sudo -u ${NAME} bash -c "$(curl -fsSL https://gist.github.com/jacksonporter/0a394e039eb0f7c63890a3a217f9c9e6/raw)"
print_success "Successfully ran SSH step for user ${NAME}"
print_success "\nFinished process \u263A"
@jacksonporter
Copy link
Author

jacksonporter commented Mar 20, 2020

How to run easily:

curl -fsSL https://gist.github.com/jacksonporter/34e59e05c1df7b7bfc7e29ae9fef8436/raw > "./add_new_user_zsh_ssh.sh" && sudo bash "./add_new_user_zsh_ssh.sh" && rm ./add_new_user_zsh_ssh.sh

@jacksonporter
Copy link
Author

jacksonporter commented Mar 21, 2020

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