Bash script gererate User
Intro
With this script I setup new users on my system (tested only with arch).
It will guide you trough each step.
Script
1#!/bin/sh
2# by matt | MIT | 2022
3set -Eeuo pipefail
4if [ "$EUID" -ne 0 ]
5then
6 echo "You need to run this script as root/sudo!"
7 exit
8fi
9echo "Generates Username and .ssh Directory"
10read -p "Enter new Username : " NEW_USER
11
12if [[ -d /home/$NEW_USER ]]
13then
14 echo "User Directory exists"
15else
16 useradd $NEW_USER
17 NEW_GROUP="$(id -nG $NEW_USER | awk 'NF{ print $NF }')"
18 mkdir /home/$NEW_USER
19fi
20
21if [[ -d /home/$NEW_USER/.ssh ]]
22then
23 echo ".ssh Directory exists"
24 echo " No change needed, please check your authorized keys"
25 exit
26else
27 mkdir /home/$NEW_USER/.ssh
28 touch /home/$NEW_USER/.ssh/authorized_keys
29 chown -R $NEW_USER:$NEW_GROUP /home/$NEW_USER
30 chmod 0700 /home/$NEW_USER/.ssh/
31 chmod 0640 /home/$NEW_USER/.ssh/authorized_keys
32 echo "Username and .ssh Directory created"
33 echo "If you want a Password type: passwd $NEW_USER otherwise set keyfile"
34fi