Applying chmod 755 (or other) when ftping to apache var/www/ virtual host directories?

1.1k views Asked by At

I'm struggling to understand how to apply directory and file permissions so that files and directories created by the ftp user, via an ftp client, are browsable. In other words 755 is applied by default.

I'm running Apache 2 on Ubuntu 14.04.2 LTS, Trusty Tahr, with vsftpd server.

My directory structure is as follows.

var/www/
var/www/sites/
var/www/sites/master_demo/public_html
var/www/sites/test_site/public_html

I've run the following

usermod -aG www-data test_user
chgrp -R www-data /var/www/sites/test_site/public_html/
chown -R test_site:www-data /var/www/sites/test_site/public_html/
chmod -R 755 /var/www/sites/test_site/public_html/

Permissions on the directories are as follows:

cd var/www/sites/
ls -l
dr-xrws--- 3 master_demo www-data 4096 Jun 17 14:00 master_demo
drwxr-xr-x 3 test_site   www-data 4096 Jun 16 13:32 test_site

cd var/www/sites/test_site/
ls -l
drwxr-xr-x 3 test_site www-data 4096 Jun 17 17:43 public_html

When a file is added using ftp by the test_site user these are the permissions

-rw------- 1 test_site test_site  152 Jun 18 08:27 about.html
drwx------ 2 test_site test_site 4096 Jun 18 08:27 css
-rw------- 1 test_site test_site  152 Jun 18 08:27 index.html

How do I get the file to automatically be given the correct permissions so the files are browsable (755)? I know the obvious answer might be to run a batch file, but is that necessary? I don't want to have to run the command everytime.

chmod -R 755 /var/www/sites/test_site/public_html/*
ls -l
-rwxr-xr-x 1 test_site test_site  152 Jun 18 08:27 about.html
drwxr-xr-x 2 test_site test_site 4096 Jun 18 08:27 css
-rwxr-xr-x 1 test_site test_site  152 Jun 18 08:27 index.html

I'm new to linux so please be gentle :)

I can confirm that test_site users is in the www-data group

id test_site
uid=1004(test_site) gid=1005(test_site) groups=1005(test_site),33(www-data)
1

There are 1 answers

2
Rikard Söderström On BEST ANSWER

I think what you are looking for is umask

 umask 0022
 umask -p
 umask -S

You will need to modify your default /etc/profile in order to make this permanent. umask 0022will on creation give directories chmod 755 and files chmod 644 which is the recommended permissions for the www folder in apache.

Example

umask 0022
mkdir www
touch www/{index.html,style.css}
drwxr-xr-x. 2 user user 4096 Jun 18 10:53 .
drwxr-xr-x. 3 user user 4096 Jun 18 10:52 ..
-rw-r--r--. 1 user user    0 Jun 18 10:53 index.html
-rw-r--r--. 1 user user    0 Jun 18 10:53 style.css

FTP also supports setting umask before initiating a transfer. Setting it with ftp, sets if for that transfer only, so that you don't need to change it system-wide as you would with /etc/profile.