linux - Always have to chown when adding new file? Centos 6

06
2014-04
  • Travis

    Whenever I upload a new file to my server, I have to chown -R apache:apache /dir in order for it to be accessible.

    Is there an easier way around this?

  • Answers
  • Levans

    Most likely, what is actually needed for the files to be accessible is that their group is set to apache. You can easily set it by default for new created files by running :

    find /path/to/root/directory/of/website -type d -print0 | xargs -0 chmod g+s
    

    This will set the setgid flag on all sub-directories too. With this flag set, any new file created in there will inherit the group of its parent directory. (-print0 and -0 options are designed to handle correctly spaces in filenames)

    Make sure ownerships of files are correct before running it. If unsure, you can fix it the same way :

    find /path/to/root/directory/of/website -print0 | xargs -0 chown apache:apache
    
  • Rik

    Here is what you could do with VSFTPD:

    You can set the following 2 options in you vsftpd.conf:

    guest_enable=YES
    guest_username=apache
    

    guest_enable, if enabled, makes sure all files are uploaded as user/group apache.

    Please note that this will only work for files uploaded via FTP. If you copy a file in Linux itself to your www directory, this will not adjust the permissions.


    That's why, if you're also accessing that directory via the filesystem, it is best to use the chmod g+s-method Levans suggested.
    (i.e.)

    find /var/www/site/public_html -print0 | xargs -0 chown apache:apache
    find /var/www/site/public_html -type d -print0 | xargs -0 chmod g+s
    

    guest_enable
    If enabled, all non-anonymous logins are classed as "guest" logins. A guest login is remapped to the user specified in the guest_username setting.

    Default: NO

    guest_username
    See the boolean setting guest_enable for a description of what constitutes a guest login. This setting is the real username which guest users are mapped to.

    Default: ftp


  • Related Question

    How to chmod and chown hidden files in Linux?
  • nothing-special-here

    How do I recursively execute chmod or chown for hidden files?

    sudo chmod -R 775 * does not work on hidden files.

    The same thing goes for sudo chown -R user:group.


  • Related Answers
  • slhck

    If you're okay also chmod'ing the current directory, do that and let -R do the heavy lifting. -R does not ignore hidden files.

    sudo chmod -R 775 .
    
  • slhck

    * doesn't include hidden files by default, but if you're in bash, you can do this with:

    shopt -s dotglob
    

    Read more about it in bash's builtin manual:

    If set, Bash includes filenames beginning with a `.' in the results of filename expansion.

    This will make * include hidden files too.

    chmod -R 775 *
    

    Disable it with:

    shopt -u dotglob
    
  • RedX

    Another option is to use find i like it since you can have very fine grained control over it.

    find <path to start from> -exec chown <options> {} \+
    
    find -path '<path to include>' -exec chown <options> {} \+
    

    The only downside is that find has different syntax on different versions.