Search code examples
macosrootmacportsrmmacos-mojave

root user can't delete file


I've enabled root user in my mac using the latest Mojave. I am trying to delete a folder with it but for some reason I get the error "Permission denied". I thought root could do anything on a unix system?

sh-3.2# whoami
root
sh-3.2# pwd
/opt/local/var/macports/home/Library
sh-3.2# ls -lhart
total 0
drwxr-xr-x  3 root  admin    96B Sep 24  2016 ..
drwxr-xr-x  3 root  admin    96B Sep 24  2016 .
drwxr-xr-x  2 root  admin    64B Oct 13 18:54 Preferences
sh-3.2# rm -rf Preferences/
rm: Preferences/: Operation not permitted
sh-3.2# whoami
root
sh-3.2#

Solution

  • It seems, that SIP protects the whole /opt/local/var/macports/home folder, as assumed above. It is not necessary to deactivate SIP to remove the folder however.

    This answer about MacPorts folder removal indicates that removing the "macports" user that this folder is the "home" of would allow root to delete the folder, so

    sudo /usr/bin/dscl . -delete "/Users/macports"
    sudo /usr/bin/dscl . -delete "/Groups/macports"
    

    should allow sudo rm -rf /opt/local/var/macports/home/Preferences to work as desired.

    If you do not want to remove the "macports" user, follow these steps to delete these folders using the terminal in Recovery Mode:

    • Shutdown your Mac
    • Boot your Mac and press immediately CMD + R at startup to start in Recovery Mode (you can release the keys when the Apple-logo shows up)
    • Open the Terminal app
    • cd /Volumes/Macintosh\ HD/opt (Your harddisk name maybe differ from "Macintosh HD")
    • rm -Rf local