Macos – sudo chown fails with operation not permitted

command linemacosownershipunix

I have found similar questions here, but the answers provided to those questions do not resolve my problem.

If I do:

sudo chown <username> main.m

I get an operation not permitted error.

The general solution to prior errors was:

sudo chflags nouchg main.m

to clear a lock flag. However, after doing this, I still cannot chown the file. I have also tried:

chflags noschg main.m

in single user mode without any luck – I get an operation not permitted error.

If anyone has any thoughts, I would be interested.

Best Answer

Mac files can be protected in four three different ways that I'm aware of:

  1. Standard Unix ownerships and permissions like r/w/x for user/group/others that you see with ls -l and fix with chown(1) and chmod(1).
  2. File flags like uchg and schg that you see with ls -lO and fix with chflags(1).
  3. The old Finder metadata "Lock bit" that you see with GetFileInfo filename. You can also see the presence of the Finder metadata, but not its meaning, with ls -l@. You can fix it with SetFile(1). Never mind, this is mapped to the uchg flag now.
  4. Access Control Lists (ACLs) that you see with ls -le and fix with the ACL-related arguments to chmod(1).

So let's see what's up with your file by combining all those flags to ls:

ls -lO@e main.m

It sounds like you already know how to deal with #1 and #2.

If you see an ACL (#4), you can fix it with the ACL-related arguments to chmod(1).

If you're still stumped, update your Answer with the output from that combined ls command I suggested above, so we can see what's going on.