Created
September 12, 2023 07:58
-
-
Save t0rik/83c1f805050ea2b842fb464bf3dd7371 to your computer and use it in GitHub Desktop.
Add ssh user to system and generate RSA keypair
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
#!/bin/bash | |
# -------------------- | |
# arguments definition | |
# -------------------- | |
V_USER=$1 | |
V_PASSWORD=$(openssl rand -base64 12) | |
# ------------- | |
# verifications | |
# ------------- | |
if [ -z "$V_USER" ]; then | |
echo "Not enough parameters." | |
echo -e "Usage: " | |
echo -e " \t ${0##*/} username" | |
exit 1 | |
fi | |
check_sysuser=$(cut -f 1 -d : /etc/passwd | grep "^$V_USER$" ) | |
if [ ! -z "$check_sysuser" ]; then | |
echo "user $V_USER already exists" | |
exit 1 | |
fi | |
# -------- | |
# actions | |
# -------- | |
/usr/sbin/useradd -p $(openssl passwd ${V_PASSWORD}) ${V_USER} | |
echo "${V_USER} ALL=(ALL) NOPASSWD:ALL" | tee -a /etc/sudoers | |
mkdir /home/${V_USER} | |
mkdir /home/${V_USER}/.ssh | |
chown -R ${V_USER}:${V_USER} /home/${V_USER} | |
chsh -s /bin/bash "${V_USER}" | |
su - "${V_USER}" -c "ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa" <<<y | |
echo -e "------------------------------" | |
echo "SSH User: ${V_USER} " | |
echo "SSH Password: ${V_PASSWORD} " |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment