We'll now discuss how to list,
set and change standard file permissions in Linux to
understand how file and directory permissions work in Linux.
We must first look at file directory owners.
If we type in LS dash L, we'll see something like this.
Now, any file or directory is owned by a user.
In this case, we see that the file family dog JP G is owned by the user called Aaron.
Only the owner of a file or directory can change permissions.
And in this case, that's Aaron
and we can only change the groups that our user is a part of.
And again,
the root user is the Exception which can change the group of
a file or directory to whatever group exists on the system.
Now, there's also a command to change the user owner of a file or directory.
And that's the cho command for change owner.
And the syntax is cho WN
the user file or directory.
For example, to change ownership of this file to Jane,
we'd use Cho Jane family dog.jp G
and we can change both the user owner and a group with a different syntax of C.
And since only root can change user ownership.
Let's set the user to Aaron and the group to family, to revert all our changes.
And one last LS dash L will show us that the owner is Erin again and the group is family.
Our LS dash L command also shows us the permissions
of all files and directories in our current directory.
Now, the first character on that line shows us what type of entry. This is
a file, a special file directory and so on.
For example, we'd see D for a directory, L for a soft link or a dash for a regular file.
Here's a table that shows the different identifiers and what they stand for.
We will learn about some of these file types later in this course.
Now the next nine characters show us permissions.
Now the first three show us permissions for the user that owns this file.
The next three show permissions for the group of this file.
And the last three show permissions for other users,
any user that's not erin or part of the family group.
Let's see what RW and X mean in two different contexts because they act
in a certain way for files and have slightly different behavior for directories.
Now, for a file,
R means the user group or other users can read the contents of the file.
A dash means they cannot read it.
W means the user group or other users can write to this file and modify its contents.
X means the user group or other users can execute this file.
Some files can be programs or shell
scripts which are instructions that we can execute
to be able to run this programmed or shell script.
We must have the X permission enabled.
A dash permission here means that the program
or shell script can be executed for directories.
We must think a little differently
unlike a file that may contain text to be read,
executed or modified directories don't have such contents,
their contents of the files and subdirectories they hold so read,
write and execute refers to these files in the subdirectories they have inside
R means the user group or other users can read the contents of this directory.
We need an R permission to be able to run a command like
LS pictures and view what files and subdirectories we have in this directory.
Now, W means the user group or other users can write to this directory
and we need a W to be able to create or delete entries in that directory.
W is required to add delete files or subdirectories or use maker.
Now, X means we can execute into this directory
and we need X to be able to do CD
pictures for example and enter into the pictures directory.
Now, when directories are meant to be accessible,
you'll normally find both the R and the X permissions enabled.
Now, whenever you're on a Linux system, you're logged in as a particular user,
we've changed permissions in an interesting way to make this easier to understand.
Look at the permissions for the family dog JP G file.
It's set to read only for the owner, read,
write for the group
and no permissions for others.
And we can see that the current owner of the file is Erin
and we know Aaron is a part of the family group.
Can Aaron write to this file considering the
fact that the owner has read permissions only,
it might seem that he should be able to do that as he's part of the family group.
And that group has RW dash permissions which are read, right?
But if we try to add a line of text to this file, it fails. Why is that?
Because permissions are evaluated in a linear fashion from left to right
with these permissions in mind.
Let's see how the operating system decides if you're allowed to do something.
It goes through a logic like this
who is trying to access this file. And in this case, it's Aaron who owns this file.
OK. Current user Aaron is the owner.
So owner permissions apply,
which is R.
Aaron can read the file but cannot write to it.
So write permission is denied
moving on to group permissions
is Jane in the family group. Yes.
OK. Then group permissions apply
and Jane has read write permissions so she can read and write to the file if the user
trying to access the file is not the owner and is also not in the family group.
The last three permissions would apply the permissions for other users.
Now that we have a basic understanding of permissions.
Let's move on to how we can change them to suit our needs
to change permissions. We use the chmod command.
The basic syntax of the chmod command is chmod permissions, file and directory
and we can specify these permissions in many ways.
Let's start out with simple examples.
We saw that our owner, Aaron could not write to the file. So let's fix that
to specify what permissions we want to add. On top of the existing ones,
we use this syntax
to add permissions for the user.
We do U plus
list of permissions,
for example, U plus W or U plus RW or U plus RWX
to add permissions for the group. We would do G plus list of permissions
and to add permissions for other users. We would do O plus list of permissions.
Now, in our case, we want to add the right permission for the user owner of the file.
And now the old R dash dash becomes RW dash with the newly added right permission.
So we fixed our problem and now Aaron can write to this file
now to remove permissions for the user.
We do U dash list of permissions, for example U dash W or U dash RW or U dash WX
to remove permissions for the group,
we'll do G dash list of permissions
to remove permissions for other users. It's O dash list of permissions.
Now, at this point, we have the permission R dash dash for other users.
That means anyone on the system can read our family
do JPG file
if we want only the user owner and the group to
be able to read it but hide it from anyone else.
we can remove this, our permission.
Now, only Aaron or the family group can read this file and no one else.
Now with plus and minus,
we saw that we can add permissions on top of
the preexisting ones or remove them from the preexisting ones.
So if a file has RWX and we remove X, we end up with RW dash.
If another file has R dash X
and we remove X, we end up with R dash dash.
If we only care about removing the execute permission
and we don't care what the other permissions are.
This is perfect, but sometimes we'll have a different requirement.
We'll want to make sure that permissions are set exactly to
certain values and we can do this with the equal sign
just like before this is done with the format U equals list of
permissions or G equals list of permissions or o list of permissions.
For example,
we wanna make sure that the group can only read this file but
not write to it or execute it so we can run this command.
We can see that before group permissions were RW dash.
We didn't tell chmod to actually remove the W permissions.
But by saying G equals R, we told it to make the group permissions exactly R dash dash.
This only affects the group permissions and not the user or other permissions.
If we want to let the group read and write but not execute, we'd use this command.
We can see that whatever letter is missing,
they will make chmod disable permissions for that thing.
No X here means no execute permission will be present on the file
which leads us to the next thing.
What if we omit all the letters? No R no, W no X,
this would disable all permissions for the group.
This is like saying make group permissions all empty.
Now another command that does the same thing is this one,
it does the same thing but follow another logic,
remove all of these permissions from the group RW and X.
Now we saw how to add permissions with the plus sign,
remove them with the dash sign or set exactly two with the equal sign.
We can group all of these specifications into one single command by
separating our permissions for the user group and others with a comma.
For example, let's consider this scenario.
We want the user to be able to read and write to the file.
We don't care if execute permission is on or off.
We want the group to only be able to read,
which is exactly this permission
and we want others to have no permissions at all.
Now, our command could look like this
or
let's say we want the user to only be able to read and write.
But we want to remove the write permissions for the
group and leave all other group permissions as they were.
We don't care about permissions that apply to other users.
We would use this command.
Now,
chmod supports another way to set and modify permissions through octal values.
First, let's look at another command that shows us permissions.
Here's a list of permissions displayed by stat.
We can see that RW dash R and a set of dashes has an octal value of 640
and ignore the first zero because that's for special permissions like set
UID set,
Gid and sticky bit which we'll discuss in a later lesson.
If we break this down 640 means the user owner permissions are six,
the group permissions are four
and the permissions for other users are zero.
Now, how are these calculated? Let's take a closer look at this permission.
We now have RW for user
R for group and none set for others. Each permission is represented in binary.
If it's set, the binary is set to one or else it's set to zero.
Now, in this case, the first part has 110,
the second part has 100
the third part is zero.
Now, converting this binary to a decimal would give us six for the first part
four for the second part
and zero for the third part. Here's a quick binary table for your reference.
Now let's take another example
this time, it's RWX and R dash X with R dash X.
So the binary format would be
111101101
the decimal which
is 755.
Now, in the last example, it's set to read, write, execute for all.
So it's one for all bits. So the decimal value is 777.
If you find binary difficult, another approach would be to use the octal table.
It's much simpler
for each permission to sign an octal value.
For example, four for read two for write and one for execute,
then whichever permission is set, consider the respective value for that
and for the permission bit not set, consider zero.
And once done add up all the numbers within each group
like four plus two equals 64 plus zero plus zero is four and the last group is zero.
Let's look at using the same approach for other examples as well.
Now RWX and R dash X with R dash X gives us 755
and RWXRWXRWX
gives us 777.
Now once we identify the number, we want to set it to,
we can use the same in chmod commands as well.
So instead of specifying the permissions for each group,
we could provide a number like this and that covers reading and setting permissions
and that's all for now and I'll see you in the next one.