Linux CHMOD Command

In Unix-like system, which includes Linux, there are sets of rules for each file, which defines who can access that file and how they can access that file. These rules are called permissions or modes.

That brings us to the chmod command, which stands for change mode.

Before using chmod, we recommend that you understand how to define and express permissions.

User Classes & Permissions

To start off, consider the following. Unix-like systems approach file permissions as follows:

  • Every file has an owner, which determines the file’s user class
  • Every file has a group, which determines the file’s group class
  • A user that is not an owner, and doesn’t belong to a group that has permissions upon a file, is referred to as other, and determines the other class

Every file on Linux has permissions assigned to every one of these classes, which determines what action is allowed to be taken on the file, by said classes.

The actions that these 3 classes are allowed to perform on a file are:

  • read – ability to view contents of a file
  • write – ability to modify contents of a file
  • execute – ability to run file as executable

Basically, a file’s permissions determine whether or not:

  • The owner can read, write or execute the file
  • The group can read, write or execute the file
  • Anyone else can read, write or execute the file

The Linux file permissions are displayed in two formats:

  • Symbolic Notation
  • Numeric Notation

File Permissions Symbolic Notation

The symbolic notation is a string of 10 characters. Example:

-rwxr-xr--
  • The first character represents the file type ( regular, directory, symbolic link etc)
  • The next 9 characters ( rwxr-x--x ) represent, the read, write and execute permissions for the owner, group, otherin that order.
    If an action is not permitted, a dash (-) is used.

To further explain this:

-rwxr-xr--
  1. -: The first character represents the type of file. In our case, - indicates that it’s a regular file. Here’s a quick summary of the Linux file types:
    • -: regular file
    • d: directory
    • c: character device file
    • b: block device file
    • s: local socket file
    • p: named pipe
    • l: symbolic link
  2. rwx: the next 3 characters represent the owner’s permissions. In our case, the owner has (r)ead, (w)rite and e(x)ecute permissions.
  3. r-x: characters 5 to 7 represent the group permissions. In our case, the group permissions are (r)ead and e(x)ecute.
  4. r--: the last 3 characters represent permissions for everyone else (others), and what we’ve got means that everyone else has only (r)ead permissions.

File Permissions Numeric Notation

The numeric notation of file permissions is represented by 3 digits, ranging from 0 to 7, each digit representing the permissions for owner, group, other – in that order. Each of those numbers is obtained
by summing the class permissions:

  • 0: means no permissions allowed
  • 1: means ability to execute the file
  • 2: means ability to write to the file
  • 4: means ability to read the file

To clarify, the meaning of each digit that chmod will use is:

  • 0: no permissions
  • 1: execute permissions
  • 2: write permissions
  • 3: 1+2 – execute + write permissions
  • 4: read permissions
  • 5: 1+4 – execute + read permissions
  • 6: 2+4 – write + read permissions
  • 7: 1+2+3 – execute + write + read permissions

Numeric Notation Examples

You have, no doubt, come across a few common file permissions in numeric notation that you might recognize:

  • chmod 644: 2+4 (write + read for owner), 4 (read for group), 4 (read for other)
  • chmod 755: 1+2+4 (execute + write + read for owner), 1+4 (execute + read for group), 1+4 (execute + read for other)
  • chmod 555: 1+2+4 (execute + write + read for everyone)

The CHMOD Command Syntax

Before learning how to use chmod, let’s examine the chmod command syntax:

chmod [OPTIONS] [PERMISSIONS] [FILE]

You can set permissions without specifying [OPTIONS]. We’ll cover options further in this guide.

[PERMISSIONS] are the permissions you want to set for the given [FILE]. You can set them using symbolic or numeric notation.

Using the CHMOD Command

CHMOD with Numeric Notation

A convenient way of setting permissions is by using numeric notation:

chmod 755 some_file.txt

CHMOD with Symbolic Notation

For others it’s more convenient to use symbolic notation. It’s also useful when changing permissions for particular classes.

Let’s see some examples of using chmod with symbolic notation:

chmod u=rwx,g=rx,o=rx some_file.txt

When setting the same permissions to more than one class, you can just combine them. Earlier we set the same permissions to group(g) and others(o).

chmod u=rwx,go=rx some_file.txt

What’s great about the symbolic notation is that it allows you to add/remove permissions for a particular action on a particular class.

Say the owner doesn’t have execute permissions on a file. Simply set the permission as for the owner as follows:

chmod u+x some_file.txt

Maybe you’d like to remove write and execute permissions for other:

chmod o-wx some_file.txt

Additionally, you can use a to add/remove permissions for (a)ll users:

chmod a+w some_file.txt

CHMOD Options

-c, –changesGives verbose output when a change to permissions is actually made.
-f, –silent, –quietQuiet mode. Suppress most error messages.
-v, –verboseVerbose mode. Outputs a diagnostic message for every file processed.
–no-preserve-rootDo not treat the root ( / ) directory in any special way, which is the default setting.
–preserve-rootDo not apply recursively to /
–reference=RFILESet permissions to match those of file RFILE, ignoring any specified MODE.
-R, –recursiveChange files and directory recursively.
–helpDisplay chmod help message.
–versionDisplay chmod version

CHMOD Recursively

A common option used is to set permissions to all files in a particular directory, in other words, recursively. To do this we use the R option, as follows:

chmod -R 755 sample_folder

Conclusion

The chmod command can be confusing at first, but by taking a few minutes to understand it goes a long way and everything starts to make a lot of sense.

Hopefully this guide has helped you with this. If you’ve got any questions, issues or something to add, then please get in touch and we’ll get back to you as soon as possible.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
× Dracula Servers

Subscribe to DraculaHosting and get exclusive content and discounts on VPS services.