Skip to main content

Command Palette

Search for a command to run...

Understand The Role Of File Ownership And Permissions In Linux

Updated
13 min read
Understand The Role Of File Ownership And Permissions In Linux

Linux is a powerful multi-user system, and permissions are the gatekeepers that control who can access or modify files. This guide will break down everything about Linux permissions and how to manage them using

  • chmod – change file mode (permissions)

  • chown – change file ownership

  • chgrp – change file group

Understanding Linux Permissions

Each file or directory has 3 types of permissions for 3 categories of users:

Categories:

  • u – user (owner)

  • g – group

  • o – others

  • a – all (u+g+o)

Permissions:

  • r – read (4)

  • w – write (2)

  • x – execute (1)

1.What is chmod?

The chmod (short for change mode) command in Linux is used to modify the permissions of files and directories. It helps control who can read, write, or execute a file.

Note: Only the file owner or a superuser (root) can change the file or directory permissions.

Two Ways to Use chmod

There are two main ways to change file/directory permissions with chmod:

  1. Octal Number Representation

  2. Symbolic Representation

1. Octal Number Representation

This is a numeric method of setting permissions using three digits:

Each digit represents permission for:

  • Owner(user) , Group , Others

Each permission is assigned a number:

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

Just add the numbers based on the permissions you want:

PermissionValue
No permission0
Execute only1
Write only2
Write + Execute3
Read only4
Read + Execute5
Read + Write6
All (rwx)7

Example:

chmod 755 myfile

This sets:

  • Owner (7 = rwx) → Has full access:

  • Group (5 = r-x) → Can read and execute, but cannot modify the file.

  • Others (5 = r-x) → Same as the group: read and execute only.

If you need to apply permission to a file, you can use like this .


Step 1: Create a File and Check Its Permissions

Let’s say you have a file called file.txt default permissions (typically rw-r--r--, or 644

-rw-r--r--

These are divided into 3 groups:

  1. User (owner) – can read and write (r+w) = 6

  2. Group – can read (r)= 4

  3. Others – can read (r)= 4

So the owner can edit it, but others can only read.


File Permissions: How to Restrict Access

You can control what each user can do (read, write, execute) using permission bits:

Permission CodeOwnerGroupOthersMeaning
777rwxrwxrwxFull access to everyone
755rwxr-xr-xOnly owner can write
700rwx------Only owner has any access
644rw-r--r--Only owner can write
444r--r--r--No one can write
000---------No access at all

Step 2: Make it Read-Only for Everyone ( No one can write)

Let’s say you don’t want anyone (even yourself) to write to it.

chmod 444 file.txt

-r--r--r--

  • Owner: read-only

  • Group: read-only

  • Others: read-only

The -r--r--r-- permission setting ensures that everyone has read-only access to the file, meaning no one (not even the owner) can modify it.

Try to edit:

You’ll see: Permission denied

This completely locks the file from being modified, even by the owner, unless they change the permissions again.


Step 3: Give Write Access Back to the Owner ( Only owner can write )

Now let’s say you want to be able to edit it again.

chmod 644 file.txt

This permission setting (-rw-r--r--) means:

-rw-r--r--

  • Owner has read and write (rw-) permissions.

  • Group has read-only (r--) permissions.

  • Others (everyone else) also has read-only (r--) permissions.

Now (the owner) can write to it again, but others still can’t.

Step 4: What If You Remove Your Own Read Access?

Now the owner can only write, but not read. You might not even be able to open the file properly.

chmod 244 file.txt

The permission setting --w-r--r-- means:

  • Owner has write-only (-w-) permissions.

  • Group has read-only (r--) permissions.

  • Others (everyone else) also has read-only (r--) permissions.

What happens:

  • You have write access, but you cannot read it (e.g., using cat file.txt or opening it in a text editor).

  • You’ll get a "Permission denied" error when trying to read

  • This shows that even the owner must have permission to access the file.

Note : Permissions must be planned — changing one can impact access for everyone, even yourself. So if you change something, the entire access model can change, and you need to reassign them carefully.


Step 5: Running a Script File

Let’s say you a script: one.sh with default permissions (typically rw-r--r--, or 644), which means:

  • Read/write for the owner

  • Read-only for group and others

  • No execute permission for anyone

So when you try to run it with: ./one.sh, you get a "Permission denied" error because there are no execute permissions.

Step 6: Add Execute Permission

chmod 744 one.sh
  • 7 for user = r (4) + w (2) + x (1) → full permissions

  • 4 for group = r (read only

  • 4 for others = r (read only)

Now run it with: ./one.sh, and it will no longer show "Permission denied."

  • After changing the permission to 744, only the user (owner) can run the script. Group and others can only read it, not execute.

  • If you want everyone (owner, group, and others) to read, write, and execute the script, you can change the permission to 777.

  • Full access to everyone : -rwxrwxrwx

**Note :**But be careful (777 ) this is not safe for important files.

Step 7 :Change All .txt Files at Once

Let’s say you want to make all text files in a folder fully accessible:

chmod 777 *.txt

This command changes all files ending in .txt to full access (read/write/execute) for everyone.


2. Symbolic Representation

In Unix-like systems, file permissions are represented using symbolic notation, which provides a more human-readable way to set and modify file permissions compared to numeric notation.

Understanding Symbolic Representation

Each file's permissions are divided into three groups:

  • Owner (u) – The user who owns the file.

  • Group (g) – Users who are part of the file’s group.

  • Others (o) – Everyone else.

  • All users (a) – Applies to owner, group, and others.

Each group can have the following permissions:

  • Read (r) – Allows viewing the file contents.

  • Write (w) – Allows modifying the file.

  • Execute (x) – Allows executing the file.

1. How to Add Permissions

You can use the chmod command with symbolic notation to grant permissions:

Let's say you have a file named data.txt with default permissions (typically rw-r--r--, or 644).

  • Add execute (x) permission for the owner:
chmod u+x data.txt

The x (execute) permission has been added for the owner (rwx).


  • Add write (w) permission for group :

  • Grants write (w) permission to the group, meaning members of the group can now modify data.txt.

chmod g+w data.txt

Now the group has both read (r) and write (w) permissions.


  • Add write (w) permission other

  • grants write (w) permission to others, meaning everyone can now modify data.txt.

chmod o+w data.txt

Now, everyone has both read (r) and write (w) permissions.


2. How to Remove Permissions

Similarly, you can revoke permissions using chmod:

Remove execute (x) permission for the owner:

chmod u-x data.txt

Removes the execute (x) permission from the owner, meaning they can no longer run the file as a program.


Remove read (r) permission for the group:

chmod g-r data.txt

removes read (r) permission for the group, meaning members of the group can no longer view the contents of data.txt.


Remove all permissions from others:

chmod o-rwx data.txt

removes all permissions (read r, write w, and execute x) from others, meaning they can no longer access data.txt in any way.


3. Setting Multiple Permissions at Once

You can combine multiple permissions:

  • Grants read (r) and execute (x) permissions to the owner, group, and others.
chmod ugo+rx data.txt

Now, the user and group have all permissions, while others have read and execute permissions for the file data.txt.


4. Understanding File Permission Modifiers in chmod

Using different modifiers in the chmod command helps manage file permissions effectively:

  • = (Equals)Resets all permissions for the specified class and applies only the given permissions.

  • + (Plus) → Adds the specified permissions without altering existing ones.

  • - (Minus) → Removes the specified permissions but keeps the rest unchanged.

Using = in chmod is like erasing the old permissions for that class and writing only what you tell it.

chmod u=x,g=rw,o=w hello.sh

This overrides permissions for each class:

u=x → User gets only execute

g=rw → Group gets read and write

o=w → Others get write only


chmod u=rwx,o=w demo.txt

u=rwx → User gets read, write, and execute

o=w → Others get write-only

g (group) is not mentioned, so its permissions remain unchanged

= replaces all permissions for that class — it overrides, not just adds.

Example: chmod u=r hello.sh

First, it removes all existing user permissions and then sets the user’s permissions to read only. This means the write and execute permissions for the user are removed, while the group and others' permissions remain unchanged. Only the permissions you specify in the command are applied.


2. Linux File Ownership: How chown and chgrp Work

In Linux, every file and directory has an owner and a group. This is part of the Linux permission system that controls who can read, write, or execute a file.

Understanding and using chown and chgrp helps you manage permissions and security on your system properly.


What Are Owner and Group?

Each file or folder has:

  • Owner: A user who owns the file (usually the creator). By default, the user who creates a file or directory becomes its owner, and they have full control over it.

  • Group: A group of users who may also have access.. Managing users in a multi-user environment involves creating separate groups (e.g., dev team, QA team, sysadmin team). Group membership simplifies permission management.

  • To view all groups, check the contents of /etc/group.

  • To View Owner and Group of a File or Directory

  • Use the ls -lrth command:

  • 1st ec2-user 1st is the owner

  • 2nd ec2-user is the group

You Need sudo to Change Ownership

Only the root user or someone with sudo access can change the owner of a file.

Changing the group may also require sudo, depending on your permissions.


1. chown – Change Owner

sudo chown root filename

Changes the owner only.

  • sudo → Run the command as superuser (required to change ownership to another user)

  • chown → Stands for change ownership

  • root → The new owner of the file

  • It changes the ownership of the file from the current owner (e.g., ec2-user) to the root owner.

2. chgrp – Change Group Only

Basic Usage:

sudo chgrp root filename

Use this if you only want to change the group, not the owner

It changes the group ownership of the file from the current group (e.g., ec2-user) to the root group.

Before, we changed the owner, so now it shows group root and owner root.

3.Owner and Group Together:

sudo chown root:root filename

Changes both owner and group.

  • sudo → You need superuser privileges to change ownership to another user or group.

  • chownChange ownership

  • root:root → Set both owner and group to root

Recursive (for directories):

sudo chown -R root:root devops123/

Changes ownership of the folder and all its contents.

  • sudo You need superuser privileges to change ownership to another user or group.

  • chown is the command used to change the ownership of files or directories.

  • The -R option stands for "recursive", which means the command will apply not only to the directory itself (devops123/) but also to everything inside it — all subfolders and files.

  • root:root tells the system to change both the owner and the group to root.

  • devops123/ is the directory you are targeting.

The devops123/ folder and everything inside it was owned by ec2-user.

After using sudo chown -R root:root devops123/, the entire folder, its files, and all subdirectories were transferred to root.

This means only the root user can now make changes — regular users like ec2-user can read, but cannot write or delete unless they use sudo.


3.echo command

The echo command in Linux is used to display text or variables in the terminal. It’s commonly used in shell scripts and command-line operations.

Creating the file with the echo command

echo "Hello DevOps Team!" > greetings.txt

  • This will create a file called greetings.txt with the content

To append to a file instead of overwriting, use >>

echo "Welcome to the project." >> greetings.txt

This adds "Another line" to the existing greethings.txt file.

4. diff Command

The diff command is used in Unix-like operating systems to compare two files line by line. It helps identify differences between them by displaying added, removed, or changed lines.

We'll make file1.txt and file2.txt with both matching and different lines.

Contents of file1.txt

Hello DevOps
Welcome to Linux
This is file one
We are learning shell scripting
Linux is powerful
Automation saves time
Docker is lightweight
End of file

Contents of file2.txt

Hello DevOps
Welcome to Linux world
This is file two
We are learning shell scripting
Linux is awesome
Automation saves time
Kubernetes manages containers
End of file

Now compare them:

diff file1.txt file2.txt
diff -y file1.txt file2.txt

What Does This Mean?

2,3c2,3

  • Lines 2 and 3 in file1.txt were changed to become lines 2 and 3 in file2.txt.

From file1.txt:

Welcome to Linux
This is file one

To file2.txt:

Welcome to Linux world
This is file two

5c5

  • Line 5 was changed.

From file1.txt:

Linux is powerful

To file2.txt:

Linux is awesome

7c7

  • Line 7 was changed.

From file1.txt:

Docker is lightweight

To file2.txt:

Kubernetes manages containers

Summary:

  • The diff command clearly shows what lines are different.

  • You saw three changes: 2 lines together (2,3), then line 5, then line 7.

  • This is super useful for:

    • Code reviews

    • Configuration management

    • Debugging

    • Version comparison


diff -y file1.txt file2.txt

The diff -y file1.txt file2.txt command displays the differences between the two files side by side, making it easier to compare visually.

  • Lines with differences are marked with a | in the middle.

  • Identical lines appear unchanged.

  • This format helps you compare differences line by line with better readability.

K

Thank You. Blogs are really excellent, clear, neat, understandable, readable and really amazing.

1
K

You're welcome! Happy to hear the blogs were helpful. 😊

1

More from this blog

kkfunda

60 posts