Fix useradd: warning: the home directory already exists

main
Anthony 2023-10-30 21:35:32 +08:00
parent 8e63098d8c
commit 27454dcfb1
1 changed files with 6 additions and 1 deletions

View File

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