add-sftp-user/add-sftp.sh

39 lines
905 B
Bash

#!/bin/bash
# Generate random username and password
USERNAME="user$(shuf -i 10000-99999 -n 1)"
PASSWORD=$(openssl rand -base64 12)
ROOT_DIRECTORY="/var/www/webroot/ROOT"
# Check if user already exists
if id "$USERNAME" &>/dev/null; then
echo "User $USERNAME already exists."
exit 1
fi
# Create user and set password
useradd -m -d $ROOT_DIRECTORY $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 the ROOT_DIRECTORY
chown $USERNAME:root $ROOT_DIRECTORY
chmod 775 $ROOT_DIRECTORY
# Get the hostname
HOSTNAME=$(hostname -f)
# Print the credentials
echo "SFTP User Created Successfully!"
echo "Username: $USERNAME"
echo "Password: $PASSWORD"
echo "SFTP Host: $HOSTNAME"
echo "Port: 22"