Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # This script creates a new user on the local system.
- # You will be prompt to enter the username, the person name, and a password
- # The username, password and host for the account will be displayed.
- # Make sure the script is being executed with superuser privileges.
- if [[ "${UID}" -ne 0 ]]
- then
- echo 'Please run with sudo or as root.'
- exit 1
- fi
- # Get the username (login).
- read -p 'Enter the username to create: ' USER_NAME
- # Get the real name (contents for description field).
- read -p 'Enter the name of the person or application that will be using this account: ' COMMENT
- # Get the password
- read -p 'Enter the password to user for the account: ' PASSWORD
- # Create the user with passwordff
- useradd -c "${COMMENT}" -m "${USER_NAME}"
- # Check to see if the useradd command succeeded.
- if [[ "${?}" -ne 0 ]]
- then
- echo 'The account could not be created'
- exit 1
- fi
- # Set the password.
- echo ${PASSWORD} | passwd --stdin ${USER_NAME}
- if [[ "${?}" -ne 0 ]]
- then
- echo 'The password for the account could not be set'
- exit 1
- fi
- # Force password change on first login.
- passwd -e ${USER_NAME}
- if [[ "${?}" -ne 0 ]]
- then
- echo "The password could not be expired"
- exit 1
- fi
- # Display the username, password, and the host where the user was created.
- echo
- echo 'username:'
- echo "${USER_NAME}"
- echo
- echo 'password: '
- echo "${PASSWORD}"
- echo
- echo 'host: '
- echo "${HOSTNAME}"
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement