What Will I Learn?
You'll learn about understanding and changing file permissions in linux.
Requirements
- Read the other tutorials in the series
Difficulty
- Basic
Tutorial Contents
Hey guys, been away from steemit for a few days, but here's a short tutorial about linux file permissions.
What are file permissions?
Linux follows the Unix philosophy of "everything is a file" pretty extensively. So obviously, setting limits on who can read, write or execute files is a pretty important thing. You don't want some random user to be able to make changes to the core system by modifying important files. That's why linux has file permissions.
In linux, there are 3 permissions:
- Permission to read a file
- Permission to write/modify a file
- Permission to execute a file as a binary
If you have typed the command ls -l
to look at the contents of a directory, you'll get something like this:
So in this, you'll see this: -rw-r--r--
What's this garbage?
Those are the permissions of the file.
This text has 4 parts:
- The first character indicates whether this is a file or a directory. If it's the former, it's a
-
. If it's the latter,it's ad
. - The next 3 characters show the permissions for the user that owns the file.
- The next 3 characters show the permissions for the group that the user is part of.
- Finally, the last 3 characters show the permissions for everyone that is not the owner of the file
Now we come to the actual characters. These define what the actual permissions are:
Character | Permission |
---|---|
r | Read |
w | Write |
x | Execute |
So if you put these 2 things together, you can find out the permissions of the file.
So in this screenshot,
- The directory
dir5
is readable, writable, and executable by the owner. It's readable and executable by other users in the group, or anyone else - The file
file1
is readable and writable by the owner. It's only readable by others in the same group, or anyone else.
Note: File permissions are not implicitly recursive. So just because dir5
can be read by anyone, the contents of dir5
may not be readable by everyone.
Changing the permissions of a file/directory
Ok so, in the above screenshot, what if you want to execute file1
like a program? Well, you have to change the permission, with the chmod
command.
The chmod
command has a specific syntax for denoting permissions. Without going into the octal number system, here are the symbols:
Permission | Symbol |
---|---|
Read | 4 |
Write | 2 |
Execute | 1 |
This is the syntax for the chmod
command:
chmod xxx filename
The x
's are the sum of all the permissions you want to give to the user/group/everyone.
So for example:
- Read and Write - 4 + 2 = 6
- Read, Write and Execute - 4 + 2 + 1 = 7
- Read and execute - 4 + 1 = 5
- Just read - 4
- No access - 0
Here's an example:
In this case, we passed the number 764
. This means that we want to set the permissions to:
- Read, Write and Execute for the owner. (4 + 2 + 1 = 7)
- Read and Write for any user that is in the same group as the owner (4+2 = 6)
- Only read for everyone else (Read = 4)
Here's another example:
In this case, we used the -R
flag to make it recursive ie. all files/directories inside dir5
have the same permissions as dir5
itself. In this case, we passed 760
:
- Read, Write and Execute for the owner. (4 + 2 + 1 = 7)
- Read and Write for any user that is in the same group as the owner (4+2 = 6)
- Everyone else has no access to the directory.
So that's it for this tutorial. Hope you found it easy to understand.
Previous Parts
- Linux Command Line Tutorial #3 - Shell features
- Linux Command Line Tutorial #2 - Working with Files
- Linux Command Line Tutorial #1 - Moving around in the Filesystem
Sources
- Header image created using this image from Pixabay
- All screenshots are of my own system.
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
PS1: where's the
chown
-'ing? WIll that be covered in part 5:-)PS2: welcome back!
You can contact us on Discord.
[utopian-moderator]
Yeah that will be covered in the next part :-)
What about what the execution means for a directory?
Hey @harshallele I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x