chmod command in Linux with examples

The chmod command in Linux is used to manage file permissions. It’s an essential command that pretty much every user will find the need to utilize at least every once in a while.

Linux file permissions involve read, write, and execute permissions. These permissions can be assigned to the file or directory by its owner, a group of users, or “other” users (users that are neither the owner nor part of the group).

In this tutorial, you’ll learn how to use the chmod command in Linux to manage file permissions across your system.

In this tutorial you will learn:

  • How to use the chmod command on Linux
chmod command in Linux with examples
chmod command in Linux with examples
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software chmod
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Frequently Used Options



Listing different file permissions for various files on a Linux system
Listing different file permissions for various files on a Linux system

If you’re brand new to Linux file permissions, you will want to check out our other guide for an introduction to Linux file permissions.

You can see the permissions of a directory or file by executing ls -l on it. Check out the diagram below for a breakdown on how to interpret the output.

Breakdown of Linux file permissions for a file
Breakdown of Linux file permissions for a file

chmod command in Linux Basic Examples

The chmod utility can change the permissions of your files and folders. You need to own the files in order to change them with chmod, unless you are using it as root or with sudo.. The following commands show how to use chmod in symbolic mode.

    1. chmod uses the u, g, and o options to change the permissions for the owning user, group, and others respectively. Take a look at how it works.
      $ chmod g+w somefile.txt
      

      The command above adds write permissions for the group on the file, somefile.txt.

    2. chmod can also remove permissions.
      $ chmod o-wx somefile.txt
      

That command removes write and execute permissions for other users.

  1. You don’t have to add or subtract to get the permissions that you want. You can also set the permissions equal to what you need.
    $ chmod w=rx somefile.txt
    
  2. There is also an a option to apply a change to all groups simultaneously. This command would give execute permissions to the owner, group, and all other users.
    $ chmod +x somefile.sh
    

Advanced Usage




Advanced usage of chmod involves using the command in absolute mode. In addition to using letters to represent permissions, Linux also has a numeric system. The system assigns a value to each permission. Add the numbers together to get the total permission value of the section.

r = 4
w = 2
x = 1

chmod command in Linux Advanced Examples

  1. To set the permissions of a file where the owner has full permissions, the group has read and write, and everyone else only has read, you can use the following chmod command.
    $ chmod 764 somefile.txt
    
  2. The numeric system is often used by applications and web hosting services because it is more concise than the letters. Take a look at this common example:
    $ chmod 755 something.php
    

    In many cases, you’d only want the owner to write the file, but web servers to be able to read and execute it.

Closing Thoughts

Once you get a solid grasp of Linux permissions, you can effectively control access to all files and directories on your system with the chmod command. You can improve your security and stop your users from making potentially harmful mistakes.