How to Restore Execute Permissions After Accidentally Running “chmod -x chmod” on Linux


1 views

Many Linux administrators and developers have faced this situation at least once: you accidentally remove execute permissions from the chmod command itself. The command chmod -x chmod essentially makes the permission-changing tool unable to execute, creating a paradox where you can't use chmod to fix chmod.

The problem occurs because:

1. chmod is typically located at /bin/chmod
2. The -x flag removes execute permission for all users
3. Without execute permissions, the binary cannot run
4. This affects both the regular user and root

Here are several methods to restore execute permissions:

Method 1: Using the cp Command

Copy the binary while preserving permissions from another system:

cp /bin/chmod /bin/chmod.bak    # First make a backup
scp user@other-server:/bin/chmod /bin/chmod.new
install -m 755 /bin/chmod.new /bin/chmod

Method 2: Using the install Command

If you have another executable with correct permissions:

install -m 755 /bin/ls /bin/chmod.tmp
mv /bin/chmod.tmp /bin/chmod

Method 3: Using Perl

For systems with Perl installed:

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

Method 4: Using Python

Python provides another alternative:

python3 -c 'import os; os.chmod("/bin/chmod", 0o755)'

To avoid this situation in the future:

  • Always test chmod commands with --preserve-root first
  • Consider using chmod u+x instead of chmod +x for more specific changes
  • Keep a backup copy of critical binaries in a safe location

On some modern Linux distributions with security features like SELinux, you might need additional steps:

restorecon -v /bin/chmod

Every Linux/Unix admin has done it at least once: accidentally removing execute permissions from critical system binaries. When you run chmod -x chmod, you've essentially disabled the very tool needed to fix permission issues. Here's how to recover without rebooting.

The command chmod -x /bin/chmod (or equivalent path) makes the chmod binary non-executable. This affects:

  • Direct chmod invocations
  • Scripts relying on chmod
  • Package managers that might need to repair permissions

Method 1: Using the cp Trick

Most systems keep a copy of core utilities in memory when they're running:

# First verify the original path
which chmod
# Typically outputs /bin/chmod or /usr/bin/chmod

# Use cp while the original is still in memory
cp --preserve=all /bin/chmod /tmp/chmod-temp
/tmp/chmod-temp +x /bin/chmod

Method 2: Package Manager Rescue

For systems with package managers:

# Debian/Ubuntu
sudo dpkg-reconfigure coreutils

# RHEL/CentOS
sudo rpm --setperms coreutils

# Arch Linux
sudo pacman -Qo /bin/chmod
sudo pacman -S coreutils --overwrite /bin/chmod

Method 3: Using Perl One-Liner

When other binaries are still functional:

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

Method 4: BusyBox Fallback

If available on your system:

busybox chmod +x /bin/chmod

To avoid repeating this mistake:

  • Use absolute paths when modifying system binaries
  • Create aliases for dangerous operations: alias chmod='chmod --preserve-root'
  • Consider using chattr +i on critical binaries in production

If you've lost execute permissions on multiple critical binaries:

  1. Boot from live USB
  2. Mount your root partition
  3. Run: chroot /mnt /bin/bash
  4. Reinstall core packages or fix permissions manually