Understanding CHMOD and Creating permissions without a calculator

CHMOD (chmod) is a UNIX oriented command that means “Changing Mode”, that let’s you change the mode (hence the name) of the file, or directory(s) depending on how the chmod command is used. When I say “changing mode”, I mean giving the file a different permission scheme, respectively read, write, and execute. This command, by the way, is one of the most useful and most used commands in Linux (among many others).
UNIX/Linux vs. MS Windows
Before getting into syntax and the whys of the whole chmod command, I want to get one thing straight. Changing modes do not exist to this extent in MS Windows. There is a similar model that windows uses in its Management and Active Directory services; using users and groups. In UNIX/Linux it’s quite a bit different though. chmod is not used in MS Windows. In MS Windows, the attrib command is used, and is (again) quite different then chmod. Another point I would like to make is as much as a proud MS Windows user I am, the UNIX/Linux commands that are offered are FAR more powerful than MS Windows. There are more options in UNIX/Linux and switches are case sensitive so you can have even more switches than in MS Windows, as MS Windows is NOT case sensitive. This is just a very little compare/contrast between UNIX/Linux and MS Windows. The main reason I’m not comparing the Mac is because I really haven’t had much experience with the Mac, but also because the Mac is built upon Darwin Linux, so most of the UNIX/Linux commands will more than likely work with a Mac.
chmod Reasoning
So why use chmod? Do I have to? Does it benefit me? Because. You should. Heck YES! To better understand the reasoning behind the chmod command, you’ll have to keep reading.
You’ll want (need) to use the chmod command for changing permissions for certain network files, for giving certain privileges for certain tasks for certain users/groups, while still keeping settings for yourself (the owner). Also, chmod as most people that are reading this know, is used ALOT when using FTP (File Transfer Protocol). In FTP, you would change the mode (change permissions) to certain directories/files so that the web server services can read/write/execute from the server, which in this case would be a UNIX/Linux server.
You don’t have to use the chmod command, but I guarantee you that you will probably fail at what you are trying to do and/or lose your job (just kidding).
It benefits you in many ways, so many I can’t list. To name a few things, you can set privileges to certain folders/directories/sub-directories for not only yourself (owner), but also groups and the world (other). As I said above, using chmod, it lets you change file permissions for the web allowing the read/write functions of a program written in such languages as PHP or CGI/Perl and other server side languages.
Using chmod in Linux
Now we can start using the keyboard and start something a little more interesting than the basics of the chmod command and why to use it. You can use the chmod command in a web server environment in two cases: if you have shell access or if you are hosting your website on your machine and have access to the computer directly. If you didn’t have this kind of access, then you would use the chmod command in an FTP environment. FTP is not covered in this write-up.
In a UNIX/Linux environment (or if you had direct Shell access) you would use the chmod command directly as if you would in the actual environment, unless you are in the environment, in that case keep reading. The chmod command is structured with the command name followed by switches, then modes, then files/directories. Here’s the raw markup:
BASH
chmod [switches] [mode] [files]
This is a raw markup, meaning that it with not work, it just shows you how you would structure your bash command for using the chmod command. Next we are going to talk about switches.
Switches
Switches are also known as line options in Linux. These line options are used to get out put by the main command (our case being the chmod command). I’m going to explain only two switches, but remember, there’s so many resources out there that can show you more switches. The switches are the -R (Recursive) and -v (verbose) switches. NOTE: UNIX/Linux is case-sensitive!
The -R (Recursive) switch is a very powerful switch. It allows you to recursively (ahh, that’s why they call it that!!!) command the chmod command to not only change the mode to the directory you specify, but also the directory’s subfolders. Like I said, very useful. Let’s say that we have a folder called Test and it has 4 sub-directories called: Production, Alpha, Beta, PR; respectively. The problem is, we are limited on time, and we have to make these kinds of changes on a Linux machine to about 400 other Linux machines; so we have to be able to do it fast. GUI’s (Graphical User Interface) are cute, but are very slow. A directory at a time is still slow, even though much faster than a GUI. So our only alternative is, use the -R switch for chmod; remember we are changing modes. Let’s see how we would do this in an example:
BASH
chmod -R 755 /Test
What we are doing here is telling the chmod command to do is give the owner Read, Write, and Execute permissions; Groups that are assigned to the file get Read and Execute permissions; World (other) also gets Read and Execute permissions. We will go into modes later. While giving permissions to the three types on objects, we are telling it to do this to the Test directory and all of its sub-directories.
Next is using the -v (verbose) switch. This is probably one of my favorite (still useful) commands that UNIX/Linux has, and is usually used for alot of different commands. The -v (Verbose) switch just outputs what the chmod command is doing. It tells you it is assigning permissions, to each directory, where it is at, and any errors is has along the way. Again, let’s say that we have a folder called Test and it has 4 sub-directories called: Production, Alpha, Beta, PR; respectively. To really show you output of this, you really should try it out for yourself. It’s neat, but looks crazy. You would use the -v (verbose) switch in the cases that you are a computer nut and want to just see what’s happening, need to find why it isn’t working (troubleshooting), or you need to just make sure everything is doing what it is suppose to be doing. Let’s see how we would do this in an example:
BASH
chmod -v 755 /Test
Before I got any farther, remember what I said about case-sensitivity; I meant it, so be aware. Since you should know what the above example is saying, I’ll be brief. The only different with this command is it displays everything that is happening from the chmod command’s start to end. Everything else is the same. If you need to read over this section again, please do; we are now going into the really fun part, modes.
Modes
Basically modes are a simple way of saying permissions, there are a few exceptions to the rule, but that isn’t part of this write-up. There are three basic modes; -r (read), -w (write), -e (execute). See the chmod figure.

