add-sftp-user/add-sftp.sh

40 lines
912 B
Bash
Raw Normal View History

2023-10-26 15:54:56 +00:00
#!/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
# Become root and Create user
sudo su
2023-10-26 15:54:56 +00:00
useradd -m -d $ROOT_DIRECTORY $USERNAME
if [ $? -ne 0 ]; then
echo "Failed to create user $USERNAME."
exit 1
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
# 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
HOSTNAME=$(hostname -f)
# Print the credentials
echo "SFTP User Created Successfully!"
echo "Username: $USERNAME"
echo "Password: $PASSWORD"
echo "SFTP Host: $HOSTNAME"
echo "Port: 22"