This question already has an answer here:

Today I was told a tale by a Unix trainer where the root password got leaked to the students, and one of the fellas removed the execute permission from /usr/bin/chmod itself. How do you recover chmod in this case and make it executable again? Let's say getting it from an external source or recompiling it is not a preferable option, is there some smart hack that can recover this chmod itself?

Note that this happened a long time ago and I'm not looking for a solution for some current problem, just curious about what ways Unix provides us around such an issue.

marked as duplicate by Gilles permissions Jul 14 '16 at 18:01

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 6
    Since all the answers provided are equally "right answers" here, I'm not marking any one of them in particular. Thanks to everyone who answered, every answer was educative in some way. – sundar Jul 22 '13 at 17:36
  • 2
    Note that the "how do I do this without rebooting?" is as important an exercise as "How do I boot a rescue/installation media and fix this?". In my opinion you need to be able to do both - it might be a job saver some day. – Thorbjørn Ravn Andersen Jul 23 '13 at 8:28
  • 2
    The same question (and similar answers) exists on ServerFault, too. – SpellingD Jul 23 '13 at 19:30
  • 1
    @SpellingD Thanks. It's weird reading that thread, like an alternate universe version of this question with only slightly different answers. And someone there answers they've seen the question previously on slideshare, and someone comments on that one that they've seen it on Reddit too! – sundar Jul 23 '13 at 19:43
  • Similar: How can I execute a file without execute permissions? at AskUbuntu – kenorb Aug 7 '15 at 23:28

You can run the loader directly, and pass it the command you want to run:

/lib/ /bin/chmod +x /bin/chmod

Your path to the loader might vary. On a 64-bit system you need to choose the right one based on how chmod was compiled; the 64-bit version is named something like /lib64/

  • 36
    I love any day where I learn something new. – EightBitTony Jul 21 '13 at 19:24
  • 3
    Thanks. What permissions does itself have? (I have only cygwin and it doesn't seem to have this file.) Is this an easy way to override the executable permission bit then? – sundar Jul 21 '13 at 19:33
  • 2
    minor correction: 64 bit libraries are usually under /lib64 – Bratchley Jul 21 '13 at 20:43
  • 6
    On x86 (32-bit) Linux, the dynamic linker must be at exactly the path /lib/, on x86-64 (64-bit) Linux, the dynamic linker must be at exactly the path /lib64/ Both of these are normally symlinks to the correct file, but that exact path is required to be present. – Jonathan Callen Jul 22 '13 at 2:43
  • 6
    @gerrit If everything gets the execution bit unset, you reboot the system with a live CD and reset the permissions. However, in that case it's probably easier to just reinstall the whole system since who knows what has been borked in the process. – Janos Pasztor Jul 22 '13 at 10:00

The chmod utility relies on the chmod() system call (see man 2 chmod). So you could do this with a few lines of C, or just about any other language that has a wrapper around it (which would be most of them). Very few *nix systems are going to lack a C compiler and a perl interpreter; most linux distros require the later to work.

perl -e 'chmod 0755, "foobar.file"'
  • 3
    Thanks, that's sweet. I just tried it out in Perl and it worked. – sundar Jul 21 '13 at 19:28
  • @sundar perl is indeed the fastest way to go. – ott-- Jul 21 '13 at 19:55
  • 2
    @ruakh, on Debian-based systems the perl-base package is marked as essential. Not having perl-base will make the package manager pretty unhappy. There are a large number of other essential/important tools like adduser that require perl, and are installed in an absolute bare-bones setup. – Zoredache Jul 22 '13 at 5:24
  • 1
    @ruakh The reason it is more true of perl than anything else is that perl 5 was around during linux's infancy, while (eg) python 2 was not. It is kind of the original "dynamically typed, object oriented interpreted" language. So in general the 3 most fundamental languages in the linux world would be 1) C, 2) bash, and 3) perl. Python has probably eclipsed it in popularity in recent years, but it is just not as historically relevant and thus less likely to be essential -- although in reality both of them are usually available anyway. – goldilocks Jul 22 '13 at 14:44
  • 1
    @goldilocks: Interesting! The Single Unix Specification does mark cc as "LEGACY" (and therefore optional), but I didn't realize that Linux distros were actually taking it up on that. :-P (Of course, it doesn't list perl at all, even as an optional utility.) – ruakh Jul 22 '13 at 16:38

Some systems also have busybox installed in which case you may run:

busybox chmod +x /bin/chmod

Since you were asking for hacks, I just thought of another one:

mv /bin/chmod /bin/chmod.orig
cp -a /bin/chown /bin/chmod

Now you have a /bin/chmod that's executable but it's actually chown (i.e. some other binary). Now all we have to do is overwrite it with the original binary.

dd if=/bin/chmod.orig of=/bin/chmod

It keeps the +x flag so you just stole the +x of another file for this one.

