Reset default permissions for /var/www

19,698

/var/ and /var/www/ are wrong.

sudo chmod 755 /var/    
sudo chmod 755 /var/www/

(sudo required). To fix directories and files inside /var/www/:

sudo find /var/www/ -type d -exec chmod 755 {} \;
sudo find /var/www/ -type f -exec chmod 644 {} \;

(sudo probably not required but there might be "root" owned files in those directories) Directories need the executable set, so 755, you might want to change it to 750, 775, 770 based on preferences; it lets group and others have more/less permissions). files do not, so 644, you might want to change it to 640, 664, 660 based on preferences; same reason).

That should be it.

In case you also want to reset the user and group setting (not needed if I look at the history):

sudo chown -R www-data:www-data /var/www/
Share:
19,698

Related videos on Youtube

mystupidstory
Author by

mystupidstory

Vagabond, Penetration Tester, etc etc. about.me/arjunv

Updated on September 18, 2022

Comments

  • mystupidstory
    mystupidstory over 1 year

    I completely messed up my /var/www/ folder after attempting to change some permissions. Now I am unable to open my localhost in browser.

    Following are my permissions in relevant folders.

    in root,

    d---------  14 root root  4096 Jan 16 23:57 var
    

    in var,

    drwxr-xr-x  2 root     root     4096 Jan 17 04:10 backups
    drwxr-xr-x 17 root     root     4096 Jan 17 00:57 cache
    drwxrwsrwt  2 root     whoopsie 4096 Jan 17 04:10 crash
    drwxr-xr-x 69 root     root     4096 Jan 17 03:35 lib
    drwxrwsr-x  2 root     staff    4096 Oct 19 14:44 local
    lrwxrwxrwx  1 root     root        9 Jan 16 21:13 lock -> /run/lock
    drwxrwxr-x 12 root     syslog   4096 Jan 17 16:31 log
    drwxrwsr-x  2 root     mail     4096 Oct 21 21:19 mail
    drwxrwsrwt  2 root     whoopsie 4096 Oct 21 21:30 metrics
    drwxr-xr-x  2 root     root     4096 Oct 21 21:19 opt
    lrwxrwxrwx  1 root     root        4 Jan 16 21:13 run -> /run
    drwxr-xr-x  8 root     root     4096 Oct 21 21:27 spool
    drwxrwxrwt  5 root     root     4096 Jan 17 17:27 tmp
    drw-------  3 www-data www-data 4096 Jan 16 23:57 www
    

    in /var/www/

    drwxrwx--- 3 www-data www-data 4096 Jan 17 00:33 html
    

    in /var/www/html/

    -rwxrwx--- 1 www-data www-data    20 Jan 17 00:00 info.php
    -rwxrwx--- 1 www-data www-data 11321 Jan 16 23:57 index.html
    drwxrwx--- 4 www-data www-data  4096 Jan 17 03:01 movie
    

    The following are some of the commands I did that messed the whole thing up

      199  sudo chmod -R 777 /var/www/html/
      200  sudo chown -R www-data:www-data /var/www
      201  chmod go-rwx /var/www
      202  chmod go-rwx /var/www/html/
      203  sudo chown -R www-data:www-data /var/www/html/
      204  chmod go-rwx /var/www/html/
      205  chmod go+x /var/www/html/
      206  chgrp -R www-data /var/www/html/
      207  hmod -R go-rwx /var/www/html/
      208  chmod -R go-rwx /var/www/html/
      209  chmod -R g+rx /var/www/html/
      210  chmod -R g+rwx /var/www/html/
      224  sudo chmod +x /var
      225  systemctl restart apache2
      230  sudo chmod +r -w -x /var/
      231  sudo chmod /var/ +r -w -x
      232  sudo chmod /var/ -r -w -x
      235  chmod 755 /var/www/
      236  chmod 644 /var/www/*
      237  sudo chgrp www-data /var/www
      238  sudo chmod 775 /var/www
      239  sudo chown -R www-data:www-data /var/www
      240  chmod go-rwx /var/www
      241  chmod go+x /var/www
      242  chgrp -R www-data /var/www
      243  chmod -R go-rwx /var/www
      244  chmod -R g+rx /var/www
      245  chmod -R g+rwx /var/www
      246  chown -R www-data:www-data /var/www/*
      247  chown -R www-data:www-data /var/www/
      248  chmod -R og-r /var/www/html/
      249  chown -R root:root /var/www/
      250  chmod -R 777 /var/www
      253  chmod -R 777 /var/www/html/
    

    Is there any way I can reset my permissions so that I can run my server again? Just for development purposes!

    • Rinzwind
      Rinzwind over 8 years
      Lines 230, 231, 232 messed things up badly ;-)
    • mystupidstory
      mystupidstory over 8 years
      that was me learning how much power I was actually getting when I type sudo. tbh, I trust myself so little now, that I wish root were an actual person and he could say nope, not gonna let you do that instead, when I am in noob mode.
  • Byte Commander
    Byte Commander over 8 years
    Afaik, you need sudo for every chown... Or am I wrong?
  • Rinzwind
    Rinzwind over 8 years
    Nope. Depends on what the files currently has. He has it in his history so they probably all are www-data:www-data (then nothing changes so sudo might not be needed). But probably better to have it there ;-)
  • mystupidstory
    mystupidstory over 8 years
    I apologize but a solution I tried in a last ditch attempt, solved it for me. I created a new installation and created an sh file which sets the permissions of all files in /var directory in the fresh installation and then ran it in mine. I guess it worked for me, only coz mine was a new installation with very few extra packages. I tried your solution, and they didn't seem to change the new permissions. So I guess yours should work just as well :)
  • T.Todua
    T.Todua over 5 years
    can you tell. in addition to 755 also write which is alternative when typing with letters? is g+w same as 755? i mean sudo chmod g+w .... Where i can see the tables that describe which number is which char
  • Rinzwind
    Rinzwind over 5 years
    "g" means group. "+w" means add write.