Shell script - craete user/password/home dir

Hi all,

I have created an ftps server using vsftpd and it works a treat

atm to create a user/password/home dir I do these two command -

useradd test -d /mnt/data/test
passwd test

also I have another dir that I use for home dirs, which is using a nfs location (vfx_ftp) and not the local sdb1 (data)

useradd test1 -d /mnt/vfx_ftp/test1
passwd test1

How can I make this into a script i.e. so it asks if its for a vfx ftp or local ftp and also so it appends a file each time with the username and password so in case someone forgets the password for their ftp I can look at the file and tell them

many thanks,

rob

How will you want to decide which user account is assigned to each?

It should be relatively easy to achieve if you can clearly state the rules you want to apply.

Kind regards,
Robin

cant the script ask if you want it to be a vfx ftp (mounted on nfs share) or a data ftp (mounted on local sdb1 drive)?

and then from there it will ask you to enter a username and password and from the above question the home dir will be what you selected ie data mount or vfx mount

It can indeed, I was assuming that you needed an automatic way to work it out. A few questions, if I may:-

  • What shell are you using? (the output from echo $SHELL would be fine)
  • What OS version are you using? (the output from uname -a would be very useful here)
  • What have you tried?

I ask the last question because it is better for us to know your style and adjust it rather than to impose a solution that you cannot understand or support.

Please post all code, files, input & output/errors in CODE tags. It makes it far easier to read.

Kind regards,
Robin

getting close -

 
#!/bin/bash
echo "Enter UserName:"
read user
echo "Enter Password:"
read passwd
echo "$user:$passwd" >> /ftp.txt
useradd $user -b /mnt/data/
echo $user:$passwd | chpasswd

Yes, getting closer. Now: ask if "vfx ftp" or "data ftp" the same way.
What do you need ftp.txt for?

i have made it to a T now -

#!/bin/bash
  echo "Enter UserName:"
  read user
    if id $user ; then
    echo "$user already exists as you can see above, please re-run the script"
    exit
    else
    echo "$user not in system, ok to continue"
    fi
  echo "Enter Password:"
  read passwd
  echo "$user:$passwd" >> /ftp_details/accounts.csv
  useradd $user -s /sbin/nologin -b /mnt/data/
  touch /mnt/data/$user/files_will_get_deleted_older_than_14_days_old.txt
  echo $user:$passwd | chpasswd

You are getting there. Ask your user if they are a vxf or normal user, read in the reply then using an if set a variable for the home directory. You can then use this variable in the useradd command.

Let us know if you get stuck,
Robin

thanks Robin,

how do i catch the users input ie if they write v make a vfx_ftp or if they write n make a normal ftp

---------- Post updated at 02:06 PM ---------- Previous update was at 01:52 PM ----------

found the answer -
bash - How do I prompt for Yes/No/Cancel input in a Linux shell script? - Stack Overflow

#!/bin/bash
        echo "Enter UserName:"
        read user
                if id $user ; then
                echo "$user already exists as you can see above, please re-run the script"
                exit
                else
                echo "$user not in system, ok to continue"
                fi
        echo "Enter Password:"
        read passwd
        echo "$user:$passwd" >> /ftp_details/accounts.csv
        echo "is this a normal user (1) or vfx user (2) ?"
        read dir

        case $dir in
                1)
                useradd $user -s /sbin/nologin -b /mnt/data/
                touch /mnt/data/$user/files_will_get_deleted_older_than_14_days_old.txt
                ;;
                2)
                useradd $user -s /sbin/nologin -b /mnt/vfx/
                ;;
        esac
        echo $user:$passwd | chpasswd
1 Like