CHMOD: Tool for Open Source Administration

CHMOD (chmod) is a UNIX oriented command that means “Changing Mode.” It lets you change the mode (hence the name) of the file, or directory(ies) 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.

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 from chmod.

Another point I would like to make is, as much as I am a proud MS Windows user, the UNIX/Linux commands that are offered are far more powerful than those offered by MS Windows. There are more options in UNIX/Linux. For example,  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 minor comparison and 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 it. I’m also not comparing it because the Mac uses Darwin Linux. This is simply a different flavor of Linux (BSD), so most of the UNIX/Linux commands will more than likely work with a Mac.

{mospagebreak title=chmod Reasoning}

So why use chmod? Do I have to? Does it benefit me? Use it because you should. Heck yes, it benefits you! 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, and for giving certain privileges for certain tasks to certain users/groups, while still keeping settings for yourself (the owner). Also, chmod, as most people that are reading this know, is used a lot 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 if you don’t use the command.

It benefits you in many ways; so many that I can’t list all of them. 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 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 article.

In a UNIX/Linux environment (or if you had direct Shell access) you would use the chmod command directly as if you were 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.

{mospagebreak title=Switches}

Switches are also known as line options in Linux. These line options are used to get output by the main command (our case being the chmod command). I’m going to explain only two switches, but remember, there are 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. As I said, it is very useful. Suppose that we have a folder called Test and it has four sub-directories called: Production, Alpha, Beta, and PR, respectively. The problem is, we are limited in 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.

GUIs (Graphical User Interface) are cute, but also very slow. A directory at a time is still slow, even though it is much faster than using a GUI. So our only alternative is to 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 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 I will discuss using the -v (verbose) switch. This is probably one of my favorite (still useful) commands that UNIX/Linux has, and is usually used for a lot 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, and any errors is has along the way.

Again, suppose that we have a folder called Test and it has four sub-directories called: Production, Alpha, Beta, and PR, respectively. To really show you output of this, you should try it out for yourself. It’s neat, but looks crazy. You would use the -v (verbose) switch in case you are a computer nut and want to just see what’s happening, need to find out why something isn’t working (troubleshooting), or 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 go any further, 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 difference 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.

{mospagebreak title=Modes}

Basically modes are a simple way of saying permissions. There are a few exceptions to this rule, but they aren’t part of this article. There are three basic modes: -r (read), -w (write), -e (execute). See the chmod figure.


CHMOD Figure

Later in this article, I’ll explain 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: the first number is for the owner, the second for the group, and the third for the world. Depending upon 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 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 or tea. If you’re avoiding caffeine, 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 put, an octet is eight bits of data. So in an IP address you have your localhost (127.0.0.1). You have four 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, which is 1 bit of data. With an IP address, however, it is viewed as eight bits in binary, so it would look like 00000001, thus the eight bits – octet (Figure 1).


Figure 1

So what’s this have to do with chmod? Well, in order to make permissions with 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, and so on. We only need the first three bits of binary: 1, 2, and 4. This is a simple structure of the chmod’s octet.

{mospagebreak title=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, and execute? That’s what we use. Look at Figure 2 to see this in a graphical implementation.


Figure 2

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.


Figure 3

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 the 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.

Using the Octet version of chmod in UNIX/Linux

Now that we know how to convert from mode permissions using -r, -w, -e to 777 (note: it’s 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 the 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 article and that it helped you.

[gp-comments width="770" linklove="off" ]

antalya escort bayan antalya escort bayan Antalya escort diyarbakir escort