In this video, you’ll learn about read, write, and execute permissions.
What it means to be the user owner and group owner of a file or directory.
And how to alter the permissions with chmod and chown.
Linux permissions supports three different modes: Read, Write, and Execute.
You’ll see these denoted with r, w, and x.
Each of these has a different meaning depending if it’s for a file or a directory.
Read for a file means you can view its contents, while for a directory, it means you can list
the contents and view metadata.
So if you don’t have read permissions when doing a directory listing, you might see a
series of question marks.
Write for a file means you can modify its contents, while for a directory, it means
you can create new files within that directory.
And finally, execute for a file means that you can run it as an application, while for
a directory, it means you can enter or traverse that directory.
Let’s jump to a terminal and take a look at some examples.
If I run ls –l within this working directory, we see that it has a file and a directory.
The permissions are defined by 10 values.
Let’s take a closer look at this first file.
The first bit identifies the type.
The dash indicates that this is as a regular file.
But there are other types, such as d for directory, l for link, b for block device and so on.
The next three values represent the permissions for the user owner.
The middle three are the group owner.
And the final three are for other, so anyone that’s not the user or group.
Each file or directory has a single user owner, and a single group owner.
As you can see, we have a file called SalesReport.txt and it's owned by the user Michael and belongs
to the group Office.
So Michael has read, write, and execute permissions, the Office group has read and execute permissions,
and other has no permissions.
On this system, there are three users: Michael, Pam, and Jan.
This is important to keep in mind because we’ll be looking at how a user’s group
affects their access to a file.
So Michael belongs to the Michael and Office groups, Pam to the Pam and Office groups,
and Jan only to the Jan group.
To set up a similar scenario on your system, you can check out the video on Users and Groups.
When evaluating permissions, the system processes from left to right and stops at the first
match.
So let’s say that Pam wants to read the SalesReport file.
The system will first look at the user, it’s not Pam so it’ll move to the group, which
is set to Office.
Pam belongs to this group so she would be able to read the SalesReport file.
We can test that by going back to the terminal, switching to the Pam user with su space Pam
and then running cat space SalesReport.txt.
And we get results.
But what about Jan?
She’s not a part of the Office group, so the other permissions would apply to her and
as a result she wouldn’t be able to read the file.
If we switch to the Jan user, with su Jan and try the same thing with cat space SalesReport.txt.
Sure enough, it says permission denied.
Let’s take a look at changing permissions for a file.
I’ll go back to the Michael user with su Michael and create a blank file using touch,
a space, and the name of a new file.
Then we’ll run ls –l to look at the details for that file.
By default the user and group is whoever created the file, in this case Michael.
And the read, write, execute permissions will also be set to a default value.
This is based on your umask, which is more than we want to get into here, but just know
the file gets created with default permissions.
To change permissions for this file, we can use the chmod command.
The sytax is chmod, a space, any options, a space, permissions, a space, and the filename.
We can define the permissions in two ways, either numerically or symbolically.
We’ll start by looking at the symbolic method.
As we noted earlier there’s a user, group, and other.
So we can set permissions using u, g, and o in combination with the r, w, x values.
Let’s jump back to the terminal and take a look at an example.
I’ll need root privileges to alter the permissions, so I’ll use sudo then chmod.
And let’s give the user read and execute permissions, which would be u=rx.
Then we’ll use a comma and set the group permissions to read, write, and execute, so
g=rwx.
And for other I don’t want any permissions, so we’ll use comma o= with nothing after
it.
Then a space and the file name.
Running ls –l we can see it did change the permissions.
The nice thing about the symbolic method is that you can add or remove permissions without
worrying about what the current permissions are.
So let’s try that, with sudo chmod.
Then to add write permissions for both the user and other, I can group u and o together
and then do +w.
And let’s take away write from the group, so I’ll use comma, then g-w, space, and
the file name.
Using ls –l and we can confirm the permissions changed.
The other method for defining permissions is numerical, where we assign a value to each
permission and add the numbers together to get the final result.
Read has a value of 4, write 2, and execute 1.
So for read/write/execute, it would be 4 plus 2 plus 1, for a total value of 7.
Read and execute would be 4 plus 1 for a total of 5.
And just write would be 2.
So the current permissions for this file would be 752.
Let’s go back to the terminal and try changing the permissions numerically.
Maybe we want to set the permissions to read and execute for the user, read write and execute
for the group, and no permissions for other.
Setting the user to have less permissions than the group like this, is uncommon, but
I’ve done it here to point out something that’s easily overlooked and you’ll see
why in a second.
So we’d do sudo chmod 570 and the filename.
Then running ls –l we see that change.
Let’s test these permissions with the Michael user.
Michael is both the user and group owner, so you might think he should be able to read,
write, and execute because he’s part of the Michael group.
But remember that the system processes permissions from left to right and stops at the first
match.
So it checks to see if Michael is the user.
He is, so it stops there and only grants him read and execute permissions.
We can test this by trying to edit the file with vi, a space, and the filename.
And you can see it says read only, and if we try to make changes it gives us a warning.
What if we want to change the user owner and group owner for this file?
We can do that with the chown command.
The syntax is chown, a space, any options, a space, the username, a colon, groupname,
a space, and then the filename.
Again we need root privileges to run this, so sudo chown.
Then we specify the user, a colon, the group followed by a space and the filename.
Using ls –l shows us that the user is now Pam and the group is Office.
If we just want to change the user, we do that by only specifying a user and leaving
off the colon and group name.
For example, I can do sudo chown Jan ClientList.
Sure enough ls –l shows the user has changed to Jan.
We can change only the group with sudo chown, a colon, the groupname, and the filename.
And finally, if we run ls –l, we see that change.
That’s it for a quick overview of permissions on a Linux system.
I hope it gives you a basic understanding of how the read, write, and execute permissions
affect access to a file.
Thanks for watching!