A bit confused about Linux permission settings

Posted July 8, 2017 3.7k views
Linux BasicsUbuntu

It’s not clear to me if my linux permission settings are secure or if I am making a mistake. I would be glad if someone chimed in on my setup running Php5-fpm on Ubuntu 14.04:

  • Document root for each virtual host: /home/<username>/public_html
  • Ownership of public_html and all its contents recursively set to username:username (chown -R username:username)
  • Added the user to the www-data group with this command: usermod -a -G www-data username so that Apache can write to the few folders/files inside public_html that it needs to.
  • My /etc/passwd file looks like username:x:1001:1001:,,,:/home/<username>:/bin/bash
  • My /etc/group file looks like www-data:x:33:username,username
  • Each of my Apache virtual hosts has a matching php5-fpm pool.conf file, where the user and group are both username (not username and www-data) so that PHP can run as the user.
  • Umask within the user account seems to be set by default to 775 for both directories and files, as any files or directories uploaded through FTP or the application get 775 permissions.

Technically, in any application, Apache should only need to read most files and write to a few directories or files, which I believe is the whole point of NOT setting ownership of the entire document root to username:www-data.

But, in my setup, all the files within a user account (document root) have permissions set to -rw-rw-r– and all the directories within the document root have permissions set to drwxrwxr-x. This makes me a bit nervous as it seems like Apache can write to all the files and directories within the user account space as the user has been added to the www-data group.

I hope someone with a better understanding of file permissions can comment on this setup.

Or, is my setup correct/secure and I just need to set the proper umask for the user?

1 comment

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

@hansen, yes. At the moment, I see the following in the home directory of each user’s account:

root@host:/home/<username># ls -la
total 247936
drwxr-xr-x  4 username username      4096 Jul  7 18:51 .
drwxr-xr-x  5 root       root            4096 Feb  5 00:25 ..
-rw-------  1 username username      7017 Jul  8 13:09 .bash_history
-rw-r--r--  1 username username       220 Feb  5 00:23 .bash_logout
-rw-r--r--  1 username username      3637 Feb  5 00:23 .bashrc
drwx------  2 username username      4096 Feb  9 02:06 .cache
-rw-------  1 username username       119 Feb 10 12:04 .mysql_history
-rw-r--r--  1 username username       675 Feb  5 00:23 .profile
drwxr-xr-x 19 username username      4096 Jul  8 15:16 public_html

Depending on the configuration, it might have a .ssh folder as well, with the public key. In the above example, public_html shows 755 permissions, which I set manually.

Thanks for offering to help.

  • @cloudnine
    But if the users are only being used to serve websites, then you can change the user, so they don’t have shell access. I actually don’t think any of those system files/folders will be created, if you set that at user creation.

    sudo chsh -s /usr/sbin/nologin username

    I would recommend that you have a look at OWASP’s guide for hardening Apache:

    • Thanks. Yes, the users are only being used to server web sites/applications. I restricted users to the document root (publichtml) by using the following command:
      “`usermod -d /home/<username>public
      html <username>”`

      I did consider disabling shell access when creating the user accounts, but the developers balk at not having shell access for carrying out some dev tasks (quick tar, untar, exporting and importing dbs etc.). Also, I don’t install FTP server, but ask users to use SFTP, which requires SSH.

      I will take a look at the link, thanks for the pointer.

      What would be the right way to set umask at the server level so that all the user accounts on the server have their directories and files set to 755 and 644 permissions? I have searched but the documentation for Ubuntu is a bit confusing. I think this will ensure that Apache can only read the directories and files in the user space, except the directories and files it’s explicitly allowed to write to.