Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- username=$1
- password=$2
- usersGroup="sftpusers"
- usersSkelDir="/etc/skel-sftpusers"
- usersJailDir="/home"
- usersRootDir=${usersJailDir}/${username}
- usersHomeDir=${usersRootDir}/home
- usersStartDir=${usersHomeDir}/${username}
- # Get the ${usersGroup} GID
- usersGID=$(getent group | grep ${usersGroup} | tr ':' ' ' | awk '{print $3}')
- # Make the users's home directory so useradd doesn't complain
- mkdir -p ${usersHomeDir}
- # Add the user. Set their jail directory, their GID, their skel, their home, their shell, and their username
- useradd --skel ${usersSkelDir} --base-dir ${usersHomeDir} --gid ${usersGID} --create-home --shell /sbin/nologin ${username}
- # Change the uers's home directory to work correctly after the jail
- usermod --home "/home/${username}" ${username}
- # Set permission for the user's root directory
- chown root:root ${usersRootDir}
- chmod 751 ${usersRootDir}
- # Set permission for the user's home directory
- chown root:root ${usersHomeDir}
- chmod 751 ${usersHomeDir}
- # Set permission for their starting directory (still not allowed to write...)
- chown root:${usersGroup} ${usersStartDir}
- chmod 750 ${usersStartDir}
- # And then set permissions for the users's starting directory (and all other files inside it recursively)
- chown -R ${username}:${usersGroup} ${usersStartDir}/*
- chmod -R 770 ${usersStartDir}/*
- # Change their password to the one provided
- echo ${username}:${password} | chpasswd
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement