chmod – change permissions of a file or directory.
The chmod is an essential command in any linux environment. The chmod command allows you set access permissions on files and directories. File permissions allow you to set access to files and directories. In the example below the first letter tells us if we’re looking at a file or a directory. If we’re looking at a directory it will be denoted with the letter “d”, otherwise it’ll be a file and simply denoted by a “-”. This is a really easy way to tell if you’re looking at a directory or a file. As for the permissions themselves they’re broken up into three main sections. We’ve got “u” which denotes the user/owner of the file, “g” which denotes the permissions a specific group has, and “o” which denotes all other users on the system. Each one of these sections has a “rwx” flag which denotes “r” read, “w” write, and “x” execute. When “x” is used on a directory it doesn’t mean “execute”, it means to “traverse” in to the directory. What this basically means is that you’re able to allow the user, group, or other to traverse into the directory.
chz@cmdlist:~/Downloads$ ls -lah total 103M drwx------ 4 chz chz 4.0K Jan 22 17:54 . drwxr-xr-x 31 chz chz 4.0K Jan 23 13:23 .. drwxr-xr-x 16 chz chz 4.0K Jan 22 17:55 midori-0.4.3 -rw-r--r-- 1 chz chz 907K Jan 22 17:53 midori-0.4.3.tar.bz2 drwxr-xr-x 6 chz chz 4.0K Oct 25 20:56 OpenELEC-ION.x86_64-1.0.2 -rw-r--r-- 1 chz chz 102M Jan 22 12:14 OpenELEC-ION.x86_64-1.0.2.tar.bz2
Still feeling a little lost? It’s ok. We’ll break down the example above to get a better understanding on how permissions work. Let’s take the third line in our example above. As you can see the first letter is “d” so we know midori-0.4.3 is a directory. Next we have “rwx” which means our user/owner has read, write, and execute permissions. Then we have “r-x” which means that our group, in this case “chz”, has both read and execute permission. Finally we have “r-x” again which would mean that all other users have read permission, and the ability to traverse the directory. However, they’re not allowed to write to the directory.
d rwx r-x r-x 16 chz chz 4.0K Jan 22 17:55 midori-0.4.30
With linux there are several ways to accomplish the same task. This is no different when it comes to chmod and permissions. We can modify our permissions by executing something like “chmod g+w midori-0.4.30.tar.bz2″, this would allow the group “chz” to write to the “midori-0.4.30.tar.bz2″ file. However, we could also use octal (read numerical) values to represent the same thing as “chmod g+w”. Most would say that using numerical values to change permissions is the quickest way to adjust file and directory permissions. Let’s take a look at how exactly the numerical numbers are broken down. As you can see in the example below I’ve added a series of three digit numbers. To convert the numbers we’ll use a simple formula. The read permission is equal to the numerical value “4″, the write permission is given the numerical value of “2″, and finally the execute permission is given the value of “1″. Therefore, if we were to breakdown our example below we would be give have the permission “755″ for our midori file. How did we get this? It’s pretty simple, we’ll just add all the numbers up. So for example 4+2+1=7, 4+0+1=5, and 4+0+1=5. This gives us our numerical value of 755.
- rwx r-x r-x 16 chz chz 4.0K Jan 22 17:55 midori-0.4.30.tar.bz2 421 401 401
Examples:
So now that we know a little bit about how permissions work, let’s take some of that knowledge and put it to use. First let’s change permissions on our midori-0.4.30/ folder so that any users in the group “chz” can write to the directory. To do this we’re going to simply execute the command below. The “g” stands for group, the “w” stands for write, and both are joined by + which literally means group (plus) write.
chz@cmdlist:~/Downloads$ chmod g+w midori-0.4.3/ chz@cmdlist:~/Downloads$ ls -lah total 103M drwx------ 4 chz chz 4.0K Jan 22 17:54 . drwxr-xr-x 31 chz chz 4.0K Jan 23 14:01 .. drwxrwxr-x 16 chz chz 4.0K Jan 22 17:55 midori-0.4.3
In this example we’re going to do the exact opposite. This time we’re going to remove the write permission to the midori-0.4.3/ directory. To do this we’ll simply add chmod g-w midori-0.4.3/. The “g” stands for group, the “w” stands for write, and both are joined by – which literally means group (minus) write. Thus removing the write permission for the group.
chz@cmdlist:~/Downloads$ chmod g-w midori-0.4.3/ chz@cmdlist:~/Downloads$ ls -lah total 103M drwx------ 4 chz chz 4.0K Jan 22 17:54 . drwxr-xr-x 31 chz chz 4.0K Jan 23 14:01 .. drwxr-xr-x 16 chz chz 4.0K Jan 22 17:55 midori-0.4.3
What if we want to modify the permissions for user, group, and others all at the same time? Well there are two different ways we could do this. We could continue using something like ugo+rwx, which would literally convert to user, group, others (plus) read, write, execute. Or we could simply use the “a” flag which represents “all”, meaning user, group, and others. Check out the example below. Do note that it’s not a great idea give write permissions to others on your system, as one could modify your files or directories.
chz@cmdlist:~/Downloads$ chmod a+rwx midori-0.4.3/ chz@cmdlist:~/Downloads$ ls -lah total 103M drwx------ 4 chz chz 4.0K Jan 22 17:54 . drwxr-xr-x 31 chz chz 4.0K Jan 23 14:01 .. drwxrwxrwx 16 chz chz 4.0K Jan 22 17:55 midori-0.4.3
Now we’re going to repeat the same examples as above. However, this time we’ll modify our permissions using numerical values. Ready? Let’s get started. As you can see we issued the chmod command followed by 775. This will give read, write, and execute permissions to both the owner and the group associated with the file. However, all others will only have read and execute permissions. How did we get 775? Simple. Since we know read=4, write=2, and execute=1 if we add up the user values we’ll get 7. Then if we add up the group attributes we get 7. Finally, if we add up the others attributes we get 4+1 which gives up 5. Now if we put all those numbers together what do we get? If you answered 775 you’re right!
chz@cmdlist:~/Downloads$ chmod 775 midori-0.4.3/ chz@cmdlist:~/Downloads$ ls -lah total 103M drwx------ 4 chz chz 4.0K Jan 22 17:54 . drwxr-xr-x 31 chz chz 4.0K Jan 23 14:01 .. drwxrwxr-x 16 chz chz 4.0K Jan 22 17:55 midori-0.4.3
In this example we’re going to do the exact opposite as the previous one. This time we want to remove the write permissions for the group “chz”. To do this we’ll simply add up the numerical values. Are you wondering how we get 755? Simple. Since we know read=4, write=2, and execute=1 if we add up the user values we’ll get 7. Then we want read and execute which combined would be 5. Finally, if we add up the others values we get 4+1 which gives up 5. I bet you already know the answer. That’s right it’s 755!
chz@cmdlist:~/Downloads$ chmod 755 midori-0.4.3/ chz@cmdlist:~/Downloads$ ls -lah total 103M drwx------ 4 chz chz 4.0K Jan 22 17:54 . drwxr-xr-x 31 chz chz 4.0K Jan 23 17:13 .. drwxr-xr-x 16 chz chz 4.0K Jan 22 17:55 midori-0.4.3
In this example we’re going to give read, write, and execute permissions on midori-0.4.3/ to the owner, group, and others. This is obviously not a good, as we don’t want normal users altering our files. Any idea how we would accomplish this by using numeric values? Yeah, I figured you did. We’ll simply use 777, which gives read, write, and execute to not only our owner, but also our group and all others.
chz@cmdlist:~/Downloads$ chmod 777 midori-0.4.3/ chz@cmdlist:~/Downloads$ ls -lah total 103M drwx------ 4 chz chz 4.0K Jan 22 17:54 . drwxr-xr-x 31 chz chz 4.0K Jan 23 17:13 .. drwxrwxrwx 16 chz chz 4.0K Jan 22 17:55 midori-0.4.3
In this example we’ll use the chmod command with the added -R flag. The -R flag means to do recursively. So what does this mean? This means that if we add -R we can change permissions not only on a directory, but also all the files and directories within it. Take a look at the example below.
chz@cmdlist:~/Downloads$ chmod -R 777 midori-0.4.3/ chz@cmdlist:~/Downloads$ ls -lah total 103M drwx------ 4 chz chz 4.0K Jan 22 17:54 . drwxr-xr-x 31 chz chz 4.0K Jan 23 17:13 .. drwxrwxrwx 16 chz chz 4.0K Jan 22 17:55 midori-0.4.3 chz@cmdlist:~/Downloads$ cd midori-0.4.3/ chz@cmdlist:~/Downloads/midori-0.4.3$ ls -lah total 292K drwxrwxrwx 16 chz chz 4.0K Jan 22 17:55 . drwx------ 4 chz chz 4.0K Jan 22 17:54 .. -rwxrwxrwx 1 chz chz 3.5K Dec 17 16:47 AUTHORS drwxrwxrwx 3 chz chz 4.0K Jan 22 17:59 _build_ -rwxrwxrwx 1 chz chz 29K Dec 17 16:47 ChangeLog -rwxrwxrwx 1 chz chz 2.8K Dec 17 16:47 configure
For a full list of options type:
man chmod


