Ubuntu – How to reset the permissions of /var

permissions

My friend did a chmod -R 777 /var by mistake. Now he has his /var folder permissions like this:

drwxr-xr-x 15 root   root     4096 Dec 29 17:36 .
drwxr-xr-x 23 root   root     4096 Dec 10 11:15 ..
drwxrwxrwx  2 root   root     4096 Dec 28 09:54 backups
drwxrwxrwx 22 root   root     4096 Nov  9 11:06 cache
drwxrwsrwx  2 root   whoopsie 4096 Dec 31 09:38 crash
drwxrwxrwx  2 root   root     4096 Apr 23  2012 games
drwxrwxrwx 62 root   root     4096 Dec 10 11:06 lib
drwxrwsrwx  2 root   staff    4096 Apr 19  2012 local
lrwxrwxrwx  1 root   root        9 Dec 29 17:36 lock -> /run/lock
drwxrwxrwx 19 root   root     4096 Dec 31 09:54 log
drwxrwsrwx  2 root   mail     4096 Apr 23  2012 mail
drwxrwxrwx  2 root   root     4096 Apr 23  2012 opt
lrwxrwxrwx  1 root   root        4 Dec 29 17:36 run -> /run
drwxrwxrwx 10 root   root     4096 Jun 29  2012 spool
drwxrwxrwx  2 root   root     4096 Dec 31 10:03 tmp
drwxrwsrwx 15 noufal www-data 4096 Dec 28 10:59 www

And I have mine as:

drwxr-xr-x 15 root root     4096 Dec 29 18:16 .
drwxr-xr-x 24 root root     4096 Dec 18 10:03 ..
drwxr-xr-x  2 root root     4096 Dec 31 10:00 backups
drwxr-xr-x 24 root root     4096 Nov  7 15:03 cache
drwxrwsrwt  2 root whoopsie 4096 Dec 31 09:55 crash
drwxr-xr-x  2 root root     4096 Apr 23  2012 games
drwxr-xr-x 74 root root     4096 Dec 29 17:30 lib
drwxrwsr-x  2 root staff    4096 Apr 19  2012 local
lrwxrwxrwx  1 root root        9 Dec 29 18:16 lock -> /run/lock
drwxr-xr-x 23 root root     4096 Dec 31 10:00 log
drwxrwsr-x  2 root mail     4096 Dec 31 10:39 mail
drwxr-xr-x  2 root root     4096 Apr 23  2012 opt
lrwxrwxrwx  1 root root        4 Dec 29 18:16 run -> /run
drwxr-xr-x 10 root root     4096 Jun 22  2012 spool
drwxrwxrwt  6 root root     4096 Dec 31 10:49 tmp
drwxrws--- 31 saji www-data 4096 Nov 27 15:05 www

We have similar systems. How can I reset the /var files and folders permission to the initial state, by not doing it individually for a file/folder.

Best Answer

  • One way is to install another machine or VM with the same version of the OS and on that machine run this two commands:

    find / -exec stat --format "chmod %a %n" {} \; > /tmp/restoreperms.sh
    find / -exec stat --format 'chown %U:%G %n' {} \; >> /tmp/restoreperms.sh
    

    command 'find' finds the root directory and checks for their permissions using 'chmod' and save it to a temporary permission file.

    Or this one that combines both:

    /usr/bin/find / -exec /usr/bin/stat --format="[ ! -L {} ] && /bin/chmod %a %n" {} \; -exec /usr/bin/stat --format="/bin/chown -h %U:%G %n" {} \; > /tmp/restoreperms.sh
    

    then, copy the /tmp/restoreperms.sh file to the machine with broken permissions:

    scp /tmp/restoreperms.sh user@ip_address:/tmp/
    

    scp securely copies the permissions stored to a temporary directory /tmp/ and execute it from there.