Last active
November 1, 2017 21:08
-
-
Save reduardo7/fb4a8227ab7338d2e2a69fa062f2decb to your computer and use it in GitHub Desktop.
Create multiple Unix users with permissions
This file contains 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
# Run as ROOT: $ sudo su | |
######### | |
# Utils # | |
######### | |
@log() { | |
echo "# $*" | |
} | |
@PTECH_SETUP_USER() { | |
########## | |
# Config # | |
########## | |
# Required | |
PTECH_SETUP_USER_NAME="$1" | |
# Optional | |
PTECH_SETUP_GROUP_ADMIN_NAME="$2" | |
# Optional | |
PTECH_SETUP_SSH_PUB="$3" | |
################# | |
# User creation # | |
################# | |
@log Create user ${PTECH_SETUP_USER_NAME} | |
useradd --create-home -s /bin/bash ${PTECH_SETUP_USER_NAME} | |
####### | |
# SSH # | |
####### | |
if [ ! -z "${PTECH_SETUP_SSH_PUB}" ]; then | |
@log Setup SSH... | |
mkdir /home/${PTECH_SETUP_USER_NAME}/.ssh | |
echo "${PTECH_SETUP_SSH_PUB}" >> /home/${PTECH_SETUP_USER_NAME}/.ssh/authorized_keys | |
chown -R ${PTECH_SETUP_USER_NAME}:${PTECH_SETUP_USER_NAME} /home/${PTECH_SETUP_USER_NAME}/.ssh | |
chmod 700 /home/${PTECH_SETUP_USER_NAME}/.ssh | |
chmod 640 /home/${PTECH_SETUP_USER_NAME}/.ssh/authorized_keys | |
fi | |
############### | |
# Admin Group # | |
############### | |
if [ ! -z "${PTECH_SETUP_GROUP_ADMIN_NAME}" ]; then | |
PTECH_SETUP_SUDO_LINE_KEY="%${PTECH_SETUP_GROUP_ADMIN_NAME}" | |
PTECH_SETUP_SUDO_LINE="${PTECH_SETUP_SUDO_LINE_KEY} ALL=(ALL:ALL) NOPASSWD: ALL" | |
PTECH_SETUP_SUDO_FILE="/etc/sudoers.d/${PTECH_SETUP_GROUP_ADMIN_NAME}" | |
@log Create ${PTECH_SETUP_GROUP_ADMIN_NAME} group... | |
groupadd ${PTECH_SETUP_GROUP_ADMIN_NAME} | |
@log Add ${PTECH_SETUP_USER_NAME} to group ${PTECH_SETUP_GROUP_ADMIN_NAME}... | |
usermod -aG ${PTECH_SETUP_GROUP_ADMIN_NAME} ${PTECH_SETUP_USER_NAME} | |
touch ${PTECH_SETUP_SUDO_FILE} | |
if grep -q "${PTECH_SETUP_SUDO_LINE_KEY}" ${PTECH_SETUP_SUDO_FILE} | |
then | |
@log ${PTECH_SETUP_SUDO_FILE} already configured | |
else | |
@log Add line "'${PTECH_SETUP_SUDO_LINE}'" to ${PTECH_SETUP_SUDO_FILE} | |
echo "${PTECH_SETUP_SUDO_LINE}" >> ${PTECH_SETUP_SUDO_FILE} | |
# WARNING! If next is actived, Copy & Paste the function call ONE BY ONE! | |
#visudo -f ${PTECH_SETUP_SUDO_FILE} | |
fi | |
fi | |
} | |
@PTECH_SETUP_USER \ | |
'user1' \ | |
'sysadmin' \ | |
'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABA...3gF0pnPgy4rEsJQZ [email protected]' | |
@PTECH_SETUP_USER \ | |
'user2' \ | |
'sysadmin' \ | |
'ssh-rsa AAAAB3NzaC1yc2EA...9ei+Nzc4oSwtHRb [email protected]' | |
@PTECH_SETUP_USER \ | |
'user3' \ | |
'sysadmin' \ | |
'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABA...PbsbyFaNyNaCgp7Wv [email protected]' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment