Webserver permissions once and for all

Posted September 25, 2019 4.5k views
Linux BasicsNginxPHPInitial Server Setup

So after hours and hours of looking around tons of links and websites, all pointing people in different directions in terms of “the correct” or “best practice” for file-permissions on a webserver, I still haven’t found a solid answer.

What also surprises me is that I have seen no real information in official documentations regarding such a vital aspect of configuring a webserver on nginx (or apache for that matter).

I have the following scenarios that will need to be taken care of:

  • Of course my web-server needs to execute/read/write to files
  • My nginx is run by www-data and /var/ww/html/ folder is owned by www-data user & group
  • I need my deployment service to write/delete in /var/www/html. It has it’s own user and is a part of www-data group.
  • I need my own user to be able to write/delete (when configuring, testing, changing, etc), without using sudo. I am also a part of www-data

Currently I’m inclined to use 775 on everything, but I know and feel this is wrong.

After running chmod 775 -R /var/www/html the permission syms looks weird: drwxrwsr-x (it seems to be an invalid one, and no online chmod calculator I have found can translate it to anything - they all say it’s invalid). The ls -la command in /var/www returns this:

mattias@jwlry:/var/www$ ls -la
total 12
drwxrwsr-x  3 www-data www-data 4096 Sep 24 17:05 .
drwxr-xr-x  14 root     root     4096 Sep 24 17:05 ..
drwxrwsr-x  4 www-data www-data 4096 Sep 25 09:45 html

Please enlighten me

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

Hey, @mattiasf


The s in rws stands for setuid meaning set user ID. This is a special permission bit that allows the program, when run by any user, to be run with the effective UID of the owner, in this case, www-data. So when you as a normal user run the sudo executable, you effectively do so as www-data. This permission bit is a security risk, and should only be applied where absolutely necessary.

Explanation of the setuid bit:

When applied to an executable file, it sets the effective user ID from that of the real user (the user actually running the program) to that of the program’s owner. Most often this is given to a few programs owned by the superuser. When an ordinary user runs a program that is “setuid root” , the program runs with the effective privileges of the superuser. This allows the program to access files and directories that an ordinary user would normally be prohibited from accessing. Clearly, because this raises security concerns, the number of setuid programs must be held to an absolute minimum.

To remove the setuid bit use the -s argument with the chmod command:

chmod u-s /path/to/file

Let me know if you have any questions.

  • Thanks for your answer. I am still not sure about what permissions my files should have on my nginx server.

    • You can set the file permissions to 644 as usually this works fine on nginx. If there are any issues you can then modify this in order to match your needs.

      Looking at what you need, 644 should do the job.

      Let me know how it goes.