Linux chmod Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux chmod command and its practical applications in managing file permissions. We will start by understanding the fundamental concepts of file permissions in Linux, including the different permission categories (owner, group, and others) and permission types (read, write, and execute). We will then learn how to use the chmod command to change the permissions of files and directories, both individually and recursively. This lab aims to provide you with a comprehensive understanding of file permissions management in the Linux operating system.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") linux/BasicFileOperationsGroup -.-> linux/chown("`Ownership Changing`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") subgraph Lab Skills linux/ls -.-> lab-422598{{"`Linux chmod Command with Practical Examples`"}} linux/touch -.-> lab-422598{{"`Linux chmod Command with Practical Examples`"}} linux/chown -.-> lab-422598{{"`Linux chmod Command with Practical Examples`"}} linux/chmod -.-> lab-422598{{"`Linux chmod Command with Practical Examples`"}} end

Understanding File Permissions in Linux

In this step, we will explore the fundamental concepts of file permissions in the Linux operating system. File permissions determine who can access, modify, or execute a file or directory.

In Linux, each file and directory has three main permission categories:

  1. Owner: The user who created the file or directory.
  2. Group: The group that the owner belongs to.
  3. Others: All other users on the system.

Each of these categories has three permission types:

  1. Read (r): Allows the user to view the contents of the file or list the files in a directory.
  2. Write (w): Allows the user to modify the contents of the file or create/delete files in a directory.
  3. Execute (x): Allows the user to run the file as a program or access the contents of a directory.

You can view the permissions of a file or directory using the ls -l command. The output will show the permissions in the following format:

-rw-r--r-- 1 labex labex 0 Apr 24 12:34 example.txt

The first 10 characters represent the file permissions:

  • The first character indicates the file type (- for regular file, d for directory).
  • The next 3 characters represent the owner's permissions.
  • The next 3 characters represent the group's permissions.
  • The final 3 characters represent the permissions for others.

In the example above, the file example.txt has the following permissions:

  • Owner (labex) has read and write permissions.
  • Group (labex) has read permissions.
  • Others have read permissions.

You can also use the stat command to view more detailed information about a file's permissions:

$ stat example.txt
  File: example.txt
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d	Inode: 131075      Links: 1
Access: (0644/-rw-r--r--)  Uid: (1000/labex)   Gid: (1000/labex)
Access: 2023-04-24 12:34:56.123456789 +0000
Modify: 2023-04-24 12:34:56.123456789 +0000
Change: 2023-04-24 12:34:56.123456789 +0000
 Birth: -

This provides additional details about the file, such as the user ID (UID) and group ID (GID) of the owner, as well as the access, modification, and change times.

Understanding file permissions is crucial for managing access to files and directories in a Linux system.

Changing File Permissions Using the chmod Command

In this step, we will learn how to use the chmod command to change the permissions of files and directories in Linux.

The chmod command allows you to modify the read, write, and execute permissions for the owner, group, and others. The syntax for the chmod command is:

chmod [options] mode file

Here, mode represents the new permissions you want to set. You can use either symbolic mode or numeric mode to change permissions.

Symbolic Mode:

  • u represents the owner
  • g represents the group
  • o represents others
  • a represents all (owner, group, and others)
  • + adds the specified permissions
  • - removes the specified permissions
  • = sets the specified permissions

For example, to give the owner read and write permissions, the group read permissions, and remove all permissions for others, you would use:

chmod u=rw,g=r,o-rwx example.txt

Numeric Mode:

  • Each permission (read, write, execute) is assigned a number: 4 for read, 2 for write, and 1 for execute.
  • The permissions for owner, group, and others are represented by a 3-digit number.

For example, to set the permissions to rw-r--r-- (owner has read and write, group has read, others have read), you would use:

chmod 644 example.txt

Let's try changing the permissions of a file using both symbolic and numeric modes:

## Create a new file
touch example.txt

## Change permissions using symbolic mode
chmod u=rw,g=r,o-rwx example.txt
ls -l example.txt
## Output: -rw-r-----

## Change permissions using numeric mode
chmod 644 example.txt
ls -l example.txt
## Output: -rw-r--r--

In the above example, we first create a new file example.txt, then use the chmod command to change the permissions. We verify the changes using the ls -l command.

Recursively Changing Permissions of Directories and Files

In this step, we will learn how to use the chmod command to recursively change the permissions of directories and all the files within them.

The -R (recursive) option of the chmod command allows you to apply the specified permissions to a directory and all its subdirectories and files.

Let's create a directory structure and change the permissions recursively:

## Create a directory structure
mkdir -p ~/project/documents/reports

## Change permissions recursively
chmod -R u=rwx,g=rx,o=r ~/project/documents

## Verify the permissions
ls -l ~/project
## Output:
## drwxrwxr-x 3 labex labex 4096 Apr 24 12:34 documents
ls -l ~/project/documents
## Output:
## drwxrwxr-x 2 labex labex 4096 Apr 24 12:34 reports

In the above example, we first create a directory structure with a parent directory documents and a subdirectory reports. Then, we use the chmod -R command to recursively set the permissions:

  • Owner (labex) has read, write, and execute permissions.
  • Group (labex) has read and execute permissions.
  • Others have read-only permissions.

We verify the permissions using the ls -l command, which shows that the permissions have been applied to both the parent directory and the subdirectory.

The recursive option is particularly useful when you need to change the permissions of an entire directory tree, ensuring that all files and subdirectories within the tree have the desired permissions.

Summary

In this lab, we explored the fundamental concepts of file permissions in the Linux operating system. We learned that each file and directory has three main permission categories: owner, group, and others, and each category has three permission types: read, write, and execute. We also discovered how to view the permissions of a file or directory using the ls -l and stat commands. Finally, we covered how to change file permissions using the chmod command, both for individual files and directories, as well as recursively for all files and subdirectories within a directory.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like