add-sftp-user/add-sftp.sh

38 lines
1.2 KiB
Bash
Raw Normal View History

2023-10-26 15:54:56 +00:00
#!/bin/bash
2023-10-30 13:13:03 +00:00
echo "Script started" >> /home/jelastic/add-sftp-user-addon/script_output.log
# Generate random username and password
2023-10-26 15:54:56 +00:00
USERNAME="user$(shuf -i 10000-99999 -n 1)"
PASSWORD=$(openssl rand -base64 12)
# Append the username to the ROOT_DIRECTORY to ensure a unique home directory for each user
ROOT_DIRECTORY="/var/www/webroot/ROOT/$USERNAME"
2023-10-26 15:54:56 +00:00
# Check if user already exists
if id "$USERNAME" &>/dev/null; then
2023-10-30 13:13:03 +00:00
echo "User $USERNAME already exists." >> /home/jelastic/add-sftp-user-addon/script_output.log
2023-10-26 15:54:56 +00:00
exit 1
fi
2023-10-30 13:13:03 +00:00
# Create user
useradd -m -d $ROOT_DIRECTORY $USERNAME
2023-10-26 15:54:56 +00:00
if [ $? -ne 0 ]; then
2023-10-30 13:13:03 +00:00
echo "Failed to create user $USERNAME." >> /home/jelastic/add-sftp-user-addon/script_output.log
exit 1
2023-10-26 15:54:56 +00:00
fi
echo "$USERNAME:$PASSWORD" | chpasswd
2023-10-27 16:19:22 +00:00
# Set user's group to both litespeed and root
usermod -aG litespeed,root $USERNAME
2023-10-30 13:13:03 +00:00
# Adjust permissions for the user to write to the ROOT_DIRECTORY
chown $USERNAME:root $ROOT_DIRECTORY
chmod 775 $ROOT_DIRECTORY
2023-10-26 15:54:56 +00:00
# Get the hostname
2023-10-30 13:13:03 +00:00
HOSTNAME=$(hostname -f)
echo "Script completed for user $USERNAME with hostname $HOSTNAME" >> /home/jelastic/add-sftp-user-addon/script_output.log
2023-10-30 13:49:41 +00:00
echo "USERNAME:$USERNAME"
echo "PASSWORD:$PASSWORD"