Access control list (ACL) Access control lists (ACLs) provide an additional layer of access control to files and directories stored in extended attributes on the filesystem. These ACLs are set and verified with the setfacl and getfacl Linux commands.
* '''ACL Entries''': Individual permissions associated with a resource, typically specifying a user or group and the permitted operations. * '''Resource''': The system resource to which the ACL is applied, such as a file, directory, or network service. * '''Permissions''': The operations that can be performed on the resource, such as read, write, execute, delete, etc.
* '''Granular Access Control''': Provides fine-grained control over who can access specific resources and what actions they can perform. * '''User and Group Permissions''': Allows setting permissions for individual users or groups of users. * '''Compatibility''': Supported by many operating systems and file systems. * '''Security''': Enhances security by allowing detailed specification of access permissions.
```bash setfacl -m u:username:rwx /path/to/file ```
```bash getfacl /path/to/file ```
```cmd icacls "C:\path\to\file" /grant username:(R,W) ```
```cmd icacls "C:\path\to\file" ```
```python import os import subprocess
def set_acl(path, user, permissions): subprocess.run(['setfacl', '-m', f'u:{user}:{permissions}', path], check=True)
def get_acl(path): result = subprocess.run(['getfacl', path], capture_output=True, text=True) return result.stdout
# Example usage file_path = '/path/to/file' set_acl(file_path, 'username', 'rwx') acl_info = get_acl(file_path) print(acl_info) ```
```java import java.io.IOException; import java.nio.file.*; import java.nio.file.attribute.AclEntry; import java.nio.file.attribute.AclEntryPermission; import java.nio.file.attribute.AclEntryType; import java.nio.file.attribute.UserPrincipal; import java.util.EnumSet; import java.util.List;
public class AclExample { public static void setAcl(String filePath, String userName, Setpermissions) throws IOException { Path path = Paths.get(filePath); UserPrincipal user = FileSystems.getDefault().getUserPrincipalLookupService().lookupPrincipalByName(userName); AclEntry entry = AclEntry.newBuilder() .setType(AclEntryType.ALLOW) .setPrincipal(user) .setPermissions(permissions) .build();
Listacl = Files.getFileAttributeView(path, AclFileAttributeView.class).getAcl(); acl.add(entry); Files.getFileAttributeView(path, AclFileAttributeView.class).setAcl(acl); }
public static ListgetAcl(String filePath) throws IOException { Path path = Paths.get(filePath); return Files.getFileAttributeView(path, AclFileAttributeView.class).getAcl(); }
public static void main(String[] args) throws IOException { String filePath = "/path/to/file"; String userName = "username"; Setpermissions = EnumSet.of(AclEntryPermission.READ_DATA, AclEntryPermission.WRITE_DATA, AclEntryPermission.EXECUTE);
setAcl(filePath, userName, permissions); Listacl = getAcl(filePath); for (AclEntry entry : acl) { System.out.println(entry); } } } ```