Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now that we understand how permissions work, how do we go about changing them? Well, you remember with ownership, we used chown, change ownership. We are going to use chmod to change permissions. It stands for change mode and that's what Unix refers to the mode of these files is being what it calls to permissions. So chmod and then we are going to put in whatever the mode is. I am just going to write the word mode as placeholder, we will talk about what the mode is, followed by the file name and so we will just put file name. That's the format. chmod, whatever changes to the mode we want, and then the file name.
So let's look at some example of what that mode might look like. So first, remember that we have three categories. We have user, group, and other. Notice the first letters of those. u, g, and o. We are going to be using those first letters to indicate where we want to make changes to, which category should get changes. Okay, so u, g, and o, r, w, and, x. So the first one I want to look at is going to be changing user, group, and other and setting them all equal to something. All right, so it can provide whatever three letters I want there. I am going to say r, w, and x. This is going to give all of them all three of those.
Now most of the time you don't want to do that. Most of the time you are trying to set different permissions for different people. So another way to write that out would be to say u= and give it whatever permission is for u, followed by a comma then the g= whatever permissions it should have and then o for other. User, group, and other. Commas separating them. That's a long thing to type and I don't know anyone who really types it out that way. Typically when you are changing the permissions, you know what the current permissions something are. what you want to do is change it to something slightly different.
You are like, oh, you know what, I wish that the user and group both had write permissions to this file. Well, to do that, you can just use the plus sign. Keep the existing permissions exactly like they are. All I am interested in is for user and group add the w permission. We can also do the same thing subtractively. We can say all right, I want to take away the write permission from other. So plus and minus, it's typically the way people work. Instead of spelling it all out for each one, typically you just say all right here are the changes that I want to make.
Now as I said, u, g, and o are how we define the three categories that we are targeting. There is one other shortcut I want to show you which is, is if you are trying to do something to all three of them and you are using ugo, you can also use a, so that's a shortcut. Those last two do the exact same thing. a means u, g, and o altogether. Let's try it out, so you can get the hang of it. Okay, I am going erase this line, because that's not going to do anything for us. Let's do ls -la so we can see the current permissions and I am going to really target everything on this ownership.txt. You can use any file you want. You can just create a new file.
You can see that the current permissions are read and write for the owner and just read for the group and other categories. So to begin, let's do chmod and let's do user group and other equals, so we can see what that looks like and let's say r and x. And we will do it to ownership.txt. So that's typically what a chmod statement looks like. chmod, then what we want to do to it, followed by the file name. So we are going to add essentially an x to the last two. ls -la and now you will see that the x has been added to those two.
You see that actually took the w away from the owner as well, because I set it to equals. So now let's try it slightly different. let's do chmod u=rwx and let's do group = rw and other. It's going to be equal to just r for ownership.txt. ls -la again. Now you can see how we changed it using that. All right so we broke it up using commas. We can also add to it, for example. Then we can say chmod user group and other, plus x for ownership, ls -la.
There we go, you see that it added the x to it and let's just do chmod a-x so you can see what that does. ls -la again. You can see that it took back away the x. So now that you have the hang of how we specify these, let's see what the actual practical affects are of these. Let's try chmod and let's do user group and other. Let's take away their read privileges. Okay, so now we don't have the ability to read anymore. We just clear the screen so you can see it. So there we are. We don't have read privileges anymore.
Let's try nano ownership.txt. And notice that it pop opened nano but look at the message that it gives us at the bottom, permission denied. So I am inside nano, but I can't do anything. I can type here, but I am not actually inside that file and when I say save, it's going to save to a new file. It's not actually reading the contents that file into the buffer for me to change. I am just essentially opening nano. So let's try now changing the ownership so that, say chmod, let's say user group and other ought to be equal to read for ownership.
All right, so now we only have read permissions. We don't have write permissions. Once again, let's try opening up nano ownership.txt. So it opened it up. It read it fine. There is nothing inside this file to begin with, but it did successfully open it. Let's say hello and now let's do exit, save buffer, yes. File name to write. Now I have the opportunity to provide a different file name but I am essentially now about to write on to this file, and I hit Return. It says, "Sorry, permission denied." So we can't write to it.
We can write to a different file name if we want, we still have the ability to save our changes, but we can't overwrite ownership.txt because we don't have write privileges. So I will just say no to saving those changes and let's go ahead and just set it back to chmod and we will make user gain its write privileges back on ownership.txt. Let's take one last look at it and there we go. The other thing I just want to mention to you is that in addition to doing changes like this, if we have a directory and we want to change this to the directory, let's say that we want to do chmod, let's add the write attribute for group, and we will write that for test.1 for the test directory.
If we do this right now, it will change it just for that top level one, same as the ownership did. If we wanted to change it to everything that's inside of there, we have to also include the recursive flag. So that's the option -R and that will recursively change everything that's inside of there, directories and files all the way down the line. So I just want to make sure you know we have that recursive option available to us. That's important if you are trying to change everything that's inside of that directory to have the same permissions. That's really all you need to being able to manage permissions in Unix. You now understand what they are, you understand how to read the notation that it gives you there on the directory listing, and you now know how to manipulate them, how to enable and disable permissions for those three categories one way or another.
But before we move on, I wanted to take a look at how we can also specify these permissions using something called octal notation and we will do that in the next movie.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98495 Viewers
61 Video lessons · 85787 Viewers
71 Video lessons · 69684 Viewers
56 Video lessons · 102001 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.