Mastering the macOS Command Line: Part 7— Working with Permissions

ElAmir Mansour
11 min readMay 5, 2024

--

In the world of macOS Zsh, understanding permissions is crucial for managing files and directories effectively. Whether you’re a seasoned developer or a newcomer to the command line, grasping the basics of permissions is essential for navigating the macOS environment securely. In this guide, we’ll delve into the fundamentals of permissions on macOS Zsh, providing you with the knowledge needed to wield its power confidently.

Example:

Viewing Permissions:

ls -l file.txt
  • Explanation: The ls -l command lists detailed information about a file, including its permissions. The output displays information such as the file type, permissions for the owner, group, and others, along with additional metadata.

Changing Permissions:

chmod u+r file.txt
  • Explanation: The chmod command is used to change permissions on a file. In this example, u+r grants the owner of the file (u) the read (r) permission, we will get deeper now .

Explanation:

  • Viewing Permissions: The ls -l command provides a detailed listing of a file's permissions. Each permission is represented by a character, where r indicates read permission, w indicates write permission, and x indicates execute permission. Additionally, a - signifies the absence of a permission.
  • Changing Permissions: With the chmod command, you can modify permissions by specifying the target user or group (u, g, o) followed by a symbol (+, -, =) and the desired permission (r, w, x).

Understanding permissions on macOS Zsh is essential for effectively managing files and directories. By mastering the basics of permissions, you gain greater control over your system, ensuring security and efficiency in your command line endeavors.

Understanding Read, Write, and Execute

Diving into the intricacies of macOS Zsh permissions is crucial for efficient file management and system security. This guide aims to provide a comprehensive understanding of permissions, focusing on the fundamental concepts of read, write, and execute. By mastering these basics, you’ll gain greater control over your command line environment, empowering you to navigate macOS Zsh with confidence.

  • Read: Someone with permission to read a file can open it and see what’s inside it. This means they can view the contents of the file but cannot modify it in any way.
  • Write: Write permission allows someone to modify an item or delete it. Users with write permission can make changes to the content of a file or directory, including adding, editing, or deleting data.
  • Execute: Execute permission determines whether a file can be run as a program or script. For a file, execute permission allows it to be executed or run, enabling it to perform its intended functions. For a directory, execute permission allows someone to list its contents, navigate through it, and access files or subdirectories within it.

On the command line, read permission is abbreviated with an r, write permission with a w, and execute permission with an x. These abbreviations are commonly used to denote the permissions associated with a file or directory when viewing its details using commands like ls -l. Understanding these permissions is essential for effectively managing file access and system security in macOS Zsh.

Understanding Permissions, Owner, and Group:

When navigating permissions in macOS Zsh, it’s essential to comprehend ownership, group settings, and permission structures. Let’s elucidate these concepts using the following examples:

drwxr--r--  15 elamir    admin  510 Aug 27 15:02 cars
-rw-r--r-- 2 root wheel 1024 Sep 02 11:34 mercedes
  • Permissions and Types: The initial character in the permissions string denotes the item type. In the example drwxr--r--, cars is a directory (d), while mercedes is a file (-).
  • Three Sets of Permissions: The remaining nine characters represent permissions for the user, group, and others. Each set consists of r (read), w (write), and x (execute) permissions, indicating access rights for each category of users.
  • Owner and Group: The user and group names (elamir and admin for cars, root and wheel for mercedes) indicate ownership and group affiliation respectively. The owner has primary control over the item, while the group may share certain access rights.

Directory or Not?:

The first character of the permissions string distinguishes between directories and files. A d signifies a directory, while a hyphen (-) denotes a file.

Three Permissions, Three Sets:

Permissions are organized into three sets for the user, group, and others, each arranged in the order of r, w, and x.

Example Permission Combinations:

  • rwxrwxrwx: All users (owner, group, and others) possess read, write, and execute permissions.
  • rwx------: Only the owner has full permissions, while the group and others are restricted.
  • rw-r--r--: The owner has read and write permissions, while the group and others can only read.

Understanding these permission structures is vital for effective file management and maintaining system security within macOS Zsh.

Owner and Group :

After the file’s permissions and a number (representing the number of links to the item), you’ll find two names: the file’s owner (user) and its group.

For example, in this item:

drwxr--r--  15 elamir    admin  510 Aug 27 15:02 cars

The owner is elamir, and the group is admin. In some cases, instead of names, you may see numbers (e.g., 501) representing the owner, group, or both.

What’s with the + and @ Characters?

Sometimes a file has an extra character at the end of the permissions string — either a + or an @.

  • The + symbol indicates that the item includes an ACL (access control list), which provides a more detailed way of specifying permissions than the traditional read, write, and execute for user, group, and others. To view the ACL settings for a file or directory, use ls -le.
  • The @ symbol indicates that the item includes extended attributes, which are additional metadata beyond the file’s contents, often used for specific macOS features like Gatekeeper. To view the types of extended attributes a file or directory contains, use ls -l@; to see the contents of those extended attributes, use xattr -l file.

Understanding, using, and modifying ACLs and extended attributes is beyond the scope of this explanation. To learn more about ACLs, you can refer to Apple’s article on “Access Control Lists” or use man chmod and scroll down to the topic “ACL Manipulation Options.” For further information on extended attributes, see the article "Show me your metadata" or refer to man xattr.

Understanding File Permissions on macOS:

When you create a file on your Mac, whether by saving, copying, moving, downloading, or any other method, you automatically become the owner (user) of that file.

By default, all users on a Mac have certain permissions within their home folders:

  • Read and Write Permissions: Users have the ability to read and write to files within their own home folders. Additionally, for directories, users also have execute permission, allowing them to navigate through and access the contents of directories.
  • Access to Shared Items: Users can read and execute shared items, such as those located in the /Applications folder. However, they cannot read or write to files, or view the contents of directories, owned by other users.

Your default group, and consequently the default group assigned to files in your home folder and new items you create, is determined by various factors, with the primary one being your user account type. You can find and adjust account types in System Preferences > Users & Groups (referred to as Accounts in earlier macOS versions). Typically, if you’re an administrator, your default group is set to admin; otherwise, it’s typically set to staff.

File Permissions with chmod on macOS:

File permissions in macOS are essential for controlling access to files and directories. The chmod command is a powerful tool that allows users to modify these permissions. There are two primary modes for using chmod: symbolic mode and absolute mode. Let's explore both methods with detailed examples, keeping in mind that the user is always elamir.

Using chmod Symbolic Mode:

Symbolic mode allows users to modify permissions using symbolic representations of the permissions (e.g., u for user, g for group, o for others, and r, w, x for read, write, and execute respectively) along with operators (+ for adding permissions, - for removing permissions, and = for setting permissions explicitly).

Example: Suppose we have a file named document.txt and we want to grant elamir read and write permissions, while also allowing the group and others to read the file.

chmod u+rw,go+r document.txt

Explanation:

  • u+rw: Grants the user (elamir) read and write permissions.
  • go+r: Grants the group and others read permission.

Using chmod Absolute Mode ( i prefer it ) :

Absolute mode allows users to set permissions directly using numeric values representing permissions. Each permission is assigned a numeric value: read (4), write (2), and execute (1). These values are then summed up to assign permissions for user, group, and others.

Example: Suppose we have a directory named project and we want to set permissions such that elamir has read, write, and execute permissions, the group has read and execute permissions, and others have no permissions.

chmod 750 project

Explanation:

  • 7 for the user (elamir): read (4) + write (2) + execute (1).
  • 5 for the group: read (4) + execute (1).
  • 0 for others: no permissions.

Understanding how to use chmod in symbolic and absolute modes empowers users to effectively manage file permissions in macOS. Whether it's granting specific access rights to users, groups, or others, chmod provides the flexibility needed to ensure proper security and access control over files and directories.

Changing File Owner or Group with chown:

In addition to modifying file permissions, macOS users can also change the owner or group of a file using the chown command. This command allows users to transfer ownership or change group affiliation for files and directories. Let's delve into how to perform these actions with detailed examples, keeping in mind that the user is always elamir.

Changing File Owner:

To change the owner of a file or directory, users can utilize the chown command followed by the new owner's username and the file or directory name.

Example:

Suppose we have a file named document.txt owned by another user, and we want to change its owner to elamir.

sudo chown elamir document.txt

Explanation:

  • sudo: The sudo command is used to execute chown with superuser privileges, as changing ownership requires administrative permissions.
  • elamir: Specifies the new owner of the file.
  • document.txt: Specifies the file whose owner will be changed.

Changing File Group:

To change the group affiliation of a file or directory, users can utilize the chown command followed by the new group's name and the file or directory name.

Example: Suppose we have a directory named project with its group affiliation set to another group, and we want to change it to the admin group.

sudo chown :admin project

Explanation:

  • sudo: Executes chown with superuser privileges.
  • :admin: Specifies the new group for the file or directory. The colon (:) indicates that only the group affiliation is being changed.
  • project: Specifies the directory whose group affiliation will be changed.

The chown command in macOS provides users with the ability to transfer ownership or change group affiliation for files and directories. By mastering chown, users can efficiently manage file ownership and group settings to ensure proper access control and security within the system.

Performing Actions as the Root User on macOS:

In macOS, the root user, also known as the superuser or administrator, has unrestricted access to the system and can perform any action. Operating as the root user grants elevated privileges, allowing users to execute commands and modify system files that are typically restricted to regular users. Let’s explore how to perform actions as the root user, along with considerations for security and system integrity.

Becoming the Root User:

To execute commands as the root user, users can utilize the sudo (superuser do) command, which temporarily elevates their privileges. The sudo command requires users to authenticate by entering their password.

Example:

Suppose we want to edit a system file, /etc/hosts, which typically requires root privileges.

sudo nano /etc/hosts
  • Explanation:
  • sudo: Executes the following command with superuser privileges.
  • nano /etc/hosts: Opens the /etc/hosts file in the Nano text editor for editing.

Performing Administrative Tasks:

As the root user, users can perform various administrative tasks, such as installing software, modifying system configurations, and managing user accounts.

Example: Suppose we want to install a package using the apt package manager, which requires root privileges.

sudo apt install package_name

Explanation:

  • sudo: Grants superuser privileges to execute the apt install command.
  • package_name: Specifies the name of the package to be installed.

Security Considerations:

Operating as the root user grants extensive control over the system, but it also carries inherent risks. Executing commands with root privileges should be done cautiously to avoid unintended consequences, such as modifying critical system files or compromising system security.

Utilizing sudo for Root Privileges

After logging in as an administrator on your macOS system, gaining access to root privileges for executing commands is straightforward. Simply prepend the command with sudo:

sudo command

Executing this command triggers a prompt for your administrator account password. Enter your password when prompted, noting that no characters (not even bullets or asterisks) will appear as you type. This is normal behavior.

Upon successful authentication, the shell processes the command as though it were issued by the root user, ensuring its execution, provided it was entered correctly.

Note: When using sudo for certain operations under Mojave or later, you might encounter a prompt to grant Terminal special permission in the Security & Privacy pane of System Preferences. Refer to "Special Permissions for Terminal and the Command Line" for detailed instructions.

If your Mac incorporates a Touch ID sensor, you can streamline authentication by using your fingerprint instead of typing your password. Instructions for setting this up are available in Dan Moren’s article on Six Colors.

Encountering a “permission denied” error while executing a command can often be resolved by retrying the command with sudo prefixed to it. For instance, if attempting to change a file's owner yields an "Operation not permitted" error:

chown bob file1

You can rectify this with:

sudo chown bob file1

Now that you’re familiar with sudo, you might enjoy the humorously geeky comic from xkcd titled "Sandwich."

As a handy tip, instead of retyping a failed command with sudo, you can leverage the shortcut sudo !!. This command essentially means "Repeat the previous command, but execute it as root." This shortcut is particularly useful for lengthy or intricate commands.

Important Notes and Precautions when Using sudo:

Before delving into the usage of sudo, it's crucial to understand and adhere to certain guidelines to ensure the safe and responsible management of system privileges. Here are some key considerations:

1. 5-Minute Rule: Once you authenticate with sudo by entering your password, you can issue additional sudo commands within a 5-minute window without being prompted for your password again. The timer resets each time you use sudo.

2. Great Power = Great Responsibility: With sudo, you gain extensive control over system operations, including the potential to cause irreversible damage to macOS. While newer macOS versions, such as El Capitan and Catalina, offer enhanced security features like System Integrity Protection and read-only system volumes, exercising caution remains paramount. Only use sudo when necessary and ensure you understand the implications of the commands you execute.

3. Stay for a While: If you anticipate the need to execute multiple commands with root privileges, you can streamline the process by switching to the root user’s shell account using sudo -s. This command prompts you to enter your password and then changes your command prompt from $ to #, indicating that all subsequent commands are executed as the root user.

4. Exercise Caution with sudo -s: While sudo -s offers convenience in executing commands with elevated privileges, it inherently increases the risk associated with sudo. It's essential to exercise extreme caution when using sudo -s, as it provides unfettered access to system resources. Once you've completed your tasks requiring root privileges, promptly exit the root shell using the exit command to revert to your regular user account.

Understanding the nuances and precautions associated with sudo usage is imperative for maintaining system integrity and security on macOS. By adhering to these guidelines and exercising caution, users can effectively leverage sudo while minimizing the risk of unintended consequences or system damage.

Thanks.

If you found this blog helpful or have any questions, feel free to reach out to me on social media:

--

--

ElAmir Mansour

🚀 Software Engineer & iOS Developer | Scrum Master 🕹 | Crafting Code & Content | Coffee enthusiast ☕️ | Simplifying Complexity, One Line at a Time 💻