Skip to content

Instantly share code, notes, and snippets.

@gmanley
Last active April 11, 2018 16:32
Show Gist options
  • Save gmanley/cfe1acfa5045d92bc19e to your computer and use it in GitHub Desktop.
Save gmanley/cfe1acfa5045d92bc19e to your computer and use it in GitHub Desktop.
How to setup locked down sftp access
# This script should be modified and run whenever you need to add another sftp user.
echo "This script needs root access"
sudo -s
### Modify
user=soshistyle
# Directory you want to give access to
base_dir=/var/www/vhosts/style.soshified.com
# The group that should still be able to access $base_dir
base_dir_group=www-data
###
### Don't modify
sftp_group=filetransfer
home=/home/$user
base_dir_mount=$home/$(basename $base_dir)
###
useradd -m -G $sftp_group $user
chown root:root $home
chmod 755 $home
mkdir $base_dir_mount
echo "$base_dir $base_dir_mount none bind 0 0" >> /etc/fstab
mount -a
chown -R $user:$base_dir_group $base_dir_mount
echo "Enter password for $user"
passwd

Add the following to your sshd_config file.

Usually either: /etc/ssh/sshd_config or /etc/sshd_config

Subsystem sftp internal-sftp

Match group filetransfer
  PasswordAuthentication yes
  ChrootDirectory %h
  X11Forwarding no
  AllowTcpForwarding no
  ForceCommand internal-sftp

Restart ssh server. Usually: /etc/init.d/ssh restart

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment