In this guide we will teach you how to change the file / folder permissions and owners using a command line on Linux / Unix systems. There are two simple commands which you could use to accomplish this task. Chmod and chown.
For this guide you will require the following:
– Access to the command line
Step 1 — Changing file and folder permissions via command line
Chmod – This command is made to change the permissions of a file or a folder. Simply put, each file has three types of users who can interact with it:
- Owner – The user who made and owns the file / directory.
- Group – Every user who is also a member of the group.
- Others – Every other user on the system who is neither a member or an owner of the group.
The command ls -1 could be used to view file permissions and owners. For example, 1s and -1 file1.txt will display:
-rwxr–rw- 1 user user 0 Jan 19 12:59 file1.txt
“-rwxr–rw-“ – This part of the line shows permissions. There are 4 main letters which you will usually see in this part: r,w,x,d. d means that the type of the file is a directory.
1 – A number of hard links, a hard link is just an extra name for a file which already exists.
User user – This help you view who is the owner and group owner of the file.
– 0 This is going to show how big is the file.
Jan 19 12:59 – This will be the date of the last edit.
File1.txt – shows the name of the file / folder.
Let’s return to the command chmod. The command will grant us the ability to change permissions of a file or a folder. Then we will show you how to do that by simply appending numbers together. Each permission type will have its own n umber:
- r (read) – 4
- w (write) – 2
- x (execute) – 1
For example, if we’d like to set the permissions of file1.txt to those:
-rwxr–rw- 1 user user 0 Jan 19 12:59 file1.txt
We’d have to type this command:
chmod 746 file1.txt
Every number in the command will represent a permission for one of the user type, such as owner, group owner, and others.
A different example would be chmod 777 file2.txt, which will basically give every permission for every type of user (owner, group and other).
This is a list of the most common permissions for files:
|1. -rw——-||600||Owner can read and write.|
|2. -rw-r–r–||644||Owner can read and write, the group and others can read.|
|3. -rw-rw-rw-||666||Owner, group and others can read and write.|
|4. -rwx——||700||Owner can read, write and execute, group and others cannot do anything with the file.|
|5. -rwx–x–x||711||Owner can read, write and execute, the group and others can execute.|
|6. -rwxr-xr-x||755||Owner can read, write and execute, the group and others can read and execute.|
|7. -rwxrwxrwx||777||Owner, group and others can read, write and execute.|
The usual 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.|
You have plenty of ways to change permissions of the file using the chmod command, however our personal suggestion is to learn one of them and use it each time (in this situation, the numbering way).
Step 2 — Changing file and folder owners via command line
Chown – This command is made to change the owners of a file or a folder. The most basic syntax of this command is this:
chown [owner/group owner] [file name]
Usually, if we have a file called “demo.txt” and we’d like to set the owner of the file to “jerry” and group owner to “clients”, we’d use the command below:
chown jerry:clients demo.txt
As you can see, we separate the owner and group owner using a symbol “:” (colon). If our goal was to only change the owner of the file, we could use the command below:
chown jerry demo.txt
As shown in the command above, we just leave out the group owner and only typed in the new owner of the file, so in that case, the group owner will stay unchanged. A different example will be if we wanted to change the group owner of the file, the command will be like this:
chown :clients demo.txt
In our case, just the group owner would be swapped to clients (the owner would stay unchanged).
Step 3 — Using additional options with chmod and chown commands
There’s one main way which should work with both commands, it is –R (stands for recursive). This option will grant you the option to replace permissions or owners in the given directory and ALL other files and folders inside that initial directory.
Different options for “chmod” and “chown”:
- “-f” – Silent, stealthy or simply force. Is not going to show most error messages.
- “-v” – Will provide you with a diagnostic of every file which was affected by the command.
- “-c” – Similar to -v, however it will only provide information in case the changes were actually mad
In this guide we have taught you how to change permissions and owners of files and folders with a command line. Using those skills will make management of your server / VPS simpler.