Of course, if all those alternative binaries (including the loader, perl, python, gcc et cetera) were chmod -x, fixing it from a rescue system may be the only option.

  • Couldn't you just use cat instead of dd? – jw013 Jul 22 '13 at 17:18
  • Yes... I ended up using dd because at first I thought it may need conv=notrunc or similar to prevent replacing the file entirely, but it does work with most commands that write files after all (including cat, cp, ...). – frostschutz Jul 22 '13 at 17:44
  • 1
    great hack, really clever – Kiwy Mar 14 '14 at 8:49
  • 2
    +1, but why tamper with an existing system file at all?  Why not just do cp /bin/tr /bin/newchmod && cp /bin/chmod /bin/newchmod?  It seems that you don't need to specify any options to cp.  (I chose tr because it's a small file with a short, easy-to-type name; obviously, any executable file, such as /bin/chown, will work.) – G-Man Aug 8 '15 at 20:25
  • @G-Man, good point, might be less confusing that way for package managers that track files by timestamps or something. – frostschutz Aug 8 '15 at 20:51

Easy. What you can do is prepare some other executable file, and then cp chmod over it.

 $ cp /bin/ls chmod
 $ cp /bin/chmod .

The first cp creates a file called chmod with executable permissions, but which is really the ls executable. The second cp populates this file with the binary code of chmod, while preserving the execute permissions of the target file (because we did not specify any options to carry over permissions of the source file).

Another approach is to use the install utility which is a glorified copying program which can set permissions in one step. (See the -m argument.)

$ install -m a+x /bin/chmod .
$ ./chmod # executes

The install program isn't part of the Unix specification, but it is found in BSD, and in GNU Coreutils. Both support the -m option with chmod-like syntax, both symbolic and octal.

  • I think this is the simplest and neatest solution here, tested on my Cygwin too, have an upboat. – sundar Jul 22 '13 at 17:32
  • 8
    Just make sure you're not inside /bin when you do this, or the first step will overwrite the real chmod. – Matthew Crumley Jul 22 '13 at 17:57
  • 1
    +1 for install. So many solutions. :) – frostschutz Jul 23 '13 at 9:52
  • Best solution, all you need to know is that cp doesn't necessarily copy file attributes, it can copy only the content (but you can tell it to copy the attributes too). – MichalH Aug 8 '15 at 20:21

First things off the top of my head,

  • Boot from any other source (network book, cd, etc.) and use the chmod on that source to set the permissions.
  • Write a tiny C program to change the permissions.
  • Write a perl script (ruby, php, python, etc.) to change the permissions

/usr/bin/chmod is just making a system call to change the permissions, you can make that system call yourself in any number of ways.

This is hackier than any of the other answers (with the possible exception of the answer, which is a really clever hack), but may be more adaptable to other problems (especially if you fall into a time vortex and travel back to a Land Before Perl™).

  1. For safety, copy /bin/chmod to a safe place:

    cp /bin/chmod .
  2. Do echo chmod | cpio -oc > chmod.cpio
    This creates a cpio archive (not unlike a tar archive) containing chmod.  The -c option specifies that the file metadata is written in hex (i.e., in ASCII).

  3. vi chmod.cpio
    You’ll see something like


    followed by a mixture of control characters, standard ASCII characters, and extended ASCII characters.  Note that 0644 = 0x1A4 and 0755 = 0x1ED.  Find the 1A4 (starting at the 20th character position) and change it to 1ED.  Save and exit.

    • If you have a hex edit or something similar, it might be better to use that to avoid the risk that vi will contaminate the binary executable code in the file.  But I tried this with vi, and it seemed to work OK.
  4. cpio -i < chmod.cpio
    You now have a copy of chmod in your directory, protected 755.

Other archive programs may have similar capabilities.  In particular, cpio is old and may be deprecated; if you don’t have it, look for pax.

  • 1
    This made me chuckle, nice little hack – sleepycal Dec 10 '15 at 19:30

Or even easier - use setfacl which is often present on modern systems:

  1. Set executable permission for user:

    # setfacl -m u::rx /bin/chmod
  2. Now use the now executable binary to set its own permissions back to the original ones (i.e. for all users)

    # chmod +x /bin/chmod
  3. Drop extended ACL entries - optional, since there might have been a good reason why they were there in the first place.

    # setfacl -b /bin/chmod

Note, that setfacl can be used on a filesystem that doesn't support ACLs as well.

  • 1
    this is a cool trick, but you may want to explain what you are doing, and how it works. – hildred Jan 27 '15 at 16:59

rsync can also be used to change the file's permissions

# rsync /bin/chmod /tmp/chmod --chmod=ugo+x
# ls -al /tmp/chmod
  -rwxr-x--- 1 root root 52000 Sep  6 15:13 /tmp/chmod

Pipe the contents of chmod into an already executable file

cp /usr/bin/executable_file ~/executable_file
cat /usr/bin/chmod > ~/executable_file
~/executable_file +x

Not the answer you're looking for? Browse other questions tagged or ask your own question.