Skip to content

Instantly share code, notes, and snippets.

@davidlares
Last active January 20, 2021 00:22
Show Gist options
  • Save davidlares/0cd809e98926bbc01d97f2b507753db4 to your computer and use it in GitHub Desktop.
Save davidlares/0cd809e98926bbc01d97f2b507753db4 to your computer and use it in GitHub Desktop.
Adding local system Linux Users (useradd command)
#!/bin/bash
# this script creates a new user on the local system
# you will be prompted to enter the username (login), the person name and password
if [[ "${UID}" -ne 0 ]]
then
echo "Run it with sudo privileges or as root"
exit 1
fi
# ask for the user name
read -p "Enter the username to create: " USER_NAME
# ask for real name
read -p "Enter the name of the person: " FULL_NAME
# ask password
read -p "What's your password: " PASSWORD
# create a user
useradd -c "${FULL_NAME}" -m ${USER_NAME}
# check if the useradd command succeded
if [[ "${?}" -ne 0 ]]
then
echo "The account could not be created"
exit 1
fi
# set password programatically
echo $PASSWORD | passwd --stdin ${USER_NAME}
if [[ "${?}" -ne 0 ]]
then
echo "The assigned password for the account could not be set"
exit 1
fi
# force changing
passwd -e ${USER_NAME}
# displaying data
echo "================"
echo "Username: ${USERNAME}"
echo "Password: ${PASSWORD}"
echo "Hostname: ${HOSTNAME}"
#!/bin/bash
# this script creates a new user on the local system
# you will be prompted to enter the username and fullname as argument (optional)
if [[ "${UID}" -ne 0 ]]
then
echo "Run it with sudo privileges or as root"
exit 1
fi
# evaluating argument existance
if [[ "${#}" -lt 1 ]]
then
echo "create an account on the local system with the USER_NAME and FULL_NAME as parameters"
exit 1
fi
# filling data
USER_NAME="${1}"
# moving positional parameters
shift
FULL_NAME="${@}" # anything else written (full name with spaces)
# generate password
PASSWORD=$(date +%s%N | sha256sum | head -c48)
# create a user
useradd -c "${FULL_NAME}" -m ${USER_NAME}
# check if the useradd command succeded
if [[ "${?}" -ne 0 ]]
then
echo "The account could not be created"
exit 1
fi
# filled password programatic
echo ${PASSWORD} passwd --stdin ${USER_NAME}
if [[ "${?}" -ne 0 ]]
then
echo "The assigned password for the account could not be set"
exit 1
fi
# force changing
passwd -e ${USER_NAME}
# displaying data
echo "================"
echo "Username: ${USER_NAME}"
echo "Password: ${PASSWORD}"
echo "Hostname: ${HOSTNAME}"
exit 0
#!/bin/bash
# this script creates a new user on the local system
# you will be prompted to enter the username and fullname as argument (optional)
if [[ "${UID}" -ne 0 ]]
then
echo "Run it with sudo privileges or as root" 1>&2
exit 1
fi
# evaluating argument existance
if [[ "${#}" -lt 1 ]]
then
echo "Create an account on the local system with the USER_NAME and FULL_NAME as parameters" 1>&2
exit 1
fi
# filling data
USER_NAME="${1}"
# moving positional parameters
shift
FULL_NAME="${@}" # anything else written (full name with spaces)
# generate password
PASSWORD=$(date +%s%N | sha256sum | head -c48)
# create a user
useradd -c "${FULL_NAME}" -m ${USER_NAME} &> /dev/null
# check if the useradd command succeded
if [[ "${?}" -ne 0 ]]
then
echo "The account could not be created" 1>&2
exit 1
fi
# filled password programatic
echo ${PASSWORD} passwd --stdin ${USER_NAME} &> /dev/null
if [[ "${?}" -ne 0 ]]
then
echo "The assigned password for the account could not be set" 1>&2
exit 1
fi
# force changing
passwd -e ${USER_NAME} &> /dev/null
# displaying data
echo "================"
echo "Username: ${USER_NAME}"
echo "Password: ${PASSWORD}"
echo "Hostname: ${HOSTNAME}"
exit 0
@davidlares
Copy link
Author

For the second script, you will need to pass the $1 and the $2 arguments that will represent the "username" and the "comment" of the "useradd" user creation Linux command.

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