Linux - Set a default Group when creating new Files with SSH/FTP How to set a default group when creating, uploading, or adding new files in Ubuntu, CentOS, Debian, and other Linux distributions

Linux - Come inviare E-Mail con sSMTP (con configurazioni-tipo per GMail, Aruba e Yahoo)

If you've stumbled upon this post, it most likely means that you're trying to fix a nasty issue affecting your Linux server: every time you upload some new files to a folder (such as var/www) that has specific group access (such as www-data) using your favorite SSH or FTP(s) client, those files are created with <user>:<user> ownership rights - instead of <user>:www-data permissions like you would like to.

Such behavior basically prevents Apache, NGINX, and any other service that is configured to use www-data permissions from accessing those files, as well as other users different than you, until you use chown/chgrp commands to fix it.

Is there a way to specify a default group when creating new files, instead of using your username's group? As a matter of fact, the answer is YES. In this post, we'll see how we can do that.

UPDATE: If you want to set default permissions for newly uploaded files/folder, don't forget to read this post as well.

How to Set a Default Group for new Files

Here's what we need to do to set a default group when adding new files in a folder:

Once we do that, all existing and new files created in the /var/www directory will be owned by the www-data group. More precisely, the first command will change the current permissions for the existing files, while the latter will set the default behavior for new files. Needless to say, if the folder(s) are already owned by the www-data group, we can omit the first command and just set the default behavior for all new files.

However, it's worth noting that the above commands will only change the behavior of the root /var/www folder - not the sub-folders within it. If we want to apply the same behavior for the whole directory tree - the /var/www folder and all subfolders - we need to execute a recursive approach using the find command in the following way:

That's it.

Using a script

If you have a lot of folders (or servers) which you want to apply this fix to, you might want to perform this task with the help of a bash script.

Here's the set_default_group.sh script we are using in our web servers:

Feel free to use it!

Conclusions

We hope that this small piece of advice will help other system administrators getting rid of this nasty "ownership issue" as well.

UPDATE: If you want to set default permissions for newly uploaded files/folder, don't forget to read this post as well.

 

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

Leave a Reply

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


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.