What the Error Code Means
The error chmod: changing permissions of '<file>': Operation not permitted (EPERM code) occurs when the chmod command cannot change the access attributes (permissions) of a file or directory. In Linux, this is a system error indicating that the operation is forbidden for the current user or by the filesystem.
The full error text typically looks like this:
chmod: changing permissions of 'example.txt': Operation not permitted
The error blocks the execution of the chmod command and prevents setting new permissions (e.g., 755, 644, or special bits like setuid).
Causes
- Insufficient permissions — you are not the file owner and not the superuser (root). Only the file owner or root can change its permissions.
- Filesystem with restrictions — the file is located on a partition mounted with options like
noexec(execution forbidden),nosuid(setuid/setgid forbidden), orro(read-only). This is typical for USB drives, network shares (NFS), or protected partitions. - Immutable or append-only attribute — the file is protected by
chattr +i(immutable) orchattr +a(append-only) flags. These flags block any changes, including permissions, even for root. - Incorrect use of setuid/setgid — attempting to set the
sbit (setuid/setgid) on:- a non-executable file (missing
xfor owner/group); - a directory (setuid/setgid on directories is ignored or forbidden).
- a non-executable file (missing
- Attempting to change permissions on a symbolic link — if you are not root,
chmodby default tries to change permissions on the link's target file, not the link itself. If the target file is not owned by you, EPERM will occur.
Method 1: Using sudo (if you are not the owner)
If the file belongs to another user (e.g., root), use sudo to run the command as the superuser.
- Check the file owner:
ls -l /path/to/file
Example output:-rw-r--r-- 1 root root 1024 Feb 17 10:00 example.txt
Here the owner isroot. - Run
chmodwithsudo:sudo chmod 644 /path/to/file
Or, to set setuid (if needed):sudo chmod u+s /path/to/executable_file
⚠️ Important: Use
sudoonly for trusted files. Changing permissions on system files can disrupt OS operation.
Method 2: Changing the file owner (if you have sudo rights)
If you are an administrator and want the user to be able to change permissions themselves, change the file's owner.
- Change the owner to the desired user (e.g.,
alex):sudo chown alex /path/to/file
You can also change the group simultaneously:sudo chown alex:developers /path/to/file - Now user
alexcan change permissions withoutsudo:chmod 755 /path/to/file
Method 3: Checking and modifying mount parameters
If the file is on an external drive (USB, network disk) or a separate partition, check how the filesystem is mounted.
- Find the file's mount point:
findmnt /path/to/file
Example output:TARGET SOURCE FSTYPE OPTIONS /mnt/usb /dev/sdb1 vfat rw,noexec,nosuid,nodev,relatime,uid=1000,gid=1000,... - If the
OPTIONScolumn containsnoexec,nosuid, orro(read-only), you need to remount the partition:sudo mount -o remount,rw,exec /dev/sdb1 /mnt/usb
For a permanent solution, edit/etc/fstaband removenoexec/nosuidfrom the options for the relevant partition.
Method 4: Removing immutable/append-only attributes
If the file is protected by chattr flags, remove them before changing permissions.
- Check the attributes:
lsattr /path/to/file
Example output:----i--------e-- /path/to/file
The letterimeansimmutable. - Remove the attribute (requires
sudo):sudo chattr -i /path/to/file
Forappend-only(a):sudo chattr -a /path/to/file - Now change the permissions:
sudo chmod 644 /path/to/file
💡 Tip: The
chattrflags are often used to protect critical system files (e.g.,/etc/passwd,/etc/shadow). Be careful when removing them.
Method 5: Checking setuid/setgid and executability
If the error occurs when setting special bits (s or S), check the file's executability.
- Ensure the file is executable (has
xfor the owner):ls -l /path/to/file
Example of correct output for setuid:-rwsr-xr-x 1 root root 12345 Feb 17 10:00 program
The lettersin the owner's position (instead ofx) indicates setuid. - If
xis missing, add it:sudo chmod u+x /path/to/file
Then set setuid:sudo chmod u+s /path/to/file - For setgid (the
sbit in the group), similarly:sudo chmod g+x /path/to/file sudo chmod g+s /path/to/file
Prevention
- Avoid changing permissions on system files without explicit need. Use
sudomindfully. - When working with external media, mount them with
rw,execoptions (if you need to execute files) and withoutnosuidif setuid is not required. - Do not set the
immutableattribute (chattr +i) on files that may require permission changes in the future. - Check executability before setting setuid/setgid. Remember: setuid/setgid require the file to be executable for the relevant category (owner/group).
- Regularly check file ownership, especially in directories used by multiple users. Use
ls -lfor auditing.