Managing File Permissions with chmod and chown on macOS Tahoe
In macOS Tahoe, like other Unix-based systems, every file and directory has a set of permissions that determine who can read, write, or execute it. Managing these permissions via the Terminal is a critical skill for system administration and scripting.
Viewing Permissions
To see the permissions of files in your current directory, use the long-list format of the ls command:
ls -l
You will see output similar to this:
-rw-r--r-- 1 user staff 1024 Jan 1 12:00 document.txt
drwxr-xr-x 3 user staff 96 Jan 1 12:05 MyFolder
Decoding the String
The first column (e.g., -rw-r--r--) represents the permissions.
- Type: The first character indicates the file type.
-: Regular file.d: Directory.
- User (Owner): The next three characters (
rw-). - Group: The middle three characters (
r--). - Others: The last three characters (
r--).
The letters stand for:
- r: Read
- w: Write
- x: Execute (run a script or enter a directory)
Changing Permissions: chmod
The chmod (Change Mode) command modifies these access rights.
Symbolic Mode
This method uses letters to represent the changes.
- u: User
- g: Group
- o: Others
- a: All (everyone)
Examples:
- Make a script executable for the user:
chmod u+x myscript.sh - Remove write permission for others:
chmod o-w shared_file.txt - Give everyone read access:
chmod a+r public_doc.md
Numeric Mode
This method uses numbers to represent the permissions (4=Read, 2=Write, 1=Execute).
- 7 (4+2+1): Read, Write, Execute.
- 6 (4+2): Read, Write.
- 5 (4+1): Read, Execute.
- 4: Read only.
Common Examples:
- 755 (User: rwx, Group: r-x, Others: r-x): Standard for scripts and programs.
chmod 755 myscript.sh - 644 (User: rw-, Group: r--, Others: r--): Standard for text files.
chmod 644 document.txt - 600 (User: rw-, Group: ---, Others: ---): Private file (like SSH keys).
chmod 600 private_key.pem
Changing Ownership: chown
The chown (Change Owner) command changes the user or group that owns a file. You usually need sudo to change ownership.
Syntax:
sudo chown [user]:[group] [file]
Examples:
- Change the owner of a file to "john":
sudo chown john file.txt - Change owner to "john" and group to "staff":
sudo chown john:staff file.txt - Recursively change ownership of a folder and everything inside it:
sudo chown -R john:staff MyFolder/
For more on general terminal usage, refer to Getting Started with macOS Tahoe.