- Ubuntu server with SFTP support.
sudo
privileges to create users and modify directories.- OpenSSL installed for password hashing.
sftp_account_creation.sh
#! /bin/bash
read -p "Enter the sftp account name: " account
echo $account
read -p "Enter the sftp account password: " account_pwd
echo $account_pwd
read -p "Create new SFTP account for user {$account} with password {$account_pwd}? y/n: " prompt_confirm
if [ "$prompt_confirm" = "y" ]
then
sudo useradd -p $(openssl passwd -1 $account_pwd) -g sftp -s /sbin/nologin $account
sudo mkdir -p /sftpusers/chroot/$account
sudo chown $account:sftp /sftpusers/chroot/$account/
sudo chmod 700 /sftpusers/chroot/$account/
echo "$account created."
elif [ "$prompt_confirm" = "n" ]
then
echo "create sftp user account cancelled."
exit
else
echo "no choice selected. exiting"
fi
-
Prompt for Account Name:
read -p "Enter the sftp account name: " account
-
Prompt for Account Password:
read -p "Enter the sftp account password: " account_pwd
-
Confirmation Prompt:
read -p "Create new SFTP account for user {$account} with password {$account_pwd}? y/n: " prompt_confirm
-
Account Creation: If the user confirms with 'y':
sudo useradd -p $(openssl passwd -1 $account_pwd) -g sftp -s /sbin/nologin $account
-
Directory Setup: Creates a chroot directory for the SFTP user:
sudo mkdir -p /sftpusers/chroot/$account sudo chown $account:sftp /sftpusers/chroot/$account/ sudo chmod 700 /sftpusers/chroot/$account/
-
Feedback: The script provides feedback on the account creation status:
echo "$account created."
-
Cancellation and Exit: If the user chooses 'n' or makes an invalid choice, the script will exit gracefully:
echo "create sftp user account cancelled." exit
- Ensure that the
sftp
group exists on your server. You can create it using:sudo groupadd sftp
- The script does not handle existing users with the same name. Ensure the account name is unique.
- The password is hashed using OpenSSL for security.
-
Make the script executable:
chmod +x sftp_account_creation.sh
-
Run the script:
./sftp_account_creation.sh
This script simplifies the process of creating SFTP accounts on an Ubuntu server, ensuring that the necessary configurations and permissions are set correctly.