Last active
October 19, 2022 01:41
-
-
Save duruyao/540f34a6c560d8262948f89c9e78fbc5 to your computer and use it in GitHub Desktop.
Create new user in Linux and add it to sudo or other groups.
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
#!/usr/bin/env bash | |
## date: 2022/02/21 | |
## author: duruyao@gmail.com | |
## desc: the shortcut to create user | |
## usage: create-user [NEW_USERNAME] [NEW_USER_PASSWD] | |
set -euo pipefail | |
function warning_ln() { | |
# usage: warning_ln "warning message" | |
printf "\033[1;32;33m%s\n\033[m" "${1}" | |
} | |
if [ "${USER}" != "root" ]; then | |
warning_ln "Warning: The user 'root' may be required." | |
fi | |
NEW_USERNAME="" | |
if [ $# -gt 0 ]; then NEW_USERNAME="$1"; fi | |
if [ -z "${NEW_USERNAME}" ]; then | |
printf "[*] Input a new username: " | |
read -r NEW_USERNAME | |
fi | |
NEW_USER_PASSWD="" | |
if [ $# -gt 1 ]; then NEW_USER_PASSWD="$2"; fi | |
if [ -z "${NEW_USER_PASSWD}" ]; then | |
printf "[*] Input the new user's password: " | |
read -r NEW_USER_PASSWD | |
fi | |
useradd -ms /bin/bash "${NEW_USERNAME}" | |
echo "${NEW_USERNAME}":"${NEW_USER_PASSWD}" | chpasswd | |
if [ -n "$(getent group sudo)" ]; then | |
add_user_to_group="" | |
printf "[*] Would you want add '%s' to the group 'sudo'? [Y/n] " "${NEW_USERNAME}" | |
read -r add_user_to_group | |
if ! echo "${add_user_to_group}" | grep -q -E "n|no|N|No|NO"; then | |
usermod -aG sudo "${NEW_USERNAME}" | |
fi | |
fi | |
if [ -n "$(getent group docker)" ]; then | |
add_user_to_group="" | |
printf "[*] Would you want add '%s' to the group 'docker'? [Y/n] " "${NEW_USERNAME}" | |
read -r add_user_to_group | |
if ! echo "${add_user_to_group}" | grep -q -E "n|no|N|No|NO"; then | |
usermod -aG docker "${NEW_USERNAME}" | |
if [ -n "$(command -v docker-check.sh)" ]; then | |
line="alias docker=\"docker-check.sh\"" | |
echo -e "\n${line}\n" >>"$(eval echo ~"${NEW_USERNAME}")"/.bashrc | |
fi | |
fi | |
fi | |
if [ -n "$(command -v samba)" ] && [ -f "/etc/samba/smb.conf" ]; then | |
add_user_to_samba="" | |
printf "[*] Would you want add '%s' to the 'samba'? [Y/n] " "${NEW_USERNAME}" | |
read -r add_user_to_samba | |
if ! echo "${add_user_to_samba}" | grep -q -E "n|no|N|No|NO"; then | |
printf "%s\n%s" "${NEW_USER_PASSWD}" "${NEW_USER_PASSWD}" | smbpasswd -a "${NEW_USERNAME}" | |
if ! grep -q -E "${NEW_USERNAME}" /etc/samba/smb.conf; then | |
cp /etc/samba/smb.conf /etc/samba/smb.conf.bak | |
sed -i "s/valid users =/valid users = ${NEW_USERNAME},/g" /etc/samba/smb.conf | |
sed -i "s/valid users = ${NEW_USERNAME}, \%S/valid users = \%S/g" /etc/samba/smb.conf | |
else | |
warning_ln "Warning: The user '${NEW_USERNAME}' exists in '/etc/samba/smb.conf'" | |
fi | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment