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
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
|Conventions||# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Frequently Used Options
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.
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.
- chmod uses the
ooptions 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,
- chmod can also remove permissions.
$ chmod o-wx somefile.txt
- chmod uses the
That command removes write and execute permissions for other users.
- 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
- There is also an
aoption 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 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
- 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
- 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.
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.