29

So I did a chmod -x chmod. How I can fix this problem? How do I give execute rights back to chmod?

Rook
  • 2,615
  • 5
  • 26
  • 34

10 Answers10

45

In Linux:

/lib/ld-linux.so.2 /bin/chmod +x /bin/chmod

http://www.slideshare.net/cog/chmod-x-chmod

bindbn
  • 5,153
  • 2
  • 26
  • 23
  • bindbn, I was about to post the same link :-) but I was reading myself as I am new to unix / linux stuff !!! – Mutahir Oct 11 '10 at 07:43
  • 2
    On a 64-bit distro, you may have to use `/lib64/ld-linux-x86-64.so.2` instead. `ldd /bin/chmod` should list exactly which linker to run. – goldPseudo Oct 11 '10 at 07:52
  • I would write a minimal program that uses chmod(2), but this is cooler – adamo Oct 11 '10 at 07:57
  • Could someone explain why that works, for the uninitiated? I think I get it, but some documentation would be great. – Stefan Lasiewski Oct 11 '10 at 22:12
  • 1
    @Stefan: From `man ld-linux.so`: "ld.so [and ld-linux.so] loads the shared libraries needed by a program, prepares the program to run, and then runs it." (`ld.so` is for `a.out` format executables and `ld-linux.so` is for ELF format.) – Dennis Williamson Oct 12 '10 at 00:58
  • 1
    @Stefan: The kernel doesn't really know how to load and execute dynamically-linked executables, because it is highly complex and follows the glibc, not the kernel. `ld-linux.so` is sort-of executable/library hybrid, sort-of statically linked, runs in userspace, and is responsible of loading dynamically-linked executables and all their dependencies, then running them. – Juliano Nov 05 '10 at 17:09
  • /lib/ld-linux.so.2: symbolic link to `/lib32/ld-linux.so.2' What if I'm on 64bit machine? – confiq Jul 24 '11 at 15:14
18

Use python:

# python
Python> import os
Python> os.chmod("/bin/chmod",0755)
Douglas Leeder
  • 2,725
  • 18
  • 15
18

This relies on the fact that permissions of a destination file are preserved rather than the source file when it is being copied over. We're "borrowing" the permissions of ls:

cp /bin/ls /tmp/chmod.tmp
cp /bin/chmod /tmp/chmod.tmp
mv /tmp/chmod.tmp /bin/chmod
Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148
10

Using Perl:

% perl -e 'chmod 0755, qw[/bin/chmod]'
Alnitak
  • 20,901
  • 3
  • 48
  • 81
7

setfacl -m u::rx /bin/chmod

... will grant the owner execute permissions.

But, the /lib/ld-linux.so.2 trick is neat. :)

Kyle Brantley
  • 1,321
  • 1
  • 11
  • 14
5

This is weird... I saw something like this a few days ago via someone's tweet...

http://www.slideshare.net/cog/chmod-x-chmod

ThatGraemeGuy
  • 15,314
  • 12
  • 51
  • 78
  • Links to other sites are not helpful as the link will become broken at some point. This is like telling someone to google the answer. – Phil Hannent Oct 11 '10 at 13:19
  • @Phil Hannent: I've seen that slideshow too, just three days ago, so it was my first thought, if the OP was one of the applicants there. – Boldewyn Oct 11 '10 at 14:04
  • Same, just saw this on Reddit a few days back... – Dentrasi Oct 11 '10 at 22:31
5

Should you be on a system where /bin/chmod can't be loaded by the dynamic linker:

# /bin/mv /bin/chmod /bin/chmod.tmp
# install -p -m 755 /bin/chmod.tmp /bin/chmod

This works on my MacOS X system.

Alnitak
  • 20,901
  • 3
  • 48
  • 81
2

/rescue/chmod 555 /bin/chmod

I think you could also use mtree.

Darius
  • 21
  • 2
  • This would my favorite solution. It doesn't work with linux unfortunately. –  Aug 07 '12 at 18:01
2

I suspect this is not a real question: http://www.slideshare.net/cog/chmod-x-chmod

  • Reinstall chown: sudo apt-get install --reinstall coreutils
  • perl -e 'chmod 0755, "chmod"'
  • more examples in the slides
Julien
  • 1,028
  • 1
  • 12
  • 24
1

create a new chmod and use that for the original

umask 000
cat chmod > ~/my-chmod
~/my-chmod a+x chmod
Kevin M
  • 2,302
  • 1
  • 16
  • 21
  • Setting a umask of 000 won't give a file execute permission when it's created. It will, at best, get rw-rw-rw- permissions. – Barry Brown Oct 11 '10 at 15:25
  • 2
    @Barry: It actually depends on the mode passed to creat(2)/open(2)/mkdir(2)/etc. If umask == 0 and the syscall that creates the file is given 0777 for the mode, then the file will have the execute bits turned on. For example, linkers/compilers pass 0777 when writing out an “executable file” (e.g. `(umask 000;gcc -o foo foo.c)` will produce a `foo` with mode 777). However, many (most?) shells pass 0666 when they open/create files for redirection, which means that this answer is not going to work under many shells. – Chris Johnsen Oct 12 '10 at 01:52