Using chmod to Set File Permissions |
 |
You can specify permissions for chmod using the letters u,
g, and o, as symbolic code for the owner ("user"), group,
and others (the class).
This "symbolic mode" is easy to remember, since the symbols r,
w, and x (the mode) are used directly as arguments in the
command.
The chmod syntax uses the +, -, and = signs.
The syntax is:
chmod class[±=]mode,[ ... ] filename
|
For example, you can use the symbolic mode to create
rw-r--r-- permissions by specifying the symbols rw, r, and
r directly in the chmod command. "User" is represented by
u, "group" by g, and "other" by o.
To assign the permissions absolutely, use the = sign in the
argument. Unspaced commas separate class permissions:
chmod u=rw,g=r,o=r myfile
|
When permissions are being set the same, you can also combine the
arguments as:
With only read permission on myfile, no one can write to it.
Also, if you now try to remove myfile, the rm command asks you
whether you really want to remove the file:
rm myfile
myfile: 444 mode? (y/n) n If you do not want to remove it, enter n.
If you do want to remove it, enter y.
|
To create rw------- permissions and set
"no permissions" for the classes g and o, use = with no
symbol following:
chmod u=rw,g=,o= filename
|
Permissions are added with the + sign. Again, separate each
class-permission with a comma and no space:
chmod u+rw,g+r,o+r filename
|
You can also subtract permissions from u, g, or o, using
-, to restrict the level of permission from a
previous "higher" level. For example, if you had set rwxrw-rw- and
you wanted to change this to rwx------:
However, unless you began with no permissions you may find that
using + or - has added to, or subtracted from, some previously
existing permissions for that file. Run the ll command to check
this. If in doubt, set the permissions absolutely by using =.
Later, if you want to permit yourself and members of your group
to read from and write to myfile, use chmod as follows:
The ll command now should show:
-rw-rw-r-- 1 leslie users 154 Nov 4 10:18 myfile
|
Here is a summary of the various chmod commands you can use to
protect myfile.
- To Set Permissions so that ...
Type This ...
- Only you can read from myfile, and no one (including you)
can write to it. Set permissions to -r--------.
chmod u=r,g=,o= myfile
- Everyone can read from myfile, but no one
can write to it. Set permissions to -r--r--r--.
chmod ugo=r myfile
- Only you can write to myfile, but everyone
can read it. Set permissions to -rw-r--r--.
chmod u=rw,go=r myfile
- Only you and members of your group can write to
myfile, but everyone can read it. Set permissions to -rw-rw-r--
chmod ug=rw,o=r myfile
- Everyone can read from or write to myfile. Set
permissions to -rw-rw-rw-.
chmod ugo=rw myfile
- Only you can read from or write to myfile, but no one else
can. Set permissions to -rw-------
chmod u=rw,go= myfile