Bash script gererate User

1 minute read

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

Web­mentions

This site supports Webmentions, a backlink-based alternative to comment forms.

Publish a response on your website and share the link here to send a webmention! You need to include the complete URL to be accepted.

This post does not have any approved Webmentions yet.