Understanding Linux Permissions: A Guide to umask, chmod, and setfacl

am
IT Security In Plain English
3 min readMar 12, 2024
source :unsplash

Linux's umask (user file-creation mode mask) is a fundamental command for users and system administrators seeking to manage file permissions in a flexible and secure manner. This article provides an in-depth exploration of umask, alongside practical examples, commands, shell scripts, and its usage. Furthermore, I 'll compare umask with chmod and setfacl, highlighting their differences and use cases through various examples.

Understanding umask

The umask command in Linux is used to determine the default permissions set for newly created files and directories. It effectively masks out permissions that should not be granted by default. Permissions in Linux are denoted by three types: read (r), write (w), and execute (x), each for three categories of users: owner, group, and others.

The umask value is subtracted from the system's default permissions to calculate the permissions for a new file or directory. By default, Linux sets the permissions for new files as 666 (read and write for owner, group, and others) and for directories as 777 (read, write, and execute for owner, group, and others).

Syntax and Usage

The basic syntax of the umask command is:

umask [OPTION]... [MODE]
  • Without any arguments, umask displays the current mask value.
  • Setting a new umask value can be done by simply typing umask followed by the desired mode.

Examples

  1. Viewing the Current umask Value
$ umask
0022
  1. Setting a New umask Value
$ umask 027
$ umask
0027

This sets a new umask which results in new directories being created with 750 permissions (rwxr-x---) and new files with 640 permissions (rw-r-----).

Practical Application: Shell Script

Here's a simple shell script that creates a directory and file with custom permissions, determined by the umask:

#!/bin/bash

# Setting umask
umask 0077

# Creating a directory and file
mkdir my_secure_dir
touch my_secure_dir/my_secure_file

# Displaying the permissions
ls -ld my_secure_dir my_secure_dir/my_secure_file

This script sets a umask of 0077, creating a directory and a file within it that are only accessible by the owner.

Comparison with chmod and setfacl

While umask sets default permissions for new files and directories, chmod and setfacl offer more granular control for modifying permissions of existing files and directories.

chmod

chmod (change mode) alters the file mode bits of each given file according to mode, which can be either a symbolic representation of changes to make, or an octal number representing the bit pattern for the new mode bits.

Example: Changing Permissions with chmod

# Creating a file
touch example_file

# Setting read and write permissions for the owner, and read for others
chmod 644 example_file

# Verifying the permissions
ls -l example_file

setfacl

setfacl allows for the management of file access control lists (ACLs), providing a more sophisticated mechanism for specifying permissions than the traditional Unix user/group/others model.

Example: Setting ACLs with setfacl

# Creating a file
touch example_file

# Giving user 'john' read access to the file
setfacl -m u:john:r example_file

# Checking the ACL
getfacl example_file

Main Differences

  • Scope: umask sets the default permissions for new files and directories. chmod changes permissions of existing files and directories. setfacl modifies access control lists to define detailed access rights for multiple users and groups.
  • Flexibility: chmod and setfacl offer more flexibility for changing permissions after file creation. umask is more about setting a secure default state.
  • Use Cases: Use umask for a secure baseline. Use chmod for traditional permission changes. setfacl is ideal when you need complex permissions for multiple users or groups beyond the owner, group, and others.

Understanding umask, chmod, and setfacl is crucial for managing file permissions effectively in Linux. While umask provides a secure default for new files and directories, chmod and setfacl offer granular control for existing resources. By mastering these commands, you can ensure the security and proper access controls are in place for their files and directories.

--

--

am
IT Security In Plain English

Unapologetically Nerdy. Privacy | Encryption | Digital Rights | FOSS | Video Tech | Security | GNU/Linux. Check out https://git.aloke.tech