[MUSIC]
>> Linux permissions can be a bit confusing,
so let's demystify that by learning how
to modify permissions using Bash.
I'll begin with an overview of Linux permissions.
To see the permissions on a particular directory or files,
you can use ls-l,
and that l option will include the permission set.
At the very start of the line is the file type.
Whenever there's a hyphen,
that means that it is a file and if you see d,
that stands for directory.
Anything with d across tier that is also highlighted in
green is a directory and anything with a hyphen is a file.
That's a first position means.
Now the next set, this rw here is the user's permissions.
The user that's assigned to this,
these are the permissions that they have.
The next is the group permissions.
The group that is assigned to it,
that's the permissions that they have,
followed by other permissions.
If you aren't in the group or you aren't a user that's assigned
these permissions then the other permissions
is what you get assigned.
What follows those permissions is the number of
hard links and that's not particularly
useful in setting permissions.
But to the right of that is the owner.
The owner of this file is vscode and
the owner has the most permissions of anyone.
The owner can read, write,
execute, but most importantly,
they are also the one that has the ability to
change the permissions and to grant
permissions on the file or folder.
After the owner is the group owner.
Just like the owner,
the group members of
the group owner group can do the same things that the owner does,
so there's owner and owner group.
Now, what follows is the number of bytes
or the size rather of the file,
and then the date modified,
and lastly, the filename.
To reiterate these first groups here are what's
most important when you're setting the file permissions.
Again, this is the user permissions.
If you're going to assign a specific user permissions,
this is what they get for the file.
If you're going to assign a group permissions,
these are the permissions that group would get.
Then other, if they're not
a user assigned or a group-assigned permissions,
these are the default permissions that a user would get.
Now with that review out of the way,
let's move on and take a look at how we can change
the ownership of a file or a directory.
If you want to follow along with this exercise,
go to the root of this whole workspace,
the Bash for beginners,
and then change into a modifying file permissions.
Then inside here, there will be
a directory and a file that we'll change the permissions on.
The command that you need to know to change
the ownership of a file or directory is that chown command.
If we were to type chown and look at
the help for this command
and scroll that up so we could see how to use it,
we can see that we could provide the options,
but then the syntax for this is
the owner or group and then the file name.
But notice when you specify a group that
you also need to include colon.
We'll take a closer look at that in just a second.
I'll clear my screen and let's take
a look at the newFile's permissions.
Currently, the owner of this file is vscode.
I want to change that to the root user.
To do that, I'm going to need to use sudo because I'm elevating to
the root user and I'll use the chown command,
so C-H-O-W-N. Then I need to specify the new owner,
which would be root, and then the name
of the file, which is newFile.txt.
Now when I take a look with ls-l again,
I'll see that the owner has been updated to root.
That's how you change the user who owns the file.
But what if you wanted to change the group?
The next thing here is there's
this vscode group that also is an owner,
but I want to change that to the root group as well.
To do that, I'll still need to use sudo,
but I'll use chown and then I'll use a colon in front.
This denotes that I'm changing a group and not a user.
I want to assign the group root as the owner of the newFile.txt.
Now when I take a look at a permission set for newFile,
I can see that the user root is the owner
and the group root is the owner, the group owner.
There is another command that you can use if
you don't want to use the chown command to change group,
and it's called the Change Group command.
But to do that, we will have to change back the owner
of the newFile back to vscode.
We'll do sudo,
chmod or chown and
then the name of the user which we want to
be vscode and then newFile.txt.
Now vscode is the owner again because without
ownership permissions you can't use the Change Group command,
that's one of the permissions that's granted to the owners.
They are the ones that have the ability to change
the permissions on the file.
Now I can change the roof.
We do ls-l, newFile.
We can see that vscode as the owner,
but the group is written,
I want to change that group back
to vscode with a different command.
It's the change groups so
C-H-G-R-P is how you as spell that command.
Then you just put the name of the group,
which is vscode in this case
and then the name of the file that you want to change,
which is just newFile.
If we take a look at the permissions for newFile,
it's back to the way it was where the owner is
vscode and then the group owner is also the group vscode.
The chown in change group command
deal with the ownership of a file.
But what about the different permissions of a file?
Well, the command to modify the permissions in Bash
is chmod, so C-H-M-O-D.
So there are two ways that you can use the chmod command.
You can use letters to represent the permissions,
or you can use the octal format.
In this directory,
I have a script named script.sh.
Right now it only has read,
write permissions for the user,
the group, and then other,
so if I tried to execute this script.
I'm going to get a permissions denied error.
Now I can use the octal format or I
can use just the letter to represent the permissions.
I want to assign xq permissions to
everyone or every one of those permission set.
To do that, I would use the chmod plus and
then x for adding the xq permissions.
If I wanted to put, read I put r,
but I'm adding so I'm using
the plus sign and then the name of the file.
Now if I look at the permission set for script now,
you'll see that it has read, write,
execute across all three different groups,
so user, group permissions, and other.
Now when I go to execute the script, it works.
Now let's say I wanted a little bit more control over
the permissions that I was setting and I want to be
able to set the owner permissions,
the group permissions and the other permission at the same time.
That's where the octal format comes in.
To see an example of this,
we can look at this octal.txt.
This can be found in the sample files or
this repository in this codespace.
I'll walk through how you would set those different permissions
using the octal format because it's
a little bit confusing at first.
It's a bunch of numbers that you put
together that assign the permissions.
But it's really not that bad once you break it down.
Remember the first placeholder is
for the owner of the file or directory.
Then next is the group,
and then the final one is others,
anyone that's not been explicitly assigned permissions.
If you look at this, the read, write,
and execute each have a value assigned to them,
a numeric value assigned to them.
The r's worth four points if you think of it that way,
the w is worth two,
and the x is worth one.
You've probably seen this before where you put chmod 777.
That's just giving everybody
all the rights all the time so you don't have to worry about it.
An active directory would be equivalent like a Domain Admin,
you just have all the keys of the kingdom.
But hopefully after this,
you'll be able to give a little bit more fine-grained
permission sets because you'll understand it.
Again, if we wanted to lower the permissions on
the script file and remove all xq from everybody,
we could give them a read and write,
which would be a total of six, so 4+2=6.
That would be the first place holder.
This is for the owner.
The next would be the group.
If we wanted to give them read and
write for the group, that would be six.
But then for everybody else,
we don't even want them to be able to write,
we want them to be able to read,
and that would be four.
To change those permissions to
a lower permission set or more constrained permissions,
we will use chmod 664,
which would be the octal format, and then script.
Now when I try to run script,
I'll get the permission denied error again because I no
longer have xq as an owner or part of a group or other.
To add those back, another very common use
of the chmod is the plus x,
which gives the xq permissions.
If I do that again,
everybody has xq and the permissions are changed.
Now I can run this script.
[MUSIC]