add-sftp-user/add-sftp.sh

33 lines
861 B
Bash

#!/bin/bash
# Generate random username and password
USERNAME="user$(shuf -i 10000-99999 -n 1)"
PASSWORD=$(openssl rand -base64 12)
# Set the user's home directory within the ROOT directory
USER_HOME="/var/www/webroot/ROOT/$USERNAME"
# Check if user already exists
if id "$USERNAME" &>/dev/null; then
echo "User $USERNAME already exists."
exit 1
fi
# Create user with the specified home directory
useradd -m -d $USER_HOME $USERNAME
if [ $? -ne 0 ]; then
echo "Failed to create user $USERNAME."
exit 1
fi
echo "$USERNAME:$PASSWORD" | chpasswd
# Set user's group to both litespeed and root
usermod -aG litespeed,root $USERNAME
# Adjust permissions for the user to write to their home directory
mkdir -p $USER_HOME
chown $USERNAME:root $USER_HOME
chmod 775 $USER_HOME
# Get the hostname (if needed later in the script)
HOSTNAME=$(hostname -f)