LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Accidentally (chmod 755) the whole thing (https://www.linuxquestions.org/questions/linux-newbie-8/accidentally-chmod-755-the-whole-thing-713106/)

littlegreywings 03-20-2009 08:07 AM

Accidentally (chmod 755) the whole thing
 
I meant to run

find . -type d -exec chmod 755 {} \;


on a certain folder, but did it to / by mistake. I ctrl+c'd pretty quick, but I think it did some damage cause now I get an error on one of my webpages (I'm running apahce and mysql).

The error is:

SQLSTATE[HY000]: General error: 1 Can't create/write to file '/tmp/#sql_5916_0.MYI' (Errcode: 13)

When trying to install magento

AlucardZero 03-20-2009 08:35 AM

chmod 1777 /tmp

Code:

drwxrwxrwt 5 root root 4096 Mar 20 04:02 /tmp
But who knows what else has been messed up by that.

i92guboj 03-20-2009 08:53 AM

The best bet could be to reinstall ALL the packages in your system, if your package manager allows that at all, if not I guess that everything will be fixed with the time as packages get updated.

The most important bit if you are running servers of any kind is to revise by hand the permissions in your server directories and files, and in everything under /home and /etc.

hatebreeder666 03-20-2009 09:00 AM

cool

i92guboj 03-20-2009 09:11 AM

By the way, I see this same problem regularly but I've never seen anyone doing a backup or the file permissions, it could be a worthy idea to consider if you are afraid of mistakes like these. I never though of this myself, but the idea just came to me and it would be very simple to do so. Something like the following code should backup all the permissions in your system and save them into a plain text file, that you can use for either future reference or even to restore them all using a simple loop to feed chmod:

Code:

find /path/to/ -exec stat --format="%a %n" "{}" \; > chmod.txt
You could even compress that file and since it's a txt file it shouldn't take a lot of space.

Just a random idea I had. Not sure if anyone will care about it :)

marvelade 03-20-2009 09:21 AM

Quote:

Originally Posted by i92guboj (Post 3482091)

Just a random idea I had. Not sure if anyone will care about it :)

I think it's a very good idea, actually. Maybe you could elaborate some more on what steps to take to successfullybackup and restore file perms. I'd use it :)



grtz,
Marvelade

i92guboj 03-20-2009 10:21 AM

Quote:

Originally Posted by marvelade (Post 3482103)
I think it's a very good idea, actually. Maybe you could elaborate some more on what steps to take to successfullybackup and restore file perms. I'd use it :)



grtz,
Marvelade

Well, let's rescue the line I posted above:

Code:

find /path/to/ -exec stat --format="%a %n" "{}" \; > chmod.txt
Now, let's piece it:

Code:

find /path/to/
This will look into /path/to (change by whatever path you wish, but it must exist) for files of any kind.

Code:

-exec stat --format="%a %n" "{}" \;
This will run "stat" on each one of the found files. What this command will do is to print the permissions and file name for each found file or directory.

Code:

> chmod.txt
Will store the results into a plain text file called "chmod.txt". You can try this yourself, just change /path/to/ by any path in your home, to test. The command doesn't write or erase anything, except in chmod.txt. Then you can open chmod.txt with any text editor to see its contents, you will see two columns, the first one are permissions, the second column is the file name.

This information could be used in these cases to restore the permissions either manually, or using a self made script that reads from this file, and chmods everything accordingly.

I don't know if some serious tool exist for this, but seeying how amazingly simple would be to develop one and the number of persons that ask this same question from time to time here and in another forums, I am surprised that I have never heard of such a tool. And I suspect that maybe it doesn't exist, which is strange.

This is just a quick idea, but it would be simple enough to implement it even with shell scripting. The simples case would be this:

Code:

while read LINE; do PERMS=${LINE%% *}; FILE=${LINE#* }; chmod $PERMS "$FILE"; done < chmod.txt
This will read lines from chmod.txt, separate the permissions from the file names, and issue a chmod command with that info, one chmod per file, restoring the permissions that were saved previously in chmod.txt.

A simple backup system for file permissions. I can't see any downside to using this, BUT if you decide to use this, make your tests. I don't wanna feel responsible if my oneliners fry your pc or kill your kitten hehe :)

EDIT: Fixed find command
EDIT: Fixed line to restore

yanghwanim 03-20-2009 11:34 AM

or you could use R1Soft hot copy software which is free. the software allows you to take a snapshot of the system before making changes, and once the changes are made you can always revert back to the point before the change all on the CLI.

jiml8 03-20-2009 11:47 AM

I do periodic (daily) / and /home backups using rsync. Rsync copies permissions.

So, faced with this disaster, I could either copy back the rsync'ed files to change the permissions, or I could construct the necessary permissions file as previously suggested, then use it to make repairs.

I can't speak to all the backup programs out there, but I would expect that most would also backup permissions automatically.

pwc101 03-20-2009 11:59 AM

Quote:

Originally Posted by i92guboj (Post 3482091)
By the way, I see this same problem regularly but I've never seen anyone doing a backup or the file permissions, it could be a worthy idea to consider if you are afraid of mistakes like these. I never though of this myself, but the idea just came to me and it would be very simple to do so. Something like the following code should backup all the permissions in your system and save them into a plain text file, that you can use for either future reference or even to restore them all using a simple loop to feed chmod:

Code:

find /path/to/ -exec stat --format="%a %n" "{}" \; > chmod.txt
You could even compress that file and since it's a txt file it shouldn't take a lot of space.

Just a random idea I had. Not sure if anyone will care about it :)

I can't believe I've never seen this before either. I've since added it to my daily backup script (with some refinements, namely multiple directories as input) and now I can be sure to have all the critical system permissions saved every day. Incidentally, the bzipped files come to a whopping 89.4K! Well worth the space if you ask me :)

GazL 03-20-2009 12:47 PM

watch out for symlinks, stat will return 777 for them, but if you use that value later via chmod it will change the permissions on the file/directory it points to, not the symlink.

So,

Code:

find /path/to ! -type l -exec stat --format="%a %n" "{}" \; >perms.backup

tredegar 03-20-2009 12:52 PM

This is an interesting thread because I once did a chown -r tred:tred .* as root from my home directory and wondered (too late!!) why it was taking more than a moment to complete (think about it).

I reinstalled :( and I learnt to be better with backups.

@pwc101,
It might be thoughtful to post your refinements to i92guboj's script here.

mrclisdue 03-20-2009 01:10 PM

Yes, this is great stuff. So thanks to all, especially i92guboj, pwc101 and GazL

cheers,

rweaver 03-20-2009 01:26 PM

Quote:

Originally Posted by littlegreywings (Post 3482018)
find . -type d -exec chmod 755 {} \;

With great power comes great responsibility-- I suggest always running find with -exec with an echo first :)

Code:

find . -type d -exec echo chmod 755 {} \;

i92guboj 03-20-2009 01:41 PM

Quote:

Originally Posted by GazL (Post 3482320)
watch out for symlinks, stat will return 777 for them, but if you use that value later via chmod it will change the permissions on the file/directory it points to, not the symlink.

So,

Code:

find /path/to ! -type l -exec stat --format="%a %n" "{}" \; >perms.backup

Good catch, well spotted and corrected. That could be a security breach so yes, it's an important thing.

Quote:

Originally Posted by rweaver (Post 3482360)
With great power comes great responsibility-- I suggest always running find with -exec with an echo first :)

Code:

find . -type d -exec echo chmod 755 {} \;

I always advice that as well. A simple "echo" thrown in the middle can be used to make sure of what the results would be. Then if you agree you can remove it.


All times are GMT -5. The time now is 08:05 PM.