In this tutorial, you will learn how to change file / folder permissions and owners via command line on Linux / Unix systems. There are two basic commands that you can use in order to accomplish this task: chmod and chown.
What you’ll need
Before you begin this guide you’ll need the following:
- Access to the command line
Step 1 — Changing file and folder permissions via command line
chmod – This command is used to change the permissions of a file or folder. Basically, every file has three types of users who can interact with it:
|owner||It’s the user who created and owns a file / directory.|
|group||All users who are members of the same group.|
|others||All other users, on the system who are neither the owners nor members of the group.|
ls -l, can be used to see file permissions and owners. For example,
ls -l file1.txt would display:
-rwxr–rw- 1 user user 0 Jan 19 12:59 file1.txt
- “-rwxr–rw-“ – This part of the line displays permissions. There are 4 main letters which you will most commonly notice in this part: r,w,x,d. d means that the type of the file is directory, in our example, there is no such letter (it would be the first letter of the line, but currently it’s empty (has a “-” symbol, which basically means “no”). The letter x means the permission to execute a file / folder (we need this kind of permission in order to enter a folder). w letter means permission to write a file / folder (edit the file, delete the file and etc.). And the last letter is r, which means read. If we have reading permissions, we can read the contents of the file, but that’s it, no editing or executing it (for example, we can read the code of the script, but we can’t execute it).
- 1 – A number of hard links. Basically, a hard link is an additional name for an existing file.
- user user – This shows the owner and group owner of the file.
- 0 – This shows the size of the file.
- Jan 19 12:59 – Displays date of the last modification.
- file1.txt – Provides name of the file/folder.
So let’s get back to the command chmod. This command allows us to change the permissions of a file or folder. And we will teach you how to do that simply by adding numbers together. Every permission type has its own number:
- r (read) – 4
- w (write) – 2
- x (execute) – 1
So, for example, if we want to set the permissions of file1.txt to these:
-rwxr–rw- 1 user user 0 Jan 19 12:59 file1.txt
We would have to write this command:
chmod 746 file1.txt
Basically, every number in this command represents permissions for one of the user type (owner, group owner, and others). So the first number is 7, now using the explanation of number meanings above, the only way we can get a number 7 is by adding up numbers 4, 2 and 1 which is: 4+2+1=7. Which basically means ALL permissions (read, write and execute – rwx). This first number sets the permissions for the owner of the file. The second number is 4, which means the permission r (read) and this number gives reading permissions to the group owner. The third number is 6, using the number meanings above, the only way to get this number is by adding 4 and 2, which is 4+2=6. So we give others the permission to read (4) and write (2) the file. The third part (file1.txt) of the command is a file name, we write the name of the file to which we want to set these permissions.
Another example would be this:
chmod 777 file2.txt, this command would basically give ALL permissions for every type of user (owner, group and other).
Here is a list of the most common permissions for files:
|-rw——-||600||Owner can read and write.|
|-rw-r–r–||644||Owner can read and write, the group and others can read.|
|-rw-rw-rw-||666||Owner, group and others can read and write.|
|-rwx——||700||Owner can read, write and execute, group and others cannot do anything with the file.|
|-rwx–x–x||711||Owner can read, write and execute, the group and others can execute.|
|-rwxr-xr-x||755||Owner can read, write and execute, the group and others can read and execute.|
|-rwxrwxrwx||777||Owner, group and others can read, write and execute.|
The common permissions for directories:
|drwx——||700||Only owner can read and write in this directory.|
|drwxr-xr-x||755||Owner, group and others can read the directory, but only owner can change its contents.|
There are other ways to change permissions of the file using chmod command but our personal recommendation would be to learn one of them and use it every time (in this case, the numbering way). If you wish to learn more about the other ways to change permissions, you can read about it here.
Step 2 — Changing file and folder owners via command line
chown – This command is used to change the owners of a file or folder. The most basic syntax of this command is this:
chown [owner/group owner] [file name]
Basically, if we have a file “demo.txt” and we want to set the owner of the file to “jerry” and group owner to “clients”, we would use this command:
chown jerry:clients demo.txt
So as you can see, we separate the owner and group owner with a symbol “:” (colon). If we only wanted to change the owner of the file, we would use this:
chown jerry demo.txt
As you see, we simply left out the group owner and only typed in the new owner of the file, so in such case, the group owner will remain unchanged. Another similar example would be if we wanted to change the group owner of the file, the command would go like this:
chown :clients demo.txt
In this case, only the group owner would change to clients (the owner would remain unchanged).
Step 3 — Using additional options with chmod and chown commands
One of the main options which works with both commands is -R, which means recursive. This option allows you to change permissions / owners in the given folder and ALL other files and folders inside of that initial folder.
IMPORTANT! Be extra careful with this option, if used incorrectly, you may accidentally change permissions or owners of ALL files in your system, which may cause critical failure or a lot of work to change the permissions / owners back.
Other options for “chmod” and “chown”:
- “-f” – Silent, stealthy or simply force. Will not show most error messages.
- “-v” – Gives you a diagnostic of every file which was affected by the command.
- “-c” – Similar to -v, but will only provide information in case the changes were actually made.
In this tutorial, you have learned how to change permissions and owners of files and folders via command line. Having these skills should make management of your server easier. If you want to continue learning, check out these hand-picked tutorials: