Changing default file permissions for SFTP

We primarily use SFTP for file transfers, and we encourage our customers to do the same.

One problem we’ve been having is that the default file permissions when files are uploaded in SFTP don’t match the umask set for that user, or the umask set on the server. This causes many php scripts to Internal Server Error. Files uploaded via normal FTP are fine, because the FTP service sets the permissions on the files with a umask configuration in the config file.

My basic solution to this, is to create a script that sits between SSH and SFTP and changes the umask as the user logs in:

> nano /opt/sftp-server.sh

#!/bin/bash
umask 022
/usr/libexec/openssh/sftp-server

Then edit the ssh_d config file (/etc/ssh/sshd_config) and edit the sftp SubSystem line to point to your script:

Subsystem       sftp    /opt/sftp-server.sh

Make sure you’ve set the permissions correctly on your new script:

> chmod 755 /opt/sftp-server.sh

Now files uploaded via SFTP should have the permissions 755!

This entry was posted in Linux and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>