Skip to content

Instantly share code, notes, and snippets.

@lyashevska
Created August 24, 2018 11:32
Show Gist options
  • Save lyashevska/bd9c5b084e8833b50aaac93cf678778b to your computer and use it in GitHub Desktop.
Save lyashevska/bd9c5b084e8833b50aaac93cf678778b to your computer and use it in GitHub Desktop.
add local user
#! /bin/bash
# this script will create new user accounts
# enforce execution with a root priviledges,
# return status 1 if not root
if [[ "${UID}" -eq 0 ]]
then
# enter the username (login),
read -p 'Enter the username to create: ' USER_NAME
# the name for the person who will be using account
read -p 'Enter the real name: ' COMMENT
# get initial password
read -p 'Enter the password to use for the account: ' PASSWORD
# create a new user with a input provided by the user
useradd -c "${COMMENT}" -m ${USER_NAME}
# Check to see if the useradd command succeeded
if [[ "${?}" -ne 0 ]]
then
echo 'The account was not created'
exit 1
fi
# Set the password for the user
echo ${PASSWORD} | passwd --stdin ${USER_NAME}
# Check to see if the passwd command succeeded
if [[ "${?}" -ne 0 ]]
then
echo 'The account was not created'
exit 1
fi
# Force password change on first login
passwd -e ${USER_NAME}
# display the user name, password, the host where the account was created
echo "User name: $USER_NAME"
echo "Password: $PASSWORD"
HOSTNAME=$(uname -n)
echo "Host: $HOSTNAME"
else
echo "The sript requires root priviledges"
exit 1
fi
@vpereira
Copy link

vpereira commented Aug 24, 2018

great!
would be better however to do like

#!/bin/bash
if [[ $EUID -ne 0 ]]; then
   echo "The script requires root priviledges" 
   exit 1
fi

... then the rest of code 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment