Home Linux troubleshooting Restore Executable Permission To Chmod Command In Linux

Restore Executable Permission To Chmod Command In Linux

By sk
Published: Updated: 11.7K views

I came across an interesting discussion thread on Twitter. It is about an Interview question. The question is - What do you do if the executable permission of Chmod command is removed accidentally? As you may know, the Chmod (stands for Change mode) command is used to set or change the access permissions of a file or directory in Unix-like systems. So if the executable permission of chmod is removed, you can't assign the permissions to any programs, including the chmod command itself. You are sitting in an interview panel and the interviewer just asked this question. So, how do you restore executable permission to chmod command in Linux?

"I will copy the chmod binary from other systems?", you might reply. Well, you are given only one system. So you can't copy the chmod binary from other systems. You may reply again, "I'll set the execute bit from perl.". The interviewer says "There are no interpreters on your machine.". There is also no Internet/Phone connection, hence you can't contact anyone for help either. In such a dire situation, how do you restore the executable permission of Chmod command? If you already know the answer, a big thumbs up to your intelligence. If you are not a critical thinker (like me!), please read on.

Warning: Please don't try to remove execute permission of chmod command in a production system. Try this in a VM or test machine. It is just for educational purpose.

What do you do if you accidentally removed the executable permission of Chmod command?

Let us say you accidentally run the following command:

# chmod -x $(which chmod)

Here, the -x flag will remove the executable permission of the given program. In our case, it is chmod.

You are very curious and you really don't know what will this command do. Now you lost the executable permission of chmod command. There is no way to restore the chmod executable permission again. If you try to set permission the following command:

# chmod +x $(which chmod)

You will get the following error message:

bash: /usr/bin/chmod: Permission denied
Remove executable permission of chmod command in linux
Remove executable permission of chmod command in linux

Even if you log out and try from other sudo users, you can't assign permissions using chmod command:

$ sudo chmod +x $(which chmod)
sudo: chmod: command not found

How do you restore the chmod permissions back to its original? Well, it is easier than you think.

Restore Executable Permission to Chmod Command in Linux

There are a few ways to restore the execute permission to chmod. Here, I have given 7 methods.

Method 1 - Copy contents of chmod binary to other working binaries

Please remember we removed the executable permission of chmod command only, but not other commands' permission. Right? Yes! The other commands still have the executable permission. So, we can fix this issue by copying the contents of the chmod command to any other executable command, for example "mkdir", and restore the chmod executable permission back to its original.

Before copying, let us take a backup of mkdir command using command:

# cp /usr/bin/mkdir /usr/bin/mkdir.bak

Next copy the contents of /usr/bin/chmod to /usr/chmod/mkdir using "cat" command:

# cat /usr/bin/chmod > /usr/bin/mkdir

Now, the mkdir command has became as chmod command (because we copied the contents of chmod to mkdir binary).

And then, restore the executable permission of actual chmod binary using command:

# mkdir +x /usr/bin/chmod

Alternatively, you can copy the contents of mkdir back to the chmod binary:

# mv /usr/bin/mkdir /usr/bin/chmod

Finally, restore the original mkdir command from backup:

# mv /usr/bin/mkdir.bak /usr/bin/mkdir
Restore executable permission to chmod command in Linux
Restore executable permission to chmod command in Linux

Done! We successfully restored the execute permission to chmod command.

Check if the executable permissions are restored with "ls" command:

# ls -l /usr/bin/chmod

Or,

# ls -l $(which chmod)
-r-xr-xr-x 1 root root 63864 May 27 07:16 /usr/bin/chmod

Note the letters "x" in the above output. It indicates execute permission.

You can also verify it by assigning executable permissions to any other programs with chmod like below:

# chmod +x ostechnix.sh
# ls -l
total 0
-rwxr-xr-x 1 root root 0 May 27 07:43 ostechnix.sh

This is the easiest method since you don't have to memorize anything. We simply copied the contents of chmod to other commands using cat command and then restored the execute permission.

I tested it in Ubuntu 20.04 server edition. In other Linux distributions, the chmod binary file may available under /bin/ directory. You can find the chmod binary location using the following command:

$ which chmod

This is, however, not only the way. I did a quick Google search and found a few more methods. You may need to memorize the following commands.

Method 2 - Using cp command

This another way to restore executable permission to chmod.

In this method, we use "cp" command to copy the attributes:

# cp --attributes-only --preserve=mode /proc/self/exe /usr/bin/chmod

Method 3 - using GNU linker/loader (ld)

In this method, you can run the "ld" loader directly, and pass it the command you want to run like below:

# /lib64/ld-linux-x86-64.so.2 /usr/bin/chmod +x /usr/bin/chmod

If you use 32 bit systems, the command would be:

$ /lib/ld-linux.so /usr/bin/chmod +x /usr/bin/chmod

The path to loader might very depending upon the Linux distribution you use. The above commands are for Ubuntu and its derivatives. In other distros, the path might be different:

# /lib/libc.musl-x86_64.so.1 /bin/chmod +x /bin/chmod

Method 4 - Using setfacl

We can restore chmod executable permissions using "setfacl" command.

First, modify ACL of a chmod binary file for user with read and execute permissions:

# setfacl -m u::rx /usr/bin/chmod

Then, restore chmod permissions back to its original ones:

# chmod +x $(which chmod)

Finally, remove all ACL entries of chmod. This is optional.

# setfacl -b /usr/bin/chmod

If setfacl command is not available, install "acl" package. For example, run the following command to install acl on Debian, Ubuntu and its derivatives:

# apt install acl

Method 5 - Using rsync

Rsync is not just for copy or backup files and directories between systems. It can also be used to assign permissions for files as well.

First, let us copy the chmod binary file to /tmp location while assigning executable permissions to all, i.e. users, groups and others:

# rsync /usr/bin/chmod /tmp/chmod --chmod=ugo+x

Next, check if the executable permission is assigned to chmod:

$ ls -l /tmp/chmod

Sample output:

-r-xr-xr-x 1 root root 63864 May 27 10:01 /tmp/chmod

If the permissions are OK, simply overwrite the original file with /tmp/chmod file:

# mv /tmp/chmod /usr/bin

Method 6 - Using Busybox

Some recent Linux distributions have "Busybox" installed by default. From the Busybox man page,

BusyBox combines tiny versions of many common UNIX utilities into a single small executable. It provides minimalist replacements for most of the utilities you usually find in GNU coreutils, util-linux, etc. The utilities in BusyBox generally have fewer options than their full-featured GNU cousins; however, the options that are included provide the expected functionality and behave very much like their GNU counterparts.

To restore Chmod execute permission with busybox, run:

# busybox chmod +x /usr/bin/chmod

Method 7 - Using Perl

Just in case Perl is available on your system, you can fix Chmod's execute permissions with command:

# perl -e 'chmod 0755, "/usr/bin/chmod"'

Conclusion

As you can see, this is a trivial problem which can be easily resolved by smart Linux users. If you're a budding or intermediate Linux user, you probably never figure it out. These kind of questions might be asked to check the interviewee's presence of mind and how he thinks out of the box. Thanks to the aforementioned Twitter thread, I learned something useful today. Hope it is useful to you too.

Related read:

You May Also Like

1 comment

s9p3r November 15, 2021 - 8:58 pm

install /user/bin/chmod ./new-chmod –mode 0755
new-chmod 0755 /usr/bin/chmod

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More