Created
May 5, 2020 10:19
-
-
Save lsotoangeldonis/f604753705f38cbe04d49260cf868117 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#### | |
# This script automatically creates SFTP Account and allow only access to Home Directory | |
# Inspired by: # https://www.hostfav.com/blog/index.php/2017/09/08/script-to-create-a-sftp-user-to-access-only-home-directory-ubuntu-12x-14x-or-16x/ | |
# Author: Luis Soto | |
# | |
# Date: 04/05/20 | |
# | |
# Tested in Ubuntu 19.04 | |
# | |
### | |
# Check user name supplied or not | |
if [ $# -lt 1 ]; then | |
echo "Please supply a username" | |
echo "Example: " $0 "john" | |
exit | |
fi | |
# Check if username already exist | |
if id "$1" >/dev/null 2>&1; then | |
echo "Username Exists" | |
echo "Use different username" | |
exit | |
fi | |
# Declare local variable and generate random password for SFTP | |
newuser=$1 | |
sftpgroup="sftponly" | |
randompw=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1) | |
# Create new user and assign random password. | |
useradd $newuser | |
echo $newuser:$randompw | chpasswd | |
#Check if sftpgroup users group exists or create it | |
[ $(getent group $sftpgroup) ] || groupadd somegroupname | |
if ! grep -q "^${sftpgroup}:" /etc/group | |
then | |
groupadd somegroupname | |
cat <<EOF >> /etc/ssh/sshd_config | |
Match group ${sftpgroup} | |
ForceCommand internal-sftp | |
PasswordAuthentication yes | |
ChrootDirectory %h | |
PermitTunnel no | |
AllowAgentForwarding no | |
AllowTcpForwarding no | |
X11Forwarding no | |
EOF | |
fi | |
# Add user to sftpgroup group | |
usermod -G $sftpgroup $newuser | |
# Setting folder permission | |
echo "Please wait Applying Permission and setting Incoming folder" | |
mkdir /home/$newuser | |
chown root:root /home/$newuser | |
sleep 2 | |
mkdir /home/$newuser/sftproot | |
sleep 2 | |
chown $newuser:$sftpgroup /home/$newuser/sftproot | |
#create | |
mkdir -p /var/www/$newuser | |
chown -R $newuser:$sftpgroup /var/www/$newuser | |
mkdir -p /home/$newuser/sftproot/public_html | |
mount --bind /home/$newuser/sftproot/public_html /var/www/$newuser | |
sleep 2 | |
service ssh restart | |
# New Username and Password to account.txt | |
cat <<EOF >> /home/account.txt | |
$newuser $randompw | |
EOF |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#### | |
# This script DELETES AN automatically created SFTP ONLY user | |
# | |
### | |
# Check user name supplied or not | |
if [ $# -lt 1 ]; then | |
echo "Please supply a username" | |
echo "Example: " $0 "john" | |
exit | |
fi | |
# Check if username already exist | |
if id "$1" >/dev/null 2>&1; then | |
echo "Username Exists" | |
else | |
echo "Username not exits" | |
echo "check Username and try again" | |
exit | |
fi | |
# Declare local variable for username to delete | |
deleteuser=$1 | |
deluser $deleteuser | |
sudo rm -rf $deleteuser | |
umount /var/www/$newuser | |
sudo rm -rf /var/www/$deleteuser |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment