Unlocking Linux Power: Demystifying the Differences Between chmod and chown Commands"
Table of contents
Imagine logging into your system with a password, gaining access to all your files and applications. But when you try to remove or modify certain packages, you’re denied access because you lack the necessary permissions. This is because Linux has a built-in security mechanism that controls who can read, write, or execute files. Just like when you create a file or folder, it comes with default permissions that restrict what you can do with it.
To gain more control, we use two powerful commands: chmod and chown. These commands allow you to change the permissions and ownership of files and directories, giving you the flexibility to manage your system as you see fit.
Let's Go Deeper: Understanding How chmod
and chown
Work
In Linux, file permissions are divided into three parts:
User (u): The owner of the file.
Group (g): A group of users who have access to the file.
Others (o): Everyone else.
There are three types of permissions:
Read (r): Allows viewing the content of the file (Numerical value: 4).
Write (w): Allows modifying the content of the file (Numerical value: 2).
Execute (x): Allows running the file as a program (Numerical value: 1).
These are represented as:
-rw-rx-rwx
(where each triplet represents user, group, and others)
SETTING THE PERMISSION WITH CHMOD
To change the permission to file or directory
chmod u+rwx,g+rwx,o+rwx filename
chmod u+rw filename
chmod g+rwx filename
chmod o+rwx filename
it change the file permission according to your wish
you can change file permission using numerical value read (4), write(2) and others (1)
chmod 775 filename
it give the permission like -rwx-rwx-r-x
CHANGING USER OWNERSHIP USING CHOWN
To change the ownership of the file to a particular user
chown <user> <filename>
chown navya file.txt
To change the ownership of the particular group
chown <user> <group> <filename>
chown navya tech file.txt
change ownership recursively for all files within a directory, use the -R
flag:
- Change user ownership for a directory and its contents
chown -R <user> <filename>
chown -R navya navyadir
You can also change group ownership with chown
or chgrp
:
chown <group> <filename>
chown devops file.txt
And that’s it! By mastering these commands, you can confidently manage file permissions and ownership in Linux. Until next time, happy coding!
Subscribe to my newsletter
Read articles from Pooja Manellore directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Pooja Manellore
Pooja Manellore
I have completed my B.Sc. in Computer Science in 2024 and have gained skills in Data Analytics, HTML, and CSS. I am currently advancing my expertise by learning DevOps, aiming to secure a role as a DevOps Engineer. I am eager to join a company immediately where I can apply my skills and continue growing in this field