The chmod vs. umask commands

chmod vs. umask

In this article, we will compare chmod vs. umask, the differences between them as well as how we can use them in our Linux environment.

Table of Contents

Introduction

The chmod and umask utilities are used to manage file permissions on a Linux machine. However, they both serve different purposes and we will explore that further by reviewing some examples, demonstrating how both commands are used.

chmod vs umask: File permissions

Photo by Stephan Muller from Pexels

The chmod command

Simply put, the change mode or chmod (abbreviated) utility is used to change the access permissions and special mode flags (e.g. setuids, setgids, and sticky-bits) of file systems objects such as, files and directories. It is bundled as part of the GNU coreutils package and written by David Mackenzie and Jim Meyering.

The chmod command is used to change the file and directory permissions of a file, whereas the umask command is used to set the default permissions for new files and directories.

The breakdown

The chmod command controls who can access a file, write to a file, and if the file is a script or executable, execute a file. Access is typically broken down into three classes. The user or owner of a file, the group that can access the file, and everyone else or others. They are represented in symbolic format as u=user, g=group, o=others, and a=all (also known as ugo).

Chmod vs Umask?

The syntax for chmod is “chmod [permissions] [file/directory]“, whereas the syntax for umask is “umask [permissions]”.

Examples of chmod syntax

To give the owner of a file read, write, and execute permissions, the group read and execute permissions, and others no permissions, you would use the following command:

chmod 750 filename.txt

The first number (7) sets the permissions for the owner of the file, the second number (5) sets the permissions for the group, and the third number (0) sets the permissions for others.

To give all users read and write permissions for a directory and its contents, you would use the following command:

chmod -R 666 directoryname

The “-R” option tells chmod to apply the permissions recursively to all files and directories within the specified directory.

Examples of umask syntax

To set the default permissions for new files to be created to read and write for the owner, and read only for the group and others, you would use the following command:

umask 022

This will subtract 022 from the default permissions of 666 (i.e., 666-022=644).

To set the default permissions for new directories to be created to read, write, and execute for the owner, and read and execute for the group and others, you would use the following command:

umask 002

This will subtract 002 from the default permissions of 777 (i.e., 777-002=775).

Note that the umask command is not used to directly set permissions on existing files or directories; it only sets the default permissions for new files and directories to be created.

Chmod vs Umask

Arguments and Outputs

  • chmod takes two arguments: the permissions to be set and the file or directory to which they should be applied. In contrast, umask takes only one argument: the permissions to be applied as the default for new files and directories.
  • When you use the chmod command, you will receive a success message indicating that the permissions have been updated. On the other hand, the umask command does not provide any output unless you use it with other commands like ls.

Scope and Execution

  • Permissions: chmod sets permissions using absolute values, such as “chmod 755 file”, which would set the permissions to read, write, and execute for the owner, and read and execute for group and others. In contrast, umask sets permissions using octal values, such as “umask 022”, which would set default permissions to 755 (i.e., 777-022=755).

  • Scope: The chmod command applies permissions to a single file or directory, while umask applies permissions to all new files and directories created in a particular directory.

  • Execution: The chmod command is executed on a file or directory to which you have access, whereas the umask command is executed in the shell to set the default permissions for new files and directories.

Ownership, Relationships

  • Owner and Group: chmod can be used to change the owner and group of a file or directory, whereas umask only affects permissions and not ownership.

  • Time of Application: chmod applies permissions immediately, while umask sets the default permissions for new files and directories to be created in the future.

  • Inverse Relationship: chmod and umask have an inverse relationship, meaning that the permissions set by umask are subtracted from the default permissions set by chmod. This means that if the default permissions for a file are set to 777 and the umask is set to 022, the actual permissions for the file will be 755 (777-022=755).

Conclusion

We have successfully reviewed the chmod and umask utilities, the differences between them, and how they are utilized in a Linux environment. Was this article helpful to you? If so, leave us a comment. We’d love to hear from you!

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *