Read LPI Linux Certification in a Nutshell Online
Authors: Adam Haeder; Stephen Addison Schneiter; Bruno Gomes Pessanha; James Stanger
Tags: #Reference:Computers
mount
mount [options
]device
mount [options
]directory
mount [options
]device directory
Used to mount filesystems into the filesystem hierarchy. The
first and second forms consult
/etc/fstab
and
mount the filesystem located ondevice
or
intended to be attached todirectory
,
respectively. In both cases, information necessary to complete the
mount operation is taken from
/etc/fstab
. The
third form is independent of
/etc/fstab
and
mounts the filesystem ondevice
at mount
pointdirectory
.
The
mount
command accepts two kinds of
options:
command-line
and
mount
. The command-line options provide general
direction for the
mount
command. The mount
options are used to specify additional information about the device
being mounted.
Mounts all of the partitions specified in
/etc/fstab
, except those with thenoauto
option.
Displays help on the
mount
command.
mount_options
Specifies mount options on the command line.
Mounts the filesystem as read-only.
fstype
Specifies that the filesystem to be mounted is of typefstype
. This option is typically
used interactively when no entry for the mount exists in
/etc/fstab
.
Sets verbose mode.
Mounts the filesystem in read/write mode.
A number of parameters are available as options for mounting
filesystems. These options may be specified in
/etc/fstab
or as arguments of the
-o
command-line
mount
argument. These options modify the way
mount
configures the mounted filesystem. Some of the options can provide
added security by controlling some operations on the filesystem.
Others protect the filesystem from damage. Here is a partial
list:
Establishes asynchronous I/O to the mounted filesystem.
The opposite is
sync
.
Enables a mount specification in
/etc/fstab
to be processed with the
-a
command-line option, as needed at boot
time. The opposite is
noauto
.
Implies
rw
,
suid
,
dev
,
exec
,
auto
,
nouser
, and
async
.
It is commonly found on
/etc/fstab
entries for
ext2
and
ext3
mount points.
Interprets character or block special devices on the
filesystem.
Enables the execution of programs contained on the
mounted partition. The opposite is
noexec
.
Prohibits automatic mounting with the
-a
option. This is usually specified for
removable media.
Prohibits the execution of executable programs, a
potential security measure.
Disables the effect of suid or sgid bits on executable
files.
Forbids nonroot users from mounting and unmounting the
filesystem. See
user
and
users
for the opposite effect.
Equivalent to specifying the command-line option
-r
.
Equivalent to specifying the command-line option
-w
.
Enables the effect of suid and sgid bits on executable
files.
Establishes synchronous I/O to the mounted filesystem.
The opposite is
async
.
Allows an ordinary user to mount the filesystem but
prohibits other ordinary users from unmounting it. This is
useful for removable media that an individual requires control
over. See also
users
.
Allows any user to mount and unmount the
filesystem.
Note that the
user
and
users
options make the
mount
and
umount
commands
available to nonroot users. This may be important for some systems
where end users must have the ability to mount removable
media.
The prevalence of removable media such as USB flash drives has
caused the majority of Linux distributions to be configured by
default to mount these devices automatically when they are plugged
into a USB port. These devices are usually mounted under the
directory
/media
and are given a directory name
that matches the label name of the partition. For example, if the
partition on my USB flash drive is labeled “USBDISK”, it will be
automatically mounted under the directory
/media/USBDISK
when I plug it into a
USB slot. Although the device is mounted
automatically, there is no way to
unmount
it
automatically, and you must be careful of potential data loss if you
remove a device like this before Linux is done writing data to it.
The
umount
command (described later
in this chapter) is required in order to detach this device from the
filesystem, flush all pending disk writes, and allow it to be safely
removed. Most Linux distributions also have some sort of GUI tool to
handle the unmounting of removable media.
When mounting a filesystem, the
filesystem
type
should be specified either by using the
-t
option to
mount
or in
the third field in
/etc/fstab
. (If
-t
is omitted or
auto
is
specified, the kernel will attempt to probe for the filesystem type.
This can be convenient for removable media, where the filesystem
type may not always be the same or even known.) Linux can mount a
variety of filesystems. The following are some of the more popular
ones:
The standard Linux filesystem.
A journaling filesystem that is backward-compatible with
ext2
.
The MS-DOS FAT filesystem, limited to “8.3” filenames
(eight characters, a dot, and a three-character
extension).
Virtual FAT, used instead of
msdos
when long filenames must be preserved. For example, you may
wish to have access to Windows partitions on systems
configured to boot both Linux and Windows.
The native MS Windows partition since Windows
2000.
The CD-ROM format.
Remote servers.
Swap partitions.
This type represents the
proc
filesystem, which is not really a filesystem at all. The
virtual files found in this virtual filesystem provide a
window into the kernel. It is usually mounted on
/proc
.
Display filesystems currently mounted on the system:
#mount
/dev/sda1 on / type ext2 (rw)
none on /proc type proc (rw)
/dev/sda5 on /boot type ext2 (rw)
/dev/sda9 on /home type ext2 (rw)
/dev/sda6 on /root type ext2 (rw)
/dev/sda10 on /tmp type ext2 (rw)
/dev/sda8 on /usr type ext2 (rw)
/dev/sda7 on /var type ext2 (rw)
none on /dev/pts type devpts (rw,mode=0622)
/dev/hdd on /mnt/zip type vfat (rw,noexec,nosuid,nodev)
In this example, you can see that most of the filesystems
specified in the
/etc/fstab
from
Example 7-1
are already
mounted.
Mount the IDE CD-ROM device found on
/dev/hdc
to the existing directory
/mnt/cdrom
(read-only, of course):
#mount -rt iso9660 /dev/hdc /mnt/cdrom
Note that without the
-r
option, you will
receive a warning but still get appropriate results:
#mount -t iso9660 /dev/hdc /mnt/cdrom
mount: block device /dev/hdc is write-protected,
mounting read-only
Another option would be to add the following to
/etc/fstab
:
/dev/hdc /mnt/cdrom iso9660 ro 0 0
Then the device can be mounted with justmount /mnt/cdrom
.
Mount an MS-DOS floppy in the first floppy disk drive
/dev/fd0
(A:
in MS-DOS) to the existing directory
/mnt/floppy
:
#mount -t msdos /dev/fd0 /mnt/floppy
The filesystems mounted at
/home
and
/opt
have been unmounted for some kind of
maintenance and are now remounted using the
-a
option:
#mount -av
mount: /dev/hda5 already mounted on /root
mount: /dev/hda9 already mounted on /usr
mount: /dev/hda7 already mounted on /var
mount: none already mounted on /proc
mount: none already mounted on /dev/pts
/dev/hda10 on /home type ext2 (rw)
/dev/hda8 on /opt type ext2 (rw)
Note that
mount
should work silently
without the
-v
option. It also safely skips
filesystems that have been previously mounted.
umount
umount [options
]device
umount [options
]directory
Unmount the filesystem ondevice
or
mounted ondirectory
.
Unmounts all of the filesystems described in
/etc/mtab
. This file is maintained by the
mount
and
umount
commands and contains an up-to-date list of mounted
filesystems. This option is typically used at shutdown
time.
fstype
Unmounts only filesystems of typefstype
.
Unmount the CD-ROM mounted on
/dev/hdc
at
/mnt/cdrom
:
#umount /mnt/cdrom
or:
#umount /dev/hdc
Unmount all NFS filesystems:
#umount -at nfs
On the Exam
Be sure that you understand how to use
mount
and mount points and how
/etc/fstab
is used when
mounting files.
Managing disk space can be a difficult problem. The
available space is a finite resource and is often consumed at an alarming
rate, turning today’s carefully sized filesystem into tomorrow’s expansion
requirement. On multiuser systems—no matter how big the filesystem—users
will find a way to fill it. The last thing you want is for a filesystem to
fill to capacity too early. One way to prevent this from happening is to
enforce
disk quotas
, which allow you to assign a
limit to the amount of space individual users or groups have on a
filesystem.
A typical quota size is usually much smaller than the filesystem it
is configured on, thus preventing the user or group from consuming too
much space. Quotas can be configured for each filesystem mentioned in
/etc/fstab
, though they are usually applied only
where multiple end users store files (e.g.,
/home/username
). There is no need for a quota on
/usr
, for example, since end users cannot store files
there. Quotas may be configured for individual users listed in
/etc/passwd
and for groups listed in
/etc/group
.
Each filesystem has up to five types of quota limits that
can be enforced on it. These limits are specified in disk
blocks
, usually 1,024 bytes each:
The
hard limit
is the maximum amount of
space an individual user can have on the system. Once the user
reaches his quota limit, he won’t be allowed to write files to the
disk.
Each user is free to store data on the filesystem
until reaching her
soft limit
. The soft limit
implements a sort of warning zone, instructing the user to clean
up while still allowing her to work. When the amount of data
exceeds this limit but does not exceed the hard limit, a message
is printed on the user’s terminal, indicating that her quota has
been exceeded; however, the write operation will succeed.
This is the final limit set for a group by the quota
system. Once this limit has been reached, none of the users within
that group will be allowed to write files to the disk—even if the
user’s individual limits are not exceeded.
This limit behaves in the same way as a user’s soft
limit but is enforced based on group ownership instead of
individual ownership.
Once a soft limit is reached, the user or group enters the
grace period
. After the grace
period expires, the soft limit becomes a hard limit until enough
files are deleted to eliminate the over-quota situation. The grace
period may be specified for any number of months, weeks, days,
hours, minutes, or seconds. A typical value is seven days.
These limits are set using the
edquota
command, detailed in the next section.
When a disk write exceeds a hard limit or an expired soft limit,
only part of the write operation will complete, leaving a truncated
and probably useless file. The messages reported to the user when a
quota is exceeded may be lost if the shell he is using is hidden (for
example, if the user is writing to the disk through an SMB share from
a Windows system). This could confuse the user because the error
message generated by the application indicates that the disk is full
or write-protected.
Linux offers a host of commands to manage, display, and report on
filesystem
quotas
. Some of the
setup required to initially enable quotas is done manually and without
specific quota commands, a process that is covered in the next
section.
To use quotas, they must first be enabled. Quota support
must also be compiled into the kernel. In the unlikely event that your
kernel does not contain quota support, you will need to recompile the
kernel. This is not a difficult process, but unfortunately it is not
completely straightforward either. To clarify the procedure, this
section provides a brief tutorial on how to enable user and group quotas
for a filesystem on
/dev/sda9
mounted under
/home
. Note that you may enable user quotas only,
group quotas only, or both, as your needs dictate.
Set options in
/etc/fstab
. On the line
containing the
/home
filesystem, add theusrquota
andgrpquota
options to the existingdefault
option, like this:
/dev/sda9 /home ext2 defaults,usrquota,grpquota 1 2
These options tell quota configuration utilities which
partitions should be managed when the utilities reference
/etc/fstab
.
Create the
quota.user
and
quota.group
files at the top of the
/home
filesystem and set their protection bits
for root access only:
#touch /home/quota.user /home/quota.group
#chmod 600 /home/quota.user /home/quota.group
These two files are the databases for user and group quotas.
Each filesystem with quotas uses its own quota databases. When
quotas are enabled, these files will contain binary data (that is,
they’re not text files). Note that if you want end users to be able
to examine quotas on groups to which they belong,
quota.group
will need a protection mode of 644
instead of 600.
Run
quotacheck
to initialize the
databases:
#quotacheck -avug
Scanning /dev/sda9 [/home] done
Checked 236 directories and 695 files
Using quotafile /home/quota.user
Using quotafile /home/quota.group
Then, verify that your quota database files have been
initialized by noting that they are no longer of size zero (here
they are 16,192 bytes each):
#ls -al /home/quota.*
-rw------- 1 root root 16192 Dec 27 19:53 /home/quota.group
-rw------- 1 root root 16192 Dec 27 19:53 /home/quota.user
Run
quotaon
to enable the quota
system:
#quotaon -a
Verify that your system’s initialization script
(
/etc/rc.d/rc.sysinit
or similar) will turn on
quotas when your system boots. Something along these lines is
appropriate, although your system may be very different:
if [ -x /sbin/quotacheck ]; then
echo "Checking quotas."
/sbin/quotacheck -avug
echo " Done."
fi
if [ -x /sbin/quotaon ]; then
echo "Turning on quotas."
/sbin/quotaon -avug
fi
Add a command script to a system
crontab
directory (such as the directory
/etc/crontab.weekly
) to execute
quotacheck
on a routine basis. An executable
script file like the following will work:
#!/bin/bash
exec /sbin/quotacheck -avug
If you prefer, you could instead put
/sbin/quotacheck
in
root
’s
crontab
file (using the
crontab
-e
command) for weekly execution, like this:
# run quotacheck weekly
0 3 * * 0 /sbin/quotacheck -avug
At this point, the
/home
filesystem is ready
to accept quotas on a per-user and per-group basis, enforce them, and
report on them.
On the Exam
A general understanding of quotas is necessary for the exam. In
particular, you should know the function of each command. Also
remember that quotas are set on a per-filesystem basis.