Linux file permissions FAQ: Can you share some examples of the Unix/Linux
chmod command? (Also written as, "How do I change permissions on Unix files and directories?")
chmod command name stands for "change mode", and as that name implies, the
chmod command is used to change the mode of Unix/Linux files.
I'll start with some simple examples, then add some more details as we go along.
chmod: Make a script executable
chmod command is commonly used to make a file "executable", like this:
chmod +x myShellScript.sh
Typically you create a new Unix shell script, then make the script executable with that command. Once your new shell script is executable, you can run your script like this:
chmod: Make files readable
Another use of the
chmod command is to make a file "readable". If a file doesn't have read permission, you can add read permission to that file like this:
chmod +r myfile.txt
This example assumes (a) you own this file or (b) you are the root Unix user (in which case you can change the permission on any file in the system).
chmod: Make files writable
In the same way, you can also give a file write permission, like this:
chmod +w myfile.txt
This example makes the same assumptions as the previous command, you either own the file, or you're logged in as the root user.
chmod and the ls command: File permission details
To really understand the details of the
chmod command, you need to understand the output of the
ls command. The relationship here is that the
ls command shows file permissions, and the
chmod command lets you change file permissions. I explain this in a lot of detail in my Unix ls command examples tutorial, so I'll just share the short story about file and directory permissions here.
In short, when you issue an
ls command with the "long" output option (
-l), like this, you'll see detailed output, as shown here:
$ ls -l *doc -rw------- 1 stu1 class 4892 May 7 12:18 sales.doc -rw-r--r-- 1 stu1 class 10874 May 7 11:27 mktg.doc
The permissions on your Linux files are shown in the first ten characters of those two lines. The first column doesn't matter for this discussion -- it shows whether the file is a real file, directory, pipe, or something else -- but the next nine characters show the file permissions.
Those nine characters can be broken up into three groups of three characters each:
- Fields 2-4 show the permissions for the owner of the file (the "user").
- Fields 5-7 show the permissions for the group the user belongs to.
- Fields 8-10 show the permissions for all other users on the system.
The first file in the example above shows these permissions:
This means that the owner of the file has read (r) and write (w) permissions on the file, and no other permissions are set, meaning they are denied.
The second file above shows these permissions:
This means that the owner of the file has read (r) and write (w) permissions on the file, users in the group have read permission (the 'r' in the 5th column), and all other users on the system also have read permission (the 'r' in the 8th column).
Linux chmod command examples (all users)
Given that extremely brief background on Unix/Linux file permissions, here is a collection of Unix chmod commands. In each example, assume we start with a file named foo.txt that has no assigned permissions, like this:
I'll then show the file permissions after the command I issue. First, in this example I'll add read permission to the file for all users:
chmod +r foo.txt -r--r--r--
Next I add write permission to the file for all users:
chmod +w foo.txt --w--w--w-
Here I add both read and write permission to the file for all users:
chmod +rw foo.txt -rw-rw-rw-
Finally, here I add read, write, and execute permissions for all users:
chmod +rwx foo.txt -rwxrwxrwx
Linux chmod command examples (file owner)
It's important to know that you can also control file permissions for the user (u), the user's group (g), and all others (o). Here's a command where I add read permission for the current user:
chmod u+r foo.txt -r--------
As you can see, only the user has read permission on the file now. Similarly I can add write permission like this:
chmod u+w foo.txt --w-------
and execute permission like this:
chmod u+x foo.txt ---x------
You can also add read, write, and execute permissions for the user with one command like this:
chmod u+rwx foo.txt -rwx------
You can do the same thing for the group:
chmod g+rwx foo.txt ----rwx---
and all others:
chmod o+rwx foo.txt -------rwx
You can also combine
chmod permission commands, like this:
chmod u+rwx,g+rw,o+r foo.txt -rwx-rw-r--
So far I've just shown "add" permissions with the plus sign (+) operator; this adds permissions to whatever permissions the file already has. You can also use the equal sign operator (=) to set permissions exactly, instead of adding to file permissions as shown so far. Here's a typical
chmod equal permission assignment:
chmod u=rw,g=rw,o=r foo.txt -rw-rw-r--
Whenever you need to subtract permissions from the permissions a file already has, you use the equal sign operator.
Linux chmod command numerical commands
In the old days that's the way I learned to assign file permissions. These days you can do the same thing with numbers. When using numerical
chmod commands you use a sequence of three numbers, and the numbers again correspond to the user, group, and owner of the file.
Here are some numerical
chmod command examples.
Add read permission to the owner of the file foo.txt:
chmod 400 foo.txt -r--------
Add read permission to the owner and the group for the file foo.txt:
chmod 440 foo.txt -r--r-----
and read permission for the user, group, and all others:
chmod 444 foo.txt -r--r--r--
As you can see, the number '4' implies 'read' permission.
You can do the same thing to add write permission using the number six, like this:
chmod 664 foo.txt -rw-rw-r--
And you can add execute permission to the file, like this:
chmod 774 foo.txt -rwxrwxr--
chmod 700 foo.txt -rwx------
Finally, if you want to set and read and execute permissions -- typically done on a directory -- you use this command:
chmod 755 MyDir -rxwr-xr-x
Unix/Linux chmod command examples - Summary
I hope these Unix/Linux
chmod command examples have been helpful. There's much more to say about the
chmod command, but I hope all of these examples are enough to help explain most things you'll see on a daily basis.