Learn Red Hat Linux Episode #14: File/Folder Ownership and Permissions Commands (chown, chmod)

Learn Red Hat Linux Episode #14: File/Folder Ownership and Permissions Commands (chown, chmod)

Welcome everybody to this tutorial. Today
I will talk a bit about permissions; how you can change them, how you can read
them, and what they are in general. So first off, let’s just go ahead and open
up a file. So, don’t worry about this command that I’m going to use now, it’s
called cat, I will explain it a bit later on when we go into text editing, and
phrasing, and extracting text, formatting it in general. But for the time being,
just leave it be. You’ll see soon enough what it does. It just pulls the content out
of a file and then prints it out to the screen. So, I have a file here, it’s called mode, and in that file I’ve made a small demonstration of, I wouldn’t say demonstration but an example of syntax of how this thing
actually looks like. So you pass in the command, so pass in the command
which is chmod. And then where it says owner, group, world and path, path is basically the path to the file of which you wish to change permissions.
Owner is changing the permissions for the owner. Group is changing the
permissions for the group. And world is basically changing the permissions for
anybody who can physically access the computer. I will shortly explain what
this actually is/. So let’s just let’s go ll mode, there we go. So down below you
see that the first spot signifies what is it. Is it a
directory, or is it a file, is it a link. That I’ve already explained. Next three
fields basically tell you the permissions which this user has. So what
what does the owner have. By the way, this here, this is the owner of the file. So, what sort of permissions does this owner have? It has a read permission, so it can read the file. You can see the contents of
the file would be a more simpler way of putting it. It has a write permission so w. This is basically
permission to edit and alter it, and modify the file, change it. It does not
have the permission to execute the file. Although, if I am the owner of the file I
can give myself the execute privileges as well. However, until I do so I will not
be able to execute it at all. I know that the mode file there is nothing to execute in it, but, still. Suppose there was a script
here which was to be executed, and unless I actually gave it execution privileges,
there would be no other way I could do this. Then, the next three spots signify
permissions for this particular group. So, any users belonging to this group will
have this set of permissions here. So, again, the group can read, it can
modify it, but there are no execution privileges for the group. Next
up it’s the world. And by world I don’t mean the whole globe, or anything of a
kind. Rather instead, I am referring to all the people who can actually log into
the computer. Everybody can read this file, everybody can view the
contents of this file without any problems. So, let’s just go ahead and
create a new file with touch. Just type in touch like this, and just how shall we
name this file? Uh, well, I don’t know. I am running out of inspiration here. So,
let’s name this file Linux. Actually, no, let’s call it RedHat. Excellent! So we
created RedHat, and let’s see what are the permissions for RedHat. Sorry,
not LS. Excellent! So we have the same set of
permissions as we do with the mode. Now let’s say we’re going to use this file
in order to run our tests on it. Before we do, let’s just go up a little bit and
see what basic permissions do we have. So as I mentioned before, we have read, write
and execute. Those are the three basic basic permissions that you can give to a
file, to a user, actually, or to a user group for a particular file. They
have their numeric values. You see these letters here. they also signify their values. They are also representations of
them, let’s put it so. However, numeric values are far better. You can use r, w, x, +, – in front to quickly alter the permissions for the owner of
that file. However, if you wish to conduct major changes you can do it in this
fashion. So let’s say that 7, it’s basically 4, plus 2, plus 1 as
stated here, is read write and execute. 6 would be 2 plus 4 which is read and write. 5 is 4 plus 1, so read and execute, and so on and so forth. And if this doesn’t seem
quite as clear as it should, don’t worry about it, a lot of people got
confused here. I got confused here but it all really sinks in rather fast, so no need to panic or anything of a kind. Let’s go ahead and clear the screen. I’m
gonna go ahead and cat this out, and let’s say that I want to change the
permissions of the file RedHat. So let’s do ll RedHat just so we have the overview of what they are at the moment. Now let’s
type as the upper syntax says we should. Let’s go ahead and type in the command, so chmod, and what sort of permissions
shall we give it? Well, let’s first think about what sort of permissions do we
want our user to have? Let’s say that we don’t want him to actually be able to
change the file, but I want my owner of the file to be able to read it and
execute it. Let’s go ahead and conduct such a change. So, read and execute would be 4 + 1, that’s 5. The first argument is 5. That’s what the owner of the file
can do. Let’s see, what now. What sort of permissions do we want to give for the
group? Well, I want the group to be able to do the same thing, actually. Let’s
not change anything here. We want a group to be able to read it and
to execute it. And the world, well, we’re not gonna really change anything for the
world. Yeah, read is way more than sufficient for the world, so let’s just
leave read as it is and give it 4. So it’s
chmod 554 that I have typed in, and then I type in the name of the file.
Also, notice that here it says path. Since I am in the same directory where
the file that I wish to modify is located, I could also, if I was elsewhere in the file system, I could type in, I don’t know, I
could go about things like this. So, /home/ RandomGuy/RedHat, and this would change it for home/RandomGuy/ RedHat. Excellent! So you see you can
compare to previous permissions to the current permissions. You see that this
has immediately changed. It says that the user can read and execute, the group can
read and execute, the world can only read. Suppose I want to quickly add write to the owner of the group. I would do chmod
without any calculations whatsoever, or looking at the numbers, or anything of
a kind. Just type in chmod +x, and here we’re gonna need the name of the file,
RedHat. Let’s see what we have done. Oh, actually, we haven’t done anything
because it already has execute
privileges. Sorry, I’m gonna add write privileges, that’s what I wanted to do.
Excellent! So how does this look like now. The user can actually read, write
and execute. So here it can read and not write and execute, and now we have
changed effectively for the group as well. The group now can actually read,
write and execute. And here read, write, sorry, up here it’s read, no write and
execute, but here it’s read, write and execute. Okay, we have changed our
minds, we no longer wish to give the write permissions. Let’s say it’s chmod
-w. What has happened? The w is gone as you can clearly see. So these are the
quick ways of changing the owner and the group that owns the
file as well. Initially I just said the owner, but it’s actually the group as
well. In this particular example, anyway. There are other things which we can do.
Let’s just go ahead and clear the screen with this, and type in chmod
– -help. So what do we have here? This is probably, the -v and
-R are probably the most important here, the ones that you will
mostly tend to use. So verbose output is just to tell you what it is that it is
doing, and recursive, well, just read what it does. Change files and directories recursively. So it just goes into the subdirectories of
directories, and then it goes into their sub directories, and then it finds files
there, and then it does alteration on the whole string. Let me just show you. So
we’re not gonna, let’s see what do we got here. First, do we have anything of no importance? So we got mode, we got RedHat, come on, something? Okay, oh, I do have a test folder. Let’s utilize the test folder.
Let’s see what it actually has to say on the subject. So what do we do? We type in, wait do I have anything in the test folder? Let’s just go ahead and check. If we don’t, we just need to create. Okay, so we
can go ahead and create with touch test1, test2, test3. Let’s go back a step, do
chmod, oops, I actually don’t know what they are first. So, ll TEST_FOLDER,
excellent! So they all have read, write, and no execute privileges. Let’s change
it to execute privileges, let’s give execute privileges. But if we print the
working directory, we’re not actually in the test folder at all. Rather instead, we
are outside of it. So how do we go about this? Well, just type in chmod as
you would ordinarily. Type in -R +x, and now we need a path of
the file which is basically TEST_FOLDER, and then change everything inside of it.
Excellent! So, let’s go ahead, there you go. So you can now compare these results to these results. It has changed everything
within that folder, and it has given it these permissions. But when you do it
like this, you see that the world changes as well, the group changes as well, and
the current user changes as well. So all the permissions are changed, which is not
the best of practices. What you can do with ease to avoid this is simply type
in chmod – R, and then use numbers as I have used them before. In that way you can be
absolutely certain what changes will take effect, and what will be changed to
what, who will be able to do what, etcetera. Anyway, here I’m gonna call this
tutorial. I’m gonna bid you farewell, and I sincerely hope that I shall see you in the next one.

7 thoughts on “Learn Red Hat Linux Episode #14: File/Folder Ownership and Permissions Commands (chown, chmod)

  1. Get The Complete Linux Administration Course Bundle! https://josephdelgadillo.com/product/linux-course-bundle/

  2. Get The Complete Linux Administration Course Bundle! https://josephdelgadillo.com/product/linux-course-bundle/
    Enroll in the FREE course! https://uthena.com/courses/hacking?ref=744aff

Leave a Reply

Your email address will not be published. Required fields are marked *