Advertisement
neal777

New user with bash script

Oct 17th, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.06 KB | None | 0 0
  1. #!/bin/bash
  2. if [ "$(id -u)" = "0" ]; then #Check if the user is root or not
  3. var=$((501 + RANDOM % 1000001)) #random generator between 501 and 1000001
  4. echo "This is user script by Sandesh Koirala."
  5. username="newbee"
  6. password="newbee"
  7. #read -p "Enter username : " username
  8. #read -s -p "Enter password : " password
  9. echo "$username:x:$var:0:10:180:14:0:" >> /etc/shadow #store userdata and password expiration
  10. echo "$username:x:$var:$var::/home/$username:/bin/bash" >>
  11. /etc/passwd #write the username and profile number
  12. echo "$username:$password" | chpasswd -c SHA512 #change password and encrypt with sha512 algorithm
  13. echo "$username:x:$var" >> /etc/group #store group id number
  14. cd /home #go to home directory
  15. mkdir $username #create a folder dedicated to the user inside home directory
  16. chmod 700 /home/$username # set owner permission to rwx whereas group and public to be no access
  17. chown -R $username /home/$username #change the ownership to newly created user
  18. echo "The user has been successuflly created"
  19. else
  20. echo "THIS SCRIPT CAN ONLY BE USED BY THE ROOT USER."
  21. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement