LINUX: chmod & chown — The File & Folder Access Control

A Tech Lead by profession, a Data Enthusiast and a forever learner. Experienced in DevOps, Cloud, FullStack Development, Data Science, Machine Learning and AI.
Search for a command to run...

A Tech Lead by profession, a Data Enthusiast and a forever learner. Experienced in DevOps, Cloud, FullStack Development, Data Science, Machine Learning and AI.
No comments yet. Be the first to comment.
Hashnode introduced 'headless' and while having a look at it, It really seemed interesting. It's nice to have a way to play with our publications and posts from Hashnode, outside of Hashnode without tedious data scraping. While reading another blog, ...

While trying to get Red Hat (RHEL9) installed in my WSL (Windows Subsystem for Linux) within my Windows 11 laptop, I stumbled upon the same hurdle I did for installing the older RHEL8. The list of available distros published by Microsoft still does n...

When it comes to collaborative software development, efficiency in communication and planning are critical. GitHub offers a feature called "Code owners" to improve project management and facilitate cooperation. Let's see how this can improve your dev...

PowerShell is a powerful tool, allowing to perform a myriad of tasks with just a few commands. However, there are times when elevated privileges are required to execute certain commands. This is as easy as right-clicking PowerShell and "Run As Admini...

While trying to setup RHEL with WSL 2, I stumbled upon this very helpful blog https://wsl.dev/mobyrhel8/ While doing the setup, I found this zsh theme very nice, and I have been using it since then. The above mentioned blog already has the steps, but...

chmod and chown are among those popular Linux commands.
chmod: To modify access to files and folders by providing read/write/execute permissions.
chown: To change the ownership of a file or folder.
There are two ways in which the permissions can be changed/set.
The symbolic method is the one where we mention:
Example :
To remove read and write permission from the group and other of file “myfile.txt”
chmod og-rw myfile.txt
To add execute permission to group of the file “myfile2.txt”
chmod g+x myfile2.txt
The numeric method (my personal favorite) adds up the numbers to determine what kind of permission a file should have:
Now here’s the magic:
1 = execute (1)
2 = write (2)
3 = write (2) + execute (1)
4 = read (4)
5 = read (4) + execute (1)
6 = read (4) + write (2)
7 = read (4) + write (2) + execute (1)

And the only extra thing to remember is the sequence of permission, it’s always
User -> Group -> Others
Example:
To set the permission for a file “myfile2.txt” such as:
a. User has full (read, write, and execute) permission …(read+write+execute = 4+2+1 = 7)
b. Group has read and write permission …(read+write = 4+2 = 6)
c. Others have only read permission …(read = 4)
chmod 764 myfile2.txt
Note: 777 is a very dangerous way to give permission, this gives everyone in the world full control over file.
chown is pretty straightforward. Just remember that the syntax used for ownership is owner:group
Here are a few examples:
To make a user named bob the owner of a folder called MyDirectory:
chown bob MyFolder
To make a group name myGroup the owner of MyDirectory folder (notice the colon before group name, remember the owner:group pattern):
chown :myGroup MyDirectory
To make a user named john and a group named group2 the owner of folder MyDirectory:
chown john:group2 MyDirectory