File permissions in Linux ensure that only authorized users can access or modify files. The traditional permission model uses rwx (read, write, execute) for the user, group, and others. But what if you need more fine-grained control? That’s where Access Control Lists (ACLs) come in. ACLs allow you to set specific permissions for individual users or groups. The getfacl command is a handy tool that lets you view these ACLs.

This guide will walk you through how to use getfacl with clear examples.

Introduction to the getfacl Command

The getfacl command is straightforward. It lets you view the ACLs of any file or directory. Here’s the basic syntax:

getfacl [OPTION] file_name

This command shows who has what permissions on a file or directory. Before you use ACLs, ensure your filesystem supports them. Most modern filesystems like ext4 and XFS do.

Let’s dive into some practical examples to understand how getfacl works.

Example 1: Viewing the ACLs of a File

To see the ACLs of a file, use the following command:

getfacl myfile.txt

Here’s what the output might look like:

# file: myfile.txt
# owner: vyom
# group: vyom
user::rw-
group::rw-
other::r--

In this example:

  • user::rw- shows the owner’s permissions.
  • group::rw- shows the group’s permissions.
  • other::r– shows permissions for all other users.

Example 2: Viewing the ACLs of a Directory

Directories can have default ACLs. These are inherited by new files created in the directory. To view the ACLs of a directory, run:

getfacl mydirectory

Output:

# file: mydirectory
# owner: vyom
# group: vyom
user::rwx
group::rwx
other::r-x

In this case, any new file in mydirectory will inherit the default ACLs, ensuring consistency.

If you want to list ACLs for all files in a directory, use the -R flag:

getfacl -R mydirectory

This command displays the ACLs of all files and subdirectories within mydirectory.

Example 3: Combining getfacl with Other Commands

You can combine getfacl with setfacl to manage and copy ACLs. For instance, if you want to copy ACLs from one file to another:

getfacl sourcefile | setfacl --set-file=- targetfile

This command ensures that targetfile has the same ACLs as sourcefile.

Conclusion

ACLs are a powerful tool in Linux for managing permissions beyond the traditional model. The getfacl command makes it easy to view and understand these permissions. Explore getfacl in your system, and see how it can simplify your permission management tasks on dedicated server hosting from Atlantic.Net!