-1

I had a big mistake

I run this command on a centos 6.8 server:

chmod 0644 -R /* instead of chmod 0644 -R ./*

now directory listing on root directory / and all files failed when I run this command:

chmod 0775 -R /*

I get this error:

bash: /bin/chmod: Permission denied

how can I fix it without reinstall my centos?

Tim
  • 30,383
  • 6
  • 47
  • 77
IVIR3zaM
  • 117
  • 2
  • See also http://serverfault.com/a/233780/37681 – HBruijn Aug 07 '16 at 03:23
  • @HBruijn this is not a dupe of the question you indicate . . . serverfault.com/a/233780/37681 only says "there are a lot of references", "reinstall". See http://serverfault.com/questions/141806/chmod-r-777-on-ubuntu-numerous-problems http://serverfault.com/questions/233764/revert-chmod-777-r (it's ubuntu but there are replies for rpm-base) – Law29 Aug 07 '16 at 08:47
  • To try and fix `/bin/chmod` itself, also see https://serverfault.com/questions/189627/i-just-did-a-chmod-x-chmod – Stefan Lasiewski Aug 08 '16 at 21:18
  • Also, get out of the habit of using `./*`. That's the same thing as just using `*`, but more complicated and error prone. Even commands like `chmod 0644 -R *` are dangerous, however. See my other answer here: [How do I prevent accidental rm -rf /*?](http://serverfault.com/a/337236/36178) – Stefan Lasiewski Aug 08 '16 at 21:21

2 Answers2

5

Reinstalling the OS is by far the easiest and quickest way to fix this. Take an image backup first so you can recover anything that hadn't made it to your regular backup before the disaster happened.

The alternative is to boot from CentOS installation media or some other Linux live media, mount your CentOS system partitions on /mnt, and and then manually fix the permissions on every file and directory. Note that setting 775 globally is not enough to make your system usable again. There are files and directories that must have different permissions settings in order to work. This is long and tedious work, and you'll wish you had heeded the advice to reinstall.

dmourati
  • 24,720
  • 2
  • 40
  • 69
Tilman Schmidt
  • 3,778
  • 10
  • 23
  • If you've installed everything from RPM packages, you can use the output of `rpm -qlva` which will list the mode and ownership of every file shipped in every package currently installed which you can use to reset the mode of most of the files without reinstalling. – bodgit Aug 07 '16 at 19:54
1

If it's not possible to reinstall the running system for obsure reason.

There is a way to install the same OS version with the same packages on another machine, and extract the filesystem rights that could be nearly restored.

sudo find / ! -path '/dev/*' ! -path '/proc/*' ! -path '/sys/*' ! -path '/tmp/*' -printf 'chmod -f %#m ' -print > fix_chmod.sh