Unix File Permissions
By default, when you create a file or a directory in either
your home or work directory from within linux, only you will be
able to read and modify the object. If you wish to allow others
to read a file or list the contents of a folder you will need to
use the chmod command as described below. This
is especially true if you create files for displaying on the web
(in public_html).
File permissions are divided into three types:
|
r
|
permission to read a file or list the contents of a
directory |
|
w
|
permission to write to a file or add and delete files
to a directory |
|
x
|
permission to execute a file or cd into a
directory |
Each of these permissions is set for each of three types of
users:
|
u
|
the owner of the file |
|
g
|
members of the group to which the owner belongs |
|
o
|
all other users |
By setting the permissions, the file owner controls what users of
each type can and cannot do to the file. The permissions to a
file are listed as a string of nine characters like this:
| user |
group |
other |
| r w x |
r w x |
r w x |
You can view the permissions of a file by entering
ls
-l at the command line prompt. The output will be a list
of all the files and directories in the present directory. An
example of one line from such a list would be:
-rwxrw-r-- yourname
ns 3754 Feb 24 15:30
my.txt
The first (-) tells you if the
entry is a file(-), directory(d), or a link(l). It is followed by
the permissions list for the file, directory, or link. This is
followed by the user ID of the person who owns the file or
directory, in this case yourname. The owner's name is then
followed by the name of the owner's group (ns), the size of the
file (in bytes, 3754), the date the file was last modified (Feb
24 15:30), and the name of the file (my.txt).
File permissions are set or changed
with the
chmod command. There is a shorthand way of
setting permissions by using octal numbers. Read permission
is given the value
4, write permission the value
2
and execute permission
1. These values are added
together for any one user category:
| 1 |
001 |
- execute only |
| 2 |
010 |
- write only |
| 3 |
011 |
- write and execute (1+2) |
| 4 |
100 |
- read only |
| 5 |
101 |
- read and execute (4+1) |
| 6 |
110 |
- read and write (4+2) |
| 7 |
111 |
- read and write and execute (4+2+1) |
Access permissions can be expressed as three digits. For example:
| Command |
Result |
|
|
|
user |
group |
others |
chmod 664 file1 |
-rw- |
rw- |
r-- |
chmod 644 file1 |
-rw- |
r-- |
r-- |
chmod 775 file1 |
drwx |
rwx |
r-x |
chmod 755 file1 |
drwx |
r-x |
r-x |
For Web pages, follow these guidelines to assure the correct
file access to files located in your public_html.
Directories: allow read, write and
execute permission to the owner, and allow the "world" to read
and execute
chmod 755 directory_name
Files: allow read, write and execute
permission to the owner, and allow the "world" to read and
execute
chmod 644 file_name
By using the -R command you can propogate permissions down a
directory tree. From your
~/ folder you can ensure that
your entire public_html is viewable on the web
chmod -R 755 public_html