Advertisement
myapit

bash script create user

Mar 12th, 2017
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.05 KB | None | 0 0
  1. #!/bin/bash
  2. genpasswd() {
  3.         local l=$1
  4.         [ "$l" == "" ] && l=5
  5.         tr -dc a-z < /dev/urandom | head -c ${l} | xargs
  6. }
  7.  
  8. checkuser() {
  9.    getent passwd $USERNAME > /dev/null 2&>1
  10.    if [ $? -eq 0 ]; then
  11.     return 1
  12.    else
  13.     return 0
  14.    fi
  15. }
  16.  
  17. createuser() {
  18.    useradd $USERNAME -d /home/$GROUP/$USERNAME/ -s /bin/false -G $GROUP
  19.    cd /home/$GROUP
  20.    chown root:root $USERNAME
  21.    chmod 755 $USERNAME
  22.    cd /home/$GROUP/$USERNAME/
  23.    mkdir public_html
  24.    cd $HTDOC
  25.    mkdir $FOLDER
  26.    echo "mount --bind $HTDOC/$FOLDER /home/$GROUP/$USERNAME/public_html" | sudo tee -a $RUNBIND
  27.    echo $GENPASS | sudo passwd --stdin $USERNAME
  28.    mount --bind $HTDOC/$FOLDER /home/$GROUP/$USERNAME/public_html
  29.    cd /home/$GROUP/$USERNAME/
  30.    chown $USERNAME:apache public_html
  31.    chmod 775 -R public_html
  32.    chmod g+s -R public_html
  33.    chmod u+s -R public_html
  34. }
  35.  
  36.  
  37.  
  38. cetak(){
  39.    echo "+----------------------------------+"
  40.    echo "|    User Account Information      |"
  41.    echo "+----------------------------------+"
  42.    echo "  SFTP :-                         "
  43.    echo "  Username : $USERNAME            "
  44.    echo "  Password : $GENPASS             "
  45.    echo "                                  "
  46.    echo "   *** Done $(date) ***      "
  47.    echo "                                  "
  48.    echo "+----------------------------------+"
  49. }
  50.  
  51. clear
  52. echo "***********************************************************************"
  53. echo "*           Welcome to SFTP Only Shell Account Creator                *"
  54. echo "***********************************************************************"
  55. echo ""
  56. ROOTUSER=""
  57. ROOTPASS=""
  58. HTDOC="/var/www/html"
  59. RUNBIND="/opt/runbind.sh"
  60. GROUP="sftponly"
  61. GENPASS=$(genpasswd)
  62. echo "---> SFTP INFO <---"
  63. read -p "Enter Username for SFTP : " USERNAME
  64. read -p "Enter Folder in WWW: " FOLDER
  65. ROOTUSER="root"
  66. echo "Folder in www: $FOLDER"
  67. echo "---> MySQL INFO <---"
  68. echo "Root user :->root"
  69. echo ""
  70. NEWUSER=$USERNAME
  71.  
  72. if checkuser -eq 0; then
  73.    createuser
  74.    cetak
  75.    exit
  76. else
  77.    echo "error"
  78.    exit
  79. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement