Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing Permissions and Owners in Linux
#1


    Changing Permissions and Owners in Linux

   

How to Change File and Folder Permissions in Linux


   

We will be using the chmod command to change file and folder permissions in Linux. But first, you need to be aware that there are three types of users who can interact with a file:

   
           
  • Owner — the user who creates and owns a file or folder.
  •        
  • Group — all users who are members of the same group.
  •        
  • Others — all other users on the system who are neither the owner nor members of a group.
  •    

   

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

   
ls -l [file name]
   

The result will look like this:

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

   

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

   
           
  • -rwxrw-rw- — this part of the line represents the file permissions. To understand it better, we have to divide it into four groups: (-), (rwx), (rw-), and (rw-).
  •        
  • The first group indicates the file type. Our example shows a hyphen, which represents a regular file. If we are inspecting a directory, the hyphen will be replaced by d.
  •        
  • The three characters after the file type represent the owner’s file permissions. In this example, we can see that the owner can read (r), write (w), and execute (x) the file.
  •        
  • The next three characters are the group’s file 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 others’ file permissions. Based on our example, this type of user cannot execute the file, but they are allowed to read and write.
  •        
  • 1 — the number of hard links. A hard link is an additional name for an existing file.
  •        
  • user user — the owner and group owner of the file.
  •        
  • 0 — the size of the file in bytes.
  •        
  • Jan 19 12:59 — the last modification date.
  •        
  • myfile.txt — the name of the file/folder.
  •    

   

How to Use chmod Command


   

Let’s say someone in the group is getting a bash: permission denied error and we want to change Linux file permissions from -rwxrw-rw- to -rwx-r–r–. Simply enter this line:

   
chmod 744 [file name]
   

By executing this command, the owner can read, write, and execute the file (rwx). However, group and others are only allowed to read (r–).


   

At this point, you might wonder why we are using a three-digit number (744) after the chmod command.

   

The number determines the file permissions. Read, write, and execute are represented by a numerical value:

   
           
  • r (read) — 4
  •        
  • w (write) — 2
  •        
  • x (execute) — 1
  •    

   

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

   

Meanwhile, since group and others are only allowed to read the file, we give them 4.

   

Remember, the owner’s permissions always come first, then followed by group and others. That’s why we enter 744.


   

Pro Tip


   

If you don’t want to give any permission to a user, enter 0 into the corresponding spot.

   

Here is a list of the most common file permissions:


                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
ValueNumeric ValueExplanation
-rw-------600Owner can read and write. Group and others have no permission.
-rw-r--r--644Owner can read and write. Group and others have read-only rights.
-rw-rw-rw-666Owner, group, and others can read and write.
-rwx------700Owner can read, write, and execute. Group and others have no permission.
-rwx--x--x711Owner can read, write, and execute. Group and others can execute.
-rwxr-xr-x755Owner can read, write, and execute. Group and others can read and execute.
-rwxrwxrwx777Owner, group, and others can read, write, and execute.

   

Common Permissions for Directories:


                                                                                                                                                                                                                                                               
ValueNumeric ValueExplanation
drwx------700Only owner can read and write to the directory.
drwxr-xr-x755Owner, group, and others can read the directory, but only the owner can write.

   

Changing Owners of Files and Folders in Linux


   

To change the owner of a file and folder, we will be using the chown command. Here is the basic syntax:

   
chown [owner/group owner] [file name]
   

For example, to set the owner of the file "myfile.txt" to "ellohost," you can use this command:

   
chown ellohost myfile.txt
   

If you want to change the group owner of the file to "clients," you can use this command:

   
chown :clients demo.txt
   

To change both the owner and group owner at the same time, you can use the following syntax:

   
chown ellohost:clients myfile.txt
   

Remember that the owner should come before the group owner, and they should be separated by a colon.


   

Using Options with chmod and chown Commands


   

An option is an additional command that modifies the behavior of a command. One commonly used option is -R (Recursive), which allows you to apply changes to all files and subdirectories within a specific directory.

   

If you want to use an option, you should place it right after the chmod or chown command. For example:

   
chown -R user /etc/myfiles
   

This command would grant the user read, write, and execute permissions for all files and subdirectories inside the /etc/myfiles directory. Group and others would have read and execute permissions.

   

However, be cautious when using the -R option, as improper use can lead to unintended changes that are difficult to reverse.

   

Other options that are often used with the chmod and chown commands include:

   
           
  • -f or force: This option makes the command line ignore errors and apply the changes.
  •        
  • -v or verbose: This option provides diagnostic information for all files processed by the command.
  •        
  • -c or changes: This option is similar to -v, but it only displays information when changes are successfully made.
  •    

   

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)