How do I change file permissions?
Author: Deron Eriksson
Description: This Linux tutorial demonstrates how to change file permissions using the chmod command.
Tutorial created using: CentOS release 4.6
In Unix, permissions for a file can be displayed using the long listing of the ls command (ie, "ls -l"). When doing a long listing, the file line begins with 10 characters. The first character indicates the type of file ("-" is a normal file and "d" is a directory, for example). The next 9 characters indicate the file permissions.
Characters 2-4 indicate the file permissions for the file owner, characters 5-7 are the file permissions for the file's group, and characters 8-10 are the file permissions for other users. Notice that the characters are grouped into groups of three. The first of these three characters indicates the read permission ("r" for read, "-" for no read). The second of these characters indicates the write permission ("w" for write, "-" for no write). The last character indicates the execute permission ("x" for execute, "-" for no execute).
The "chmod" command can be used to set the various read, write, and execute permissions for the user, group, and other users. "r" is given a value of 4, "w" is given a value of 2, and "x" is given a value of 1. No permission is given a value of 0. We can add the "r", "w", and "x" values together for each triplet to come up with a value representing the permissions for that triplet. Thus, 7 (4 + 2 + 1) indicates that read, write, and execute permissions are present for a particular triplet. 3 (2 + 1) would indicate that that write and execute permissions are present for a triplet. A command such as "chmod 755 mytestfile" would set the user permissions to read, write, and execute (rwx), the group permissions to read and execute (r-x), and the other user permissions to read and execute (r-x).
In the screen capture below, we can see various chmod commands run on a file called test. It starts with 755 permissions, is changed to 000 permissions, is changed to 777 permissions, and then changes to 641 permissions.
After this, notice that chmod commands are issued in a different format. "chmod o+r test" says to add read permissions to the other users. "chmod g-r test" says to remove read permissions from the group for the test file. "chmod a+x" says to add execute permissions to "all" (user, group, and other users) for the test file.