In this tutorial, we will teach you how to change file or folder permissions and owners using a command line on Linux or Unix systems. There are two simple commands that can be used to accomplish this task; they are Chmod and Chown.
I have talked about the CMd and Linux, the details of which you can read in these articles:
- How To Find Installed Packages Version In Linux
- How to Change Permissions and Owners via Command Line
Change Permissions and Owners via Command Line
For this guide you will require the following:
– Access to the command line
Step 1 — Changing file and folder permissions via command line
Chmod is a command that is used 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 or directory.
- Group – Each user who is also a member of the group.
- Others – Every other user on the system who are neither a member or an owner of the group.
The command ‘ls -1’ can be used to view file permissions and owners of a file or folder. For example, ‘1s and -1 file1.txt’ will display:
-rwxr–rw- 1 user user 0 Jan 19 12:59 file1.txt
‘-rwxr–rw-‘ – This is the part of the line which shows permissions. There are 4 main letters which you will usually see in this part of the line: r,w,x,d. The letter ‘d’ means that the type of the file is a directory.
1 – This is the number of hard links. A hard link is just an extra name for a file which already exists.
User user – This helps you view who the owner and group owner of the file is.
0 – This shows how big the file is.
Jan 19 12:59 – This is the date of the last edit.
File1.txt – This shows the name of the file or folder.
Let’s return to the command ‘Chmod’. This command grants us the ability to change the permissions of a file or folder. We will show you how to do this by simply appending numbers. Each permission type will have its own number:
- r(read) – 4
- w(write) – 2
- x(execute) – 1
For example, if we would like to set the permissions of ‘file1.txt’ to those:
-rwxr–rw- 1 user user 0 Jan 19 12:59 file1.txt
We would have to type the command:
chmod 746 file1.txt
Every number in the command represents a permission for one of the user types, such as the owner, group owner, or others.
‘chmod 777 file2.txt’ is a different example, this command will grant every permission for every type of user (owner, group, or other).
This 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 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.|
There are plenty of ways to change permissions of a file using the chmod command. However, our 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 is a command that is used to change the owners of a file or a folder. The most basic syntax of the Chown command is this:
chown [owner/group owner] [file name]
- Usually, if we have a file called ‘demo.txt’ and would like to set the owner and group owner of the file to ‘Jerry’ and ‘clients’ respectively, we would use the command below:
chown jerry:clients demo.txt
- As you can see, the owner and group owner are separated using a colon symbol ‘:’.
- If the goal was to change the owner of the file only, then we would use the command below:
chown jerry demo.txt
In the command above, we left out the group owner, instead only typing in the new owner of the file; in this way, the group owner will stay unchanged.
If we wanted to change the group owner of the file but not the owner, then we would use the command below:
chown :clients demo.txt
In this case, only the group owner has been changed to ‘clients’, leaving the owner unchanged.
Step 3 — Using additional options with chmod and chown commands
There’s one way which should work with both commands, it is ‘–R’ in which the R stands for recursive.
This option allows you to replace the permissions or owners in the given directory and all other files and folders within that directory.
Different options for “chmod” and “chown”:
- “-f” – Silent, stealthy, and simple. Force is not going to show most error messages.
- “-v” – Will provide you with a diagnostic of every file which has been affected by the command.
- “-c” – This is similar to ‘-v’. However, it will only provide information in a case where changes were actually made.
Using the skills we have taught you in this guide, you will be able to manage your Server or VPS in a more simple manner by changing permissions and owners of files and folders with a command line.