Learn Linux File Permissions the Easy Way (And the Hard Way, Too)

Do you remember when you were a little kid, and you had to ask your parents’ permission to do just about anything?
“Mom, can I ride my bike to the park?”
“Dad, can I go climb a tree?”
“Mom, can I have a snack?”
I don’t know about you, but very often (for me) the answer was a resounding, “No.”
That’s kind of the way Linux approaches file system permissions. Unless you need to, the answer is “No.”
Of course, you do have control of this, thanks to Linux file permissions.
There are two ways of approaching file permissions on Linux: the easy way and the hard way. I’m going to show you both, but I will say this: I rarely go the hard route, because the easy route is just too simple to ignore.
Are you ready for this?
Let’s do it.
The Easy Route
Most likely, this is how you’ll deal with permissions on a daily basis.
We’ll start our journey with permissions by considering two things:
- User types
- Permission types
There are three different user types:
- user: This is the person who owns the file (the user who created it).
- group: All users who are members of this group have the same permissions for the file/folder.
- other: Everyone else.
Think of the above as u, g and o.
Next, let’s talk permissions, which break down into three bits:
- read: You can only view a file.
- write: You can read and edit a file.
- execute: You can run a file.
Think of the above as r, w and x.
We use all of the above in conjunction with the chmod command (change file mode bits).
Let’s say you have the file newstack.sh. You created the file, so you’re the owner. By default, you will have r/w permissions for the file. But what if that file is a bash script and you need to run it? To do that, you have to give yourself executable permissions. Since you are the user, the command for that would be:
1 |
chmod u+x newstack.sh |
What you’ve done is add executable permissions for the user to the file newstack.sh.
Now, let’s say you’ve created a group (called dev1) and have already changed the group ownership with chown :dev1 newstack.sh, and you want to make sure everyone in that group has executable permissions for the file. To do that, you could issue the command:
1 |
chmod g+x newstack.sh |
Or maybe you want to allow everyone in the group to be able to edit the file, which can be done with:
1 |
chmod g+w newstack.sh |
At some point, you might need to revoke the group edit permissions, which means you’d issue the command:
1 |
chmod g-x newstack.sh |
See the difference?
Let’s say you have a folder with subfolders and files and you want to give everyone in the group write permissions to everything within the parent folder. If the parent folder is named NEWSTACK, the command would be:
1 chmod -R g+w NEWSTACK
The -R option stands for recursive.
The Challenging Method
This really isn’t that hard, it’s just a bit of memorizing.
Remember, we have:
- user
- group
- other
And we have:
- read
- write
- execute
Each of the above permissions is assigned a numerical value of:
- read – 4
- write – 2
- execute – 1
The permission structure is set up like this:
1 |
-rwxrwxrwx |
So there are three sections with three possible permissions. What you do is add the numerical values together. So, if you want to give the user read/write permissions, the numeric total is 6. If you want to give group read permissions, the numeric total is 4, and if you want to give other read permission, the numeric total is 4, so the combined permission is then 644.
Here are a few more examples:
- 755 – user = rwx, group r/x, other r/x
- 600 – user = rw, group/other = none
- 777 – user = rwx, group = rwx, other = rwx
- 666 – user = rw, group = rw, other = rw
As long as you can remember the numeric values for rwx, you can simply add those values together to get the proper permissions.
That’s not so hard.
So, to use the numeric values for permissions, you’d issue a command like:
1 |
chmod 755 newstack.sh |
The above command would give the following permissions to the newstack.sh file:
- user – rwx
- group – rx
- other – rx
Are you getting the hang of it now?
Special Permission Bits
There’s one last thing I’ll mention: special permission bits, which you’ll rarely use. Those bits are:
- Set User ID (SUID): This bit allows the owner of a file or directory to run it with elevated privileges as if they were running the command directly. Example: chmod u+s /path/to/file.
- Set Group ID (SGID): This bit allows a group of users to run files and directories in groups with elevated privileges as if they were running the command directly, but only for members of that group. Example: chmod g+s /path/to/directory.
- Set Owner ID (SUID) and Set Group ID (SGID): This is a combination of both SUID and SGID bits, which allows both the owner and members of the group that own or have access to a file or directory to run it with elevated privileges as if they were running the command directly. Example: chmod u+s,g+s /path/to/file.
And that, my friends, is your introduction to Linux file permissions.