#!/bin/bash # create_user.sh # Meryll Larkin December 1, 2018 # updated October 1, 2019 # defined undefined group, -z on ifs, added : on egrep # Create login, add to primary, secondary group accounts # September 9, 2019 added error checking and password reminder # June 20, 2022 Various updates including UID selection MYSHELL="/bin/bash" HOMEBASE="/home" if [[ $EUID -ne 0 ]]; then echo "This script must be run as root" exit 1 fi function UID_GID_DUP_CHECK { dup_check=`egrep ":${uidno}:" /etc/passwd` if [ -z "$dup_check" ] ; then # so far so good, uid does not already exist in /etc/passwd dup_check=`egrep ":${uidno}:" /etc/group` if [ -z "$dup_check" ] ; then # so far so good, uid does not already exist in /etc/group echo else printf "\n gid ${uidno} found in /etc/group. Select a different uid." printf " $dup_check" printf "\n Quitting.\n\n" exit fi else printf "\n uid ${uidno} found in /etc/password. Select a different uid." printf " $dup_check" printf "\n Quitting.\n\n" exit fi } # Determine the actual user who is acting as root parent_id=`ps -ef | grep $0 | awk '{ print $3 }'| head -n1` # echo "parent id = $parent_id" originating_user=`ps -ef |grep $parent_id | grep -v root | awk '{print $1}'` # echo "originating_user = $originating_user" while [ -z $originating_user ] ; do # No originating user found, try parent pid parent_id=`ps -ef | grep $parent_id | awk '{ print $3 }'| head -n1` # echo "parent id = $parent_id" originating_user=`ps -ef |grep $parent_id | grep -v root | awk '{print $1}'` # echo "originating_user = $originating_user" done local_user=$originating_user # get the uid for the originating user: user1_uid=`grep $local_user /etc/passwd | awk -F: '{ print $3 }'` echo "$local_user has uid $user1_uid" length_uid=${#user1_uid} echo "length_uid = $length_uid" echo "Checking available UIDs starting with UID $user1_uid" # uids = echo "These UIDS are already claimed:" for i in `cat /etc/passwd | awk -F: '{ print $3 }'| sort -n`; do len=${#i}; if [ $len -eq $length_uid ]; then echo $i; fi; done # Take the first sequential UID available. end_loop=$(($user1_uid + 100)) echo "end_loop = $end_loop" avail1=0 avail2=0 avail3=0 i=$user1_uid while [ "$i" -le "$end_loop" ]; do in_passwd=`grep ":${i}:" /etc/passwd |wc -l` in_group=`grep ":${i}:" /etc/group |wc -l` if (( $in_passwd == 0 )) ; then if (( $in_group == 0 )) ; then if (( $avail1 == 0 )) ; then avail1=$i elif (( $avail2 == 0 )); then avail2=$i elif (( $avail3 == 0 )); then avail3=$i else break fi fi fi i=$(($i + 1)) done echo "These UIDs are available: $avail1 $avail2 $avail3" echo -n "Username (login) to add? " read username # make sure username does not already exist on system dup_check=`egrep "^${username}:" /etc/passwd` if [ -z "$dup_check" ] ; then # username is does not exist on this system. Ok to proceed. echo else printf "\n Found this in /etc/password. Already installed? Pick a different username?\n" echo " $dup_check" printf "\n Quitting.\n\n" exit fi username=`echo "$username" | tr '[:upper:]' '[:lower:]'` dup_check=`egrep -i "^${username}:" /etc/passwd` if [ -z "$dup_check" ] ; then # username is does not exist on this system. Ok to proceed. printf "\n Username $username does not already exist in /etc/password. Okay to proceed.\n\n" else printf "\n Found this in /etc/password:\n $dup_check\n Already installed? Pick a different username?\n" printf "\n Quitting.\n\n" exit fi uidno=$avail1 echo "In order to create user $username, with uid $uidno need additional info" echo -n "Is uid $uidno acceptable? [Y|n]: " read okay_uid ok_uid=`echo "$okay_uid" | tr '[:lower:]' '[:upper:]'` if [[ "$ok_uid" != "Y" ]] ; then echo "Please provide acceptable uid number: " read uidno UID_GID_DUP_CHECK fi echo -n "First Name? " read fname fname=`echo "$fname" | tr '[:upper:]' '[:lower:]'` fname="$(tr '[:lower:]' '[:upper:]' <<< ${fname:0:1})${fname:1}" echo -n "Last Name? " read lname lname=`echo "$lname" | tr '[:upper:]' '[:lower:]'` lname="$(tr '[:lower:]' '[:upper:]' <<< ${lname:0:1})${lname:1}" echo -n "Primary Group? (enter to create $username as primary group) " read groupone if [ -z "$groupone" ] ; then # create group same as username if no Primary Group has been selected echo "adding group $username with gid $uidno" groupadd -g $uidno $username groupone=$username else # make sure selected group exists Q=`egrep "^${groupone}" /etc/group` if [ -z "$Q" ] ; then # The group needs to exist before the user can be added to it printf "\n Group \'$groupone\' does not exist. Create it first!" printf "\n Quitting\n\n." exit fi fi echo -n "Secondary Groups? (space delimited) ? " read moregroups echo -n "Create home directory? [Y|n]: " read mkhome mkhome=`echo "$mkhome" | tr '[:lower:]' '[:upper:]'` if [[ "$mkhome" != "Y" ]] ; then mkhome="-m" else mkhome="-M" fi groups2=$(echo $moregroups | tr " " ",") useradd -c "$fname $lname" -u $uidno -g $groupone -d ${HOMEBASE}/${username} -s $MYSHELL $mkhome $username if [ -z "$groups2" ] ; then # do nothing echo "No additional groups to add" else usermod -a -G $groups2 $username fi printf "\n Done. REMEMBER TO SET A PASSWORD\n\n"