Skip to content

Instantly share code, notes, and snippets.

@serweb-labs
Last active May 6, 2018 10:59
Show Gist options
  • Save serweb-labs/3bf99a37bc24e25035b4c96698062564 to your computer and use it in GitHub Desktop.
Save serweb-labs/3bf99a37bc24e25035b4c96698062564 to your computer and use it in GitHub Desktop.
moss servers: add sftp user
#! /bin/bash
# $1 user
# $2 from path
# prerequisites:
# create sftp-only group
# sudo groupadd sftp-only
# add sftp-only configurations
# we need to modify the ssh configuration on file /etc/ssh/sshd_config replacing the following line:
# Subsystem sftp /usr/lib/openssh/sftp-server
# to: Subsystem sftp internal-sftp
# and add in end of file:
# Match group sftp-only
# PermitTunnel no
# X11Forwarding no
# AllowTcpForwarding no
# ForceCommand internal-sftp
if test "$#" -ne 2; then
echo "expected 2 params: {user} {from-path}"
exit 1
fi
printf "add user $1\n"
read -n 1 -s -r -p "Press any key to continue"
printf "\n"
# create user
sudo adduser $1
# prevent ssh access
sudo usermod -s /bin/false $1
# add to server group
# (in moss.sh is "dev")
sudo usermod -a -G dev $1
# add sft-only group
sudo usermod -a -G sftp-only $1
# set de primary group
sudo usermod -g dev $1
printf "mounting the directory /home/$1/$(basename $2)\n"
read -n 1 -s -r -p "Press any key to continue"
printf "\n"
# create mountpoint container
sudo mkdir /home/$1/$(basename $2)
sudo chown $USER:dev /home/$1/$(basename $2)
# mount the real path
sudo mount --bind $2 /home/$1/$(basename $2)
printf "add to crontab: mounting the directory /home/$1/$(basename $2)\n"
read -n 1 -s -r -p "Press any key to continue"
printf "\n"
#write out current crontab
crontab -l > /tmp/mycron
#echo new cron into cron file
echo "@reboot mount --bind $2 /home/$1/$(basename $2)" >> /tmp/mycron
#install new cron file
crontab /tmp/mycron
rm /tmp/mycron
printf "success\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment