Advertisement
Guest User

Untitled

a guest
Oct 11th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # This script creates a new user on the local system.
  4. # You will be prompt to enter the username, the person name, and a password
  5. # The username, password and host for the account will be displayed.
  6.  
  7. # Make sure the script is being executed with superuser privileges.
  8. if [[ "${UID}" -ne 0 ]]
  9. then
  10. echo 'Please run with sudo or as root.'
  11. exit 1
  12. fi
  13.  
  14. # Get the username (login).
  15. read -p 'Enter the username to create: ' USER_NAME
  16.  
  17. # Get the real name (contents for description field).
  18. read -p 'Enter the name of the person or application that will be using this account: ' COMMENT
  19.  
  20. # Get the password
  21. read -p 'Enter the password to user for the account: ' PASSWORD
  22.  
  23.  
  24. # Create the user with passwordff
  25. useradd -c "${COMMENT}" -m "${USER_NAME}"
  26.  
  27. # Check to see if the useradd command succeeded.
  28. if [[ "${?}" -ne 0 ]]
  29. then
  30. echo 'The account could not be created'
  31. exit 1
  32. fi
  33.  
  34. # Set the password.
  35. echo ${PASSWORD} | passwd --stdin ${USER_NAME}
  36.  
  37. if [[ "${?}" -ne 0 ]]
  38. then
  39. echo 'The password for the account could not be set'
  40. exit 1
  41. fi
  42.  
  43.  
  44. # Force password change on first login.
  45. passwd -e ${USER_NAME}
  46.  
  47. if [[ "${?}" -ne 0 ]]
  48. then
  49. echo "The password could not be expired"
  50. exit 1
  51. fi
  52.  
  53. # Display the username, password, and the host where the user was created.
  54. echo
  55. echo 'username:'
  56. echo "${USER_NAME}"
  57. echo
  58. echo 'password: '
  59. echo "${PASSWORD}"
  60. echo
  61. echo 'host: '
  62. echo "${HOSTNAME}"
  63. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement