2Apr il 2003, 17:00:47 The Complete FreeBSD (filesys.mm), page 181
10
File systems and
devices
In this chapter:
• File permissions
• Mandator y Access
Control
• Links
• Director y hierarchy
• File system types
• Mounting file
systems
• FreeBSD devices
• Vir tual ter minals
In this chapter:
• File permissions
• Mandator y Access
Control
• Links
• Director y hierarchy
• File system types
• Mounting file
systems
• FreeBSD devices
• Vir tual ter minals
One of the most revolutionary concepts of the UNIX operating system was its file system,
the way in which it stores data. Although most other operating systems have copied it
since then, including Microsoft’splatforms, none have come close to the elegance with
which it is implemented. Manyaspects of the file system are not immediately obvious,
some of them not eventoseasoned UNIX users.
required.For example, the file ˜/.rhosts,which is used by some network programs for
user validation, may contain the user’spassword in legible form. To help ensure that
other people don’tread it, the network programs refuse to read it unless its permissions
are rw-------.The vast majority of system problems in UNIX can be traced to
incorrect permissions, so you should pay particular attention to them.
Apart from these access permissions, executables can also have two bits set to specify the
access permissions of the process when it is run. If the setuid (set user ID)bit is set, the
process always runs as if it had been started by its owner.Ifthe setgid (set group ID)bit
is set, it runs as if it had been started by its group. This is frequently used to start system
programs that need to access resources that the user may not access directly.We’ll see an
example of this with the ps command on page 185. ls represents the setuid bit by
setting the third letter of the permissions string to s instead of x;similarly,itrepresents
the setgid bit by setting the sixth letter of the permissions string to s instead of x.
In addition to this access information, the permissions contain a character that describes
what kind of file it represents. The first letter may be a - (hyphen), which designates a
regular file, the letter d for directory,orthe letters b or c for a device node. We’lllook at
device nodes in Chapter 11, page 195. There are also a number of other letters that are
less used. See the man page ls(1) for a full list.
To list files and showthe permissions, use the ls command with the -l option:
$ ls -l
total 2429
-rw-rw-r-- 1 grog wheel 28204 Jan 414:17 %backup%˜
drwxrwxr-x 3 grog wheel 512 Oct 11 15:26 2.1.0-951005-SNAP
drwx------ 4 grog wheel 512 Nov 25 17:23 Mail
-rw-rw-r-- 1 grog wheel 149 Dec 414:18 Makefile
-rw-rw-r-- 1 grog wheel 108 Dec 412:36 Makefile.bak
-rw-rw-r-- 1 grog wheel 108 Dec 412:36 Makefile˜
-rw-rw-r-- 1 grog wheel 0Dec 4 12:36 depend
-rw-rw-r-- 1 daemon wheel 1474560 Dec 14 17:03 deppert.floppy
-rwxr-xr-x 1 grog wheel 100 Dec 19 15:24 doio
programs to do this:
• To change the file owner,use chown.For example, to change the ownership of the
file deppert.floppy,which in the list above belongs to daemon, root would enter:
# chown grog deppert.floppy
Note that only root may perform this operation.
• To change the file group, use chgrp,which works in the same way as chown.To
change the group ownership to lemis,you would enter:
# chgrp lemis deppert.floppy
chown can also change both the owner and the group. Instead of the twoprevious
examples, you could enter:
filesys.mm,v v4.17 (2003/04/02 06:43:57)
File permissions 184
2April 2003, 17:00:47 The Complete FreeBSD (../tools/tmac.Mn), page 184
# chown grog:lemis deppert.floppy
This changes the owner to grog,asbefore, and also changes the group to lemis.
• To change the permissions, use the chmod program. chmod has a number of
different formats, but unfortunately the nine-character representation isn’tone of
them. Read the man page chmod(1) for the full story,but you can achieve just about
anything you want with one of the formats shown in table 10-1:
Table 10-1: chmod permission codes
Specification Effect
go-w Denywrite permission to group and others
=rw,+X Set the read and write permissions to the usual defaults, but retain
anyexecute permissions that are currently set
+X Makeadirectory or file searchable/executable by everyone if it is
already searchable/executable by anyone
u=rwx,go=rx Makeafile readable/executable by everyone and writable by the
owner only
go= Clear all mode bits for group and others
g=u-w Set the group bits equal to the user bits, but clear the group write bit
It’spreferable to set this in your shell initialization file—see page 135 for further details.
Beware of creating a too restrictive umask. For example, you will get into a lot of trouble
with a umask like 377,which creates files that you can only read, and that nobody else
can access at all. If you disallowthe x (executable) bit, you will not be able to access
directories you create, and you won’tbeable to run programs you compile.
Making a program executable
File permissions enable one problem that occurs so often that it’sworth drawing attention
to it. Manyoperating systems require that an executable program have a special naming
convention, such as COMMAND.COM or FOO.BAT,which in MS-DOS denotes a
specific kind of binary executable and a script file, respectively.InUNIX, executable
programs don’tneed a special suffix, but theymust have the x bit set. Sometimes this bit
gets reset (turned off), for example if you copyitacross the Net with ftp.The result
looks likethis:
$ ps
bash: ps: Permission denied
$ ls -l /bin/ps
-r--r--r-- 1 bin kmem 163840 May 6 06:02 /bin/ps
$ su you need to be super user to set ps permission
Password: passworddoesn’techo
# chmod +x /bin/ps makeitexecutable
# ps now it works
PID TT STAT TIME COMMAND
226 p2 S 0:00.56 su (bash)
239 p2 R+ 0:00.02 ps
146 v1 Is+ 0:00.06 /usr/libexec/getty Pc ttyv1
147 v2 Is+ 0:00.05 /usr/libexec/getty Pc ttyv2
# ˆD exit su
$ ps
ps: /dev/mem: Permission denied hey! it’sstopped working
Huh? It only worked under su,and stopped working when I became a mere mortal
modules to augment the system security policy. MAC isintended as a toolkit for
developing local and vendor security extensions, and it includes a number of sample
policymodules, including Multi-LevelSecurity (MLS) with compartments, and a number
of augmented UNIX security models including a file system firewall. At the time of
writing it is still considered experimental software, so this book doesn’tdiscuss it further.
See the man pages for more details.
Links
In UNIX, files are defined by inodes,structures on disk that you can’taccess directly.
Theycontain the metadata,all the information about the file, such as owner,permissions
and timestamps. What theydon’tcontain are the things you think of as making up a file:
theydon’thav e anydata, and theydon’thav e names. Instead, the inode contains
information about where the data blocks are located on the disk. It doesn’tknow
anything about the name: that’sthe job of the directories.
Adirectory is simply a special kind of file that contains a list of names and inode
numbers: in other words, theyassign a name to an inode, and thus to a file. More than
one name can point to the same inode, so files can have more than one name. This
connection between a name and an inode is called a link,sometimes confusingly hard
link.The inode numbers relate to the file system, so files must be in the same file system
filesys.mm,v v4.17 (2003/04/02 06:43:57)