Setting File Permissions with chmod
- What are permissions?
Permissions are what users are allowed to do with files and directories.
There are three types of permissions:
- r - read
- w - write
- x - execute
These permissions mean different things for files and directories.
For files:
- read - you can open and read the file, you can also copy it.
- write - you can modify the file
- execute - you can execute (run) the file if it is executable (like a program or a command)
For directories:
- read - you can ls the directory and see the contents.
- write - you can make and remove files in that directory.
- execute - you can cd into that directory.
- How can I see the permissions for a file or directory and how do I interpret them?
When you do the command:
allspice$ ls -l
You get a list of files and directories, and other information, including their owner, their group, and their permissions, in this form:
-rwxr-xr-x 1 holman users 512 Jan 20 2002 hw_03
Or, more generally...
-rwxr-xr-x # username group size date-modified filename
To explain this information:
-rwxr-xr-x 1 username users size date-modified filename | | | permissions owner group So, how does this relate to what you know about permissions?
- owner: person who owns the file/directory and determines its
permissions. The creator is automatically the owner. - group: this is a set of users who have special permissions for
the file/directory. The "users" group is the default and includes
all users on the system. - permissions:
- rwx rwx rwx | | | | -: file
d: directory
l: linkowner
permissionsgroup
permissionsall other user
permissionsSo, for this file, everybody had read, write, and execute permission. If it looked like:
drwxr-x-r-x
then the owner would have read, write, and execute permissions, the group would have read and execute permissions, everyone else would also have read and execute permissions. These, by the way, are the default permissions for a directory. The default permissions for a file are -rw-r--r-- (mini-quiz: what does this mean?). Also, you'll notice that since the first letter was a "d", this is a directory.
- owner: person who owns the file/directory and determines its
- How do I change permissions on a file/directory?
Here's where chmod comes in. chmod sets permissions for you.
The chmod command uses numbers:
- 4 - read
- 2 - write
- 1 - execute
The reason these aren't 1,2,3 is because they need to add up to a unique number depending on what combination of them you use.
So, to give read and write permission ...
read + write = 4 + 2 = 6
or execute and read permission ...
execute + read = 1 + 4 =5
or just execute permission ...
execute = 1
or all permissions ...
read + write + execute = 4 + 2 + 1 = 7
The chmod command takes three numbers for three permissions:
owner, group, all users (in that order)
The basic chmod command goes:
allspice$ chmod ### directory/filename
So, if you wanted to give the default file permissions (-rw-r--r--)...
allspice$ chmod 644 filename
To make a file readable, writable, and executable by only you ...
allspice$ chmod 700 filename
To make a file readable and exectuable by you and your group, but only readable by everyone else ...
allspice$ chmod 554 filename
For more information about chmod, see the man page (man chmod).