How to change permissions and owners in Linux through the command line

In this tutorial, you will learn how to change permissions and owners in Linux using the chmod and chown commands. By doing so, you will achieve better management in team projects.

Why is it necessary to change permissions and owners in Linux

it is a multi-user operating system, so more than one person can work on the same computer at the same time. In addition, it is possible to access the system locally or remotely. That is why developers often use this operating system for group projects.

In such a vast environment, we need to set permissions and ownership of files so that only certain users can access our data. In this way, we will protect sensitive information and prevent unwanted changes from occurring.

Fortunately, thanks to the commands chmod Y chown, it is easy to change permissions and owners in Linux. But before you start learning how to use them, make sure you have access to the command line. You can start the terminal by pressing Ctrl + Alt + T.

Change file and folder permissions

We will use the command chmod to change permissions of a file or folder in Linux. But first, you should know that there are three types of users that can interact with a file:

GuyExplanationownerIt is the user who created and owns a file/directory.group (group)All users who are members of the same group.others (others)All other users on the system who are not owners or members of the group.

To see the permissions and owners of a specific file, you can run this command:

ls -l

The result would show something like this:

-rwxr–rw- 1 user user 0 Jan 19 12:59 myfile.txt

Let’s break down the output to see what each field means:

  • ‘-rwxr-rw-‘ – This part of the line represents the permissions of the file. To understand it better, we have to divide it into four groups: (), (rwx), (rw-) Y (rw-).
    – The first group indicates the type of file. Our example shows a script, which represents a normal file. If we are inspecting a directory, the hyphen will be replaced by the d.
    – The three characters after the file type represent the owner permissions. In this example, we can see that the owner can read (r), to write (w) and execute (x) the file.
    – The next three characters are the group permissions. We can conclude that the group can read (r) and write (w), but cannot execute the file. This is because the last character is a hyphen instead of the letter x.
    – The last group is that of the permissions of other users. Based on our example, this type of user cannot execute the file, but is allowed to read and write.
  • 1 – It is the number of fixed links (hard links). A hard link is an additional name for an existing file.
  • user user – The owner and group that owns the file.
  • 0 – The size of the file in bytes.
  • Jan 19 12:59 – The date of the last modification.
  • myfile.txt – The name of the file/folder.
See also  15 VPS Security Tips to Avoid Attacks on Your Server

How to use the chmod command

Let’s say we want to change the permissions of Linux files from -rwxrw-rw- a -rwx-rr-. Simply enter this line:

chmod 744

By running this command, the owner can read, write, and execute the file (rwx). However, the group and others can only read (r-).

At this point, you may be wondering why we use a three-digit number (744) after the chmod command.

This number determines the permissions of the file. Read, to write Y run are represented by a numerical value:

  • r (read) – 4
  • w (write) – two
  • x (run) – 1

So if you want to give all permissions (rwx) to a user, we have to add read (4), write (2) Y run (1). Therefore, rwx is equal to 7.

Meanwhile, since the group and others can only read the file, we assign them 4.

Remember that the permissions of the owner always come first, followed by those of the group and others. That is why we introduce 744.

If you don’t want to give a user any permissions, enter 0 in the appropriate place.

Here is a list of the most common permissions for files:

WorthNumerical valueExplanation-rw——-600The owner can read and write.-rw-r–r–644The owner can read and write, the group and others can read.-rw-rw-rw-666The owner, the group and others can read and write.-rwx——700The owner can read, write and execute, the group and others cannot do anything with the file.-rwx–x–x711The owner can read, write and execute, the group and others can execute.-rwxr-xr-x755The owner can read, write and execute, the group and others can read and execute.-rwxrwxrwx777 The owner, the group, and others can read, write, and execute.

See also  About

Common permissions for directories:

WorthNumerical valueExplanationdrwx——700Only the owner can read and write to this directory.drwxr-xr-x755 The owner, group, and others can read the directory, but only the owner can change its contents.

Change the owners of files and folders

To change the owner of a file and a folder, we will use the command chown. This is the basic syntax:

chown

Let’s say we have a file called “myfile.txt«. If we want the owner of the file is “hoster«, we can use this command:

chown myfile.txt

However, if we want to change the owner group from file to “customers“, we will introduce this line instead:

chown :customers myfile.txt

Notice that we use a colon (:) before “clients” to indicate that it is an owner group.

Now, to change the owner and the group at the same time, the syntax would be like this:

chown :customers myfile.txt

The main rule is that the owner must come before the owning group, and they must be separated by a colon.

Using options with the chmod and chown commands

A option is an additional command to modify the response of a command.

One of the most popular options that you can combine with chmod Y chown it is -R (recursive). This option allows you to change permissions or owners of all files and subdirectories within a specific directory.

If you want to use an option, you have to place it right after the command chmod/chown.

Look at this example:

chown -R 755 /etc/myfiles

See also  WordPress pattern builder is now available

After entering the above command, the owner can read, write and execute all files and subdirectories within the directory /etc/myfiles. The command also gives read and execute permissions to the group and others.

Be especially careful with this option. Using the command incorrectly can cause a serious crash, and it will take a lot of work to revert the changes.

In addition to -R, the following options are often used with the chmod and chown commands:

  • -F or force. The command line will ignore any errors and apply the chmod and chown commands.
  • The option -v (verbose) gives you a diagnostic of all the files that are processed by the command.
  • -c (changes) is similar to -vbut it will only provide feedback if the changes were successful.

conclusion

In this tutorial, you have learned how to use the chmod and chown commands to change permissions and owners in Linux. We’ve also provided the basic syntax and several useful options that you can combine with any of these commands.

To learn more, you can read our article on .

If you have any questions, feel free to comment below!

Gustavo is passionate about creating websites. He focuses on the application of SEO strategies at for Spain and Latin America, as well as the creation of high-level content. When he is not applying new WordPress tricks you can find him playing the guitar, traveling or taking an online course.

Loading Facebook Comments ...
Loading Disqus Comments ...