3.3. Protecting and Sharing FilesUnix 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! 3.3.1. Directory Access PermissionsA directory's access permissions help to control access to the files and subdirectories in that directory:
3.3.2. File Access PermissionsThe access permissions on a file control what can be done to the file's contents. The access permissions on the directory where the file is kept control whether the file can be renamed or removed. (If this seems confusing, think of it this way: the directory is actually a list of files. Adding, renaming, or removing a file changes the contents of the directory. If the directory isn't writable, you can't change that list.) Read permission controls whether you can read a file's contents. Write permission lets you change a file's contents. A file shouldn't have execute permission unless it's a program. 3.3.3. Setting Permissions with chmodOnce 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.
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.
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 LinuxMost 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
3.3.5. Changing Group and OwnerGroup ownership lets a certain group of users have access to a file or directory. You might need to let a different group have access. The chgrp program sets the group owner of a file or directory. You can set the group to any of the groups you belong to. (The system staff control the list of groups you're in.) On most versions of Unix, the groups program lists your groups. For example, if you're an instructor creating a directory named csc303 for students in a course, the directory's original group owner might be faculty. You'd like the students, all of whom are in the group named csstudnt, to access the directory; members of other groups should have no access. Use commands such as these:[9]
$ groups faculty csstudnt wheel research $ mkdir csc303 $ ls -ld csc303 drwxr-xr-x 2 roberts faculty 4096 Aug 25 13:35 csc303 $ chgrp csstudnt csc303 $ chmod o= csc303 $ ls -ld csc303 drwxr-x--- 2 roberts csstudnt 4096 Aug 25 13:35 csc303 The chown program changes the owner of a file or directory. On most Unix systems, only the superuser can use chown.[10]
Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|