3

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.

Braiam
  • 67,791
  • 32
  • 179
  • 269
saji89
  • 12,007

2 Answers2

7

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.

owl
  • 4,951
  • / has to be replaced with /var. Right? – saji89 Dec 31 '12 at 06:57
  • Looks like what I had been looking for, could you please explain what the commands are doing? – saji89 Dec 31 '12 at 07:01
  • When I tried find /var -exec stat --format "chmod %a %n" {} \; > /tmp/restoreperms.sh find /var -exec stat --format 'chown %U:%G %n' {} \; >> /tmp/restoreperms.sh I got the error find: paths must precede expression: find – saji89 Dec 31 '12 at 07:20
  • @saji89 added some information about what I am doing. – owl Jun 13 '14 at 11:39
1

try this find /var -name "*" | while read -r dir ; do echo "$dir"; stat -c %a "$dir"; done you will be able to find the file name with the permission and write it to a file and copy the file to your friend's computer and match each filename by writing another script there.note that chmod has chmod --reference=RFile file facility so you can search for each matching file and apply the reference there.