Later in this write-up, I’ll explain on how you can find out the number system for certain modes (permissions). Modes to a basic extent is very easy and simple. You have three numbers: first number is for the owner, the second for the group, and the third for the world. Depending on the numbers, you will get a certain permission assigned to the respective object. We will use the examples from above:
BASH
chmod -v 755 /Test
This states, unreguarding the verbose mode, that the owner is assigned Read, Write, and Execute permissions; groups that are assigned to the file/directory get Read and Execute permissions; world (other) also gets Read and Execute permissions.
Take a break. Drink some coffee. If your diabetic/hate tea, do 100 jumping jacks. Then come back for the fun part; binary/octet conversion.
The Structure of chmod octet
You see octets all the time. If you recall, an IP address is a series of four (4) octets. Simply an octet is 8 bits of data. So in an IP address you have your localhost (127.0.0.1). You have 4 octets: 127, 0, 0, and 1. We now have to go back to remembering binary. Even though it might be a one (1) it is still set in binary as: 0001, is 1 bit of data, but with an IP address it is viewed as 8 bits in binary, so it would look like: 00000001, thus the 8 bits - octet (Figure 1).

So what’s this have to do with chmod? Well, in order to make permissions using chmod using the numerical format, you have to know how to convert the permission (binary) to octet. Read = 4, Write = 2, Execute = 1. Do you see the pattern? If you don’t, look at Figure 1; it goes: 1, 2, 4, 8, 16, etc. We only need the first three bits of binary: 1, 2, and 4. This is a simple structure of the chmod’s octet.
Mentally Converting from “read/write/execute” to 777
Now we go through the process of converting the -r, -w, -e modes, to the numerical (faster) way of implementing a chmod command. Remember how I said that the first three bits in binary (read 4, 2, 1) are what are used for read, write, execute? That’s what we use. See Figure 2 to see this in a graphical implementation.

But that’s just one number. Yes, that one number is the first number, which is used for the owner. We still have two other permissions to set; one for group and one for world. You do the same for group and world as we did for owner; convert the textual (read, write, execute) permissions to the octet (777) permissions.
As we do this over the process, we come up with a situation. We need to set the following permissions for a file called test.txt: owner = read, write, execute; group = read, execute; world = read. This is our practical example. The permissions would be: 754. To get this follow Figure 3 as a guide.

So by way of the guide, you just add up the numbers that are “on” and leave out or put a zero (0) in place of an “off” number. You get your number and you go to the next object. When you are finished you come out with three (3) numbers; this is your chmod mode you are going to use. Remember, that your mode is just permissions, so if you want to set certain permissions, your mode will change.
<h3>Using the Octet version of <code>chmod in UNIX/Linux
Now that we know how to convert from mode permissions using -r, -w, -e to 777 (note: its not always 777, it could be 755; it’s dependent on your permissions), we can now use this methodology in a UNIX/Linux environment. Say that we want to do a basic chmod, giving owner read and write access, group write access, and the rest of the world read access to a file called text.txt. The following would be the correct way to do it:
BASH
chmod -v 624 test.txt
That’s It! Take if from here, explore more ways of using the command line syntax. Don’t just look at it from the chmod perspective, look up other commands and compare them. Most of all, have fun and learn something new! Hope you enjoyed the write-up and that it helped you or got you somewhat interested.