3.3. Protecting and Sharing Files
Unix makes it easy for users to share files and directories.
For instance, everyone in a group can read documents stored in one of
their manager's directories without needing to make their own
copies--if the manager has allowed access.
There might be no need to fill peoples' email inboxes with file
attachments if everyone can access those files directly through
the Unix filesystem.
Here's a brief introduction to file security and sharing.
Networked systems with multiple users, such as Unix, have complex
security issues that take tens or hundreds of pages to explain.
If you have critical security needs or you just want more information,
talk to your system staff or see an up-to-date book on Unix security.
WARNING:
Note that the system's superuser (the system administrator and possibly
other users) can do anything to any file at any time, no matter what
its permissions are.
So, access permissions won't keep your private information safe from
everyone--although let's hope that you can
trust your system staff!
Your system staff should also keep backup copies of users' files.
These backup copies may be readable by anyone who has physical access to
them.
That is, anyone who can take the backup out of a cabinet (or
wherever) and mount it on a computer system may be able to read the file
copies.
The same is true for files stored on floppy disks and any other
removable media.
(Once you take a file off of a Unix system, that system can't control
access to it anymore.)
3.3.3. Setting Permissions with chmod
Once you know what permissions a file or directory needs--and if you're
the owner (listed in the third column of
ls -l output)--you can change the
permissions with the chmod program.
There are two ways to change permissions: by specifying the permissions to
add or delete, or by specifying the exact permissions.[8]
For instance, if a directory's permissions are almost correct,
but you also need to make it writable by its group,
tell chmod to add group-write permission.
But if you need to make more than one change to the permissions--for
instance, you want to add read and execute permission, but delete write
permission--it's easier to set all permissions explicitly instead
of changing them one-by-one.
The syntax is:
chmod permissions file(s)
Let's start with the rules; we see examples next.
The permissions argument has three parts, which you
must give in order with no space between.
The category of permission you want to change.
There are three: the owner's permission
(which chmod calls "user,"
abbreviated u),
the group's permission (g), or
others' permission (o).
To change more than one category, string the letters together, such as
go for "group and others," or
simply use a to mean "all" (same as ugo).
Whether you want to add (+) the permission,
delete (-) it, or specify it exactly (=).
What permissions you want to affect:
read (r), write (w), or
execute (x).
To change more than one permission, string the letters together--for example,
rw for "read and write."
Some examples should make this clearer!
In the following command lines, you can replace dirname
or filename with the pathname (absolute or
relative) of the directory or file.
An easy way to change permissions on the working directory is by using
its relative pathname, . (dot),
as in "chmod a-w .".
You can combine two permission changes in the same
chmod command by separating them with
a comma (,), as shown in the final example.
-
To protect a file from accidental editing,
delete everyone's write permission with the command
"chmod a-w filename".
On the other hand, if you own an unwritable file that you want to edit,
but you don't want to change other peoples' write permissions, you can
add "user" (owner) write permission with
"chmod u+w filename".
-
To keep yourself from accidentally removing files
(or adding or renaming files) in an important directory of yours,
delete your own write permission with the command
"chmod u-w dirname".
If other users have that permission, too, you could delete
everyone's write permission with
"chmod a-w dirname".
-
If you want you and your group to be able to read and write all the
files in your working directory--but those files have various
permissions now, so adding and deleting the permissions individually
would be a pain--this is a good place to use the =
operator to set the exact permissions you want.
Use the filename wildcard *, which means "everything
in this directory" (explained in
Section 4.2 of Chapter 4)
and type:
"chmod ug=rw *".
If your working directory had any subdirectories, though, that command
would be wrong because it takes away execute permission from the
subdirectories, so the subdirectories couldn't be accessed anymore.
In that case, you could try a more specific wildcard.
Or, instead of a wildcard, you can simply list the filenames you want to
change, separated by spaces, as in
"chmod ug=rw afile bfile cfile".
-
To protect the files in a directory and all its
subdirectories from everyone else on your system, but still keep
the access permissions you have there, you could
use "chmod go-rwx dirname"
in order to delete all "group" and "others" permission to read, write, and execute.
A simpler way is to use the command
"chmod go= dirname"
to set "group" and "others" permission to exactly nothing.
-
You want full access to a directory.
Other people on the system should be able to see what's in the
directory--and read or edit the files if the file permissions allow
it--but not rename, remove, or add files.
To do that, give yourself all permissions,
but give "group" and "others" only read and execute permission.
Use the command
"chmod u=rwx,go=rx dirname".
After you change permissions, it's a good idea to check your work at first
with "ls -l filename"
or "ls -ld dirname".
3.3.4. More Protection Under Linux
Most Linux systems have a program named chattr
that gives you more choices on file and directory protection.
chattr is being developed, and your
version may not have all the features that it will have in later
Linux versions.
For instance, chattr can make a
Linux file append-only (so it can't be overwritten,
only added to), compressed (to save disk space
automatically), immutable (so it can't be changed
at all), undeletable, and more.
Check your online documentation (type
man chattr--see
Chapter 8).
3.3.4.1. Problem checklist
- I get the message "chmod: Not owner."
-
Only the owner of a file or directory--or the superuser--can set
its permissions.
Use ls -l to find the owner, or ask a
system staff person to change the permissions.
- A file is writable, but my program says it can't be written.
-
First, check the file permissions with ls -l
and be sure you're in the category (user, group, or others) that
has write permission.
The problem may also be in the permissions of the
file's directory.
Some programs need permission to write more files into the same directory
(for example, temporary files), or to rename
files (for instance, making a file into a backup) while editing.
If it's safe to add write permission to the directory (if other files
in the directory don't need protection from removal or
renaming) try that.
Otherwise, copy the file to a writable directory
(with cp), edit it there, then
copy it back to the original directory.
 |  |  | 3.2. Looking Inside Files with less |  | 3.4. Graphical Filesystem Browsers |
Copyright © 2003 O'Reilly & Associates. All rights reserved.
|
|