If you're new to *nix type OS's or just haven't spent a ton of time in the terminal, this will be a very handy guide to file permissions. I picked a raspberry pi as the cover image because that's often time people's first introduction to Unix based operating systems. It was for my oldest daughter.
Seeing File Permissions
First off, you need to know where to look for the file permissions. So, we need to run a command.
100% -> ls -l
This will show all of the files in the current directory. The -l
is the flag for "long format" without the -l
you'd just get a list of filenames and directory names.
100% -> ls -l
total 0
-rw-r--r-- 1 admin admin 0 Feb 16 08:43 test.sh
So what in the world does all that mean?!
Breaking Down the Output
Let's break each piece down. We'll actually skip over the first part for now, we'll come back.
The total 0
is the total size of the directory in blocks, which are 1024 bytes.
The 1
is how many links there are to that file. Links are an entirely different discussion.
The first admin
is the owner of the file.
The second admin
is the group owner of the file
The 0
is how many bytes the file is
The date Feb 16 08:43
is the modification date and time.
Finally, the test.sh
is the file name
Okay, so the meat of why you're still reading this. -rw-r--r--
What is this?
Well, we can break this down even further to explain it better. These are your permissions for that specific file.
Let's break it into 1-3-3 characters.
The first character in this case is a -
This position shows if the file is a file or a directory. Look at this example.
100% ➜ ls -l
total 0
drwxr-xr-x 2 admin admin 64 Feb 16 08:50 directory
-rw-r--r-- 1 admin admin 0 Feb 16 08:43 test.sh
See the d
? The shows that that file is actually a directory.
Next the first set of three. rw-
These are the owner's permissions. This means if you are admin
or whoever is in the owner
field you have these permissions. The r
stands for read
, the w
stands for write
and the -
means you don't have this permission. But look at the directories permissions, there's an x
there. That means that the user admin
has execute
permission on the directory but not test.sh
The next set of three r--
, is for the group. The group of users admin
only has read permissions for test.sh
.
Finally the world
or others
group r--
, also only has read permissions for test.sh
Changing Permissions
The command chmod
, meaning change mode
, is used to change the permissions of a file or directory. There are two ways you can use this command. Absolute and Symbolic.
Absolute Mode
This is the mode you may have seen the most. File permissions are represented as a three digit octal number.
•100% ➜ chmod 000 test.sh
•100% ➜ ls -l
total 0
drwxr-xr-x 2 admin admin 64 Feb 16 08:50 directory
--------------- 1 admin admin 0 Feb 16 08:43 test.sh
You see that chmod 000 test.sh
clears out all permissions for the test.sh
file. So, 0
stands for ---
.
Number | Permission | Symbol |
---|---|---|
0 | None | --- |
1 | Execute | --x |
2 | Write | -w- |
3 | Execute + Write | -wx |
4 | Read | r-- |
5 | Read + Execute | r-x |
6 | Read + Write | rw- |
7 | Read + Write + Execute | rwx |
If I wanted to give read, write, and execute to the owner, read and execute to the group, and read only to others I would do this.
•100% ➜ chmod 754 test.sh
•100% ➜ ls -l
total 0
drwxr-xr-x 2 admin admin 64 Feb 16 08:50 directory
-rwxr-xr-- 1 admin admin 0 Feb 16 08:43 test.sh
Each digit corresponds to the respective group.
Symbolic Mode
This mode can take some getting used to. Once you break it down it is easy.
Operator | Description |
---|---|
+ | Adds permission |
- | Removes permission |
= | Sets the permissions |
Symbol | User Denoation |
---|---|
u | User/Owner |
g | Group |
o | Other |
a | all |
So if I wanted to set the groups permissions all at once I could run
•100% ➜ chmod g=rwx test.sh
•100% ➜ ls -l
total 0
drwxr-xr-x 2 admin admin 64 Feb 16 08:50 directory
-rwxrwxr-- 1 admin admin 0 Feb 16 08:43 test.sh
g=rwx
Group = read, write, and execute. Let's remove execute from the group. They don't need that.
•100% ➜ chmod g-x test.sh
•100% ➜ ls -l
total 0
drwxr-xr-x 2 admin admin 64 Feb 16 08:50 directory
-rwxrw-r-- 1 admin admin 0 Feb 16 08:43 test.sh
Well, I want others
to be able to write too. Let's do that.
•100% ➜ chmod o+w test.sh
•100% ➜ ls -l
total 0
drwxr-xr-x 2 admin admin 64 Feb 16 08:50 directory
-rwxrw-rw- 1 admin admin 0 Feb 16 08:43 test.sh
That's all there is to it.
Ownership and Groups
Changing ownership
chown user test.sh
Changing group
chgrp group test.sh
Want to change user AND group?!
chown user:group test.sh
That's it!
Credits
Photo by Vishnu Mohanan on Unsplash
Discussion (0)