Mount a filesystem, usually on an external device,
such as a floppy or CDROM. The file
/etc/fstab provides a handy listing
of available filesystems, partitions, and devices,
including options, that may be automatically or manually
mounted. The file /etc/mtab shows
the currently mounted filesystems and partitions
(including the virtual ones, such as /proc).
mount -a mounts all filesystems and
partitions listed in /etc/fstab,
except those with a noauto
option. At bootup, a startup script in
(rc.sysinit or something similar)
invokes this to get everything mounted.
mount -t iso9660 /dev/cdrom /mnt/cdrom
# Mounts CD ROM. ISO 9660 is a standard CD ROM filesystem.
# Shortcut, if /mnt/cdrom listed in /etc/fstab
The versatile mount command can even
mount an ordinary file on a block device, and the file will
act as if it were a filesystem. Mount
accomplishes that by associating the file with a loopback device. One application of
this is to mount and examine an ISO9660 filesystem image before
burning it onto a CDR.
Example 17-7. Checking a CD image
# As root...
mkdir /mnt/cdtest # Prepare a mount point, if not already there.
mount -r -t iso9660 -o loop cd-image.iso /mnt/cdtest # Mount the image.
# "-o loop" option equivalent to "losetup /dev/loop0"
cd /mnt/cdtest # Now, check the image.
ls -alR # List the files in the directory tree there.
# And so forth.
Unmount a currently mounted filesystem. Before physically removing a
previously mounted floppy or CDROM disk, the device must be
umounted, else filesystem corruption may result.
# You may now press the eject button and safely remove the disk.
The automount utility, if
properly installed, can mount and unmount floppies or
CDROM disks as they are accessed or removed. On
"multispindle" laptops with swappable
floppy and optical drives, this can cause problems,
The newer Linux distros have deprecated
mount and umount.
The successor, for command-line mounting of removable storage
devices, is gnome-mount. It can take the
-d option to mount a device file by its listing in
For example, to mount a USB flash drive:
bash$ gnome-mount -d /dev/sda1
. . .
/dev/sda1 63584 12034 51550 19% /media/disk
Forces an immediate write of all updated data from
buffers to hard drive (synchronize drive
with buffers). While not strictly necessary, a
sync assures the sys admin or
user that the data just changed will survive a sudden
power failure. In the olden days, a sync;
sync (twice, just to make absolutely sure) was a
useful precautionary measure before a system reboot.
At times, you may wish to force an immediate buffer
flush, as when securely deleting a file (see Example 16-61) or when the lights begin to
Sets up and configures loopback devices.
Example 17-8. Creating a filesystem in a file
SIZE=1000000 # 1 meg
head -c $SIZE < /dev/zero > file # Set up file of designated size.
losetup /dev/loop0 file # Set it up as loopback device.
mke2fs /dev/loop0 # Create filesystem.
mount -o loop /dev/loop0 /mnt # Mount it.
# Thanks, S.C.
Creates a swap partition or file. The swap area must
subsequently be enabled with
- swapon, swapoff
Enable / disable swap partitition or file.
These commands usually take effect at bootup and
Create a Linux ext2
filesystem. This command must be invoked as
Example 17-9. Adding a new hard drive
# Adding a second hard drive to system.
# Software configuration. Assumes hardware already mounted.
# From an article by the author of the ABS Guide.
# In issue #38 of _Linux Gazette_, http://www.linuxgazette.com.
ROOT_UID=0 # This script must be run as root.
E_NOTROOT=67 # Non-root exit error.
if [ "$UID" -ne "$ROOT_UID" ]
echo "Must be root to run this script."
# Use with extreme caution!
# If something goes wrong, you may wipe out your current filesystem.
NEWDISK=/dev/hdb # Assumes /dev/hdb vacant. Check!
MOUNTPOINT=/mnt/newdisk # Or choose another mount point.
mke2fs -cv $NEWDISK1 # Check for bad blocks (verbose output).
# Note: ^ /dev/hdb1, *not* /dev/hdb!
chmod 777 $MOUNTPOINT # Makes new drive accessible to all users.
# Now, test ...
# mount -t ext2 /dev/hdb1 /mnt/newdisk
# Try creating a directory.
# If it works, umount it, and proceed.
# Final step:
# Add the following line to /etc/fstab.
# /dev/hdb1 /mnt/newdisk ext2 defaults 1 1
See also Example 17-8 and Example 31-3.
Create a DOS FAT
Tune ext2 filesystem. May be
used to change filesystem parameters, such as maximum
mount count. This must be invoked as
This is an extremely dangerous command. Use it at
your own risk, as you may inadvertently destroy your filesystem.
Dump (list to stdout) very verbose
filesystem info. This must be invoked as
root# dumpe2fs /dev/hda7 | grep 'ount count'
dumpe2fs 1.19, 13-Jul-2000 for EXT2 FS 0.5b, 95/08/09
Mount count: 6
Maximum mount count: 20
List or change hard disk parameters. This command must be
invoked as root, and it may be
dangerous if misused.
Create or change a partition table on a storage device,
usually a hard drive. This command must be invoked as
Use this command with extreme caution. If something
goes wrong, you may destroy an existing
- fsck, e2fsck, debugfs
Filesystem check, repair, and debug command set.
fsck: a front end for checking a UNIX
filesystem (may invoke other utilities). The actual
filesystem type generally defaults to
e2fsck: ext2 filesystem checker.
debugfs: ext2 filesystem debugger.
One of the uses of this versatile, but dangerous command
is to (attempt to) recover deleted files. For advanced users
All of these should be invoked as
root, and they can damage or destroy
a filesystem if misused.
Checks for bad blocks (physical media flaws) on a
storage device. This command finds use when formatting
a newly installed hard drive or testing the integrity
of backup media.
As an example, badblocks /dev/fd0
tests a floppy disk.
The badblocks command
may be invoked destructively (overwrite all data) or
in non-destructive read-only mode. If root
user owns the device to be tested, as is
generally the case, then root
must invoke this command.
- lsusb, usbmodules
The lsusb command lists all USB
(Universal Serial Bus) buses and the devices hooked up to
The usbmodules command outputs
information about the driver modules for connected USB
Bus 001 Device 001: ID 0000:0000
bDeviceClass 9 Hub
. . .
Lists pci busses present.
00:00.0 Host bridge: Intel Corporation 82845 845
(Brookdale) Chipset Host Bridge (rev 04)
00:01.0 PCI bridge: Intel Corporation 82845 845
(Brookdale) Chipset AGP Bridge (rev 04)
00:1d.0 USB Controller: Intel Corporation 82801CA/CAM USB (Hub #1) (rev 02)
00:1d.1 USB Controller: Intel Corporation 82801CA/CAM USB (Hub #2) (rev 02)
00:1d.2 USB Controller: Intel Corporation 82801CA/CAM USB (Hub #3) (rev 02)
00:1e.0 PCI bridge: Intel Corporation 82801 Mobile PCI Bridge (rev 42)
. . .
Creates a boot floppy which can be used to bring up the
system if, for example, the MBR (master boot record) becomes
corrupted. Of special interest is the --iso
option, which uses mkisofs to create a
bootable ISO9660 filesystem image
suitable for burning a bootable CDR.
The mkbootdisk command is actually
a Bash script, written by Erik Troan, in the /sbin directory.
Creates an ISO9660 filesystem
suitable for a CDR image.
CHange ROOT directory. Normally commands are fetched
from $PATH, relative to
/, the default
directory. This changes the
root directory to a different one
(and also changes the working directory to there). This is
useful for security purposes, for instance when the system
administrator wishes to restrict certain users, such as
those telnetting in,
to a secured portion of the filesystem (this is sometimes
referred to as confining a guest user to a "chroot
jail"). Note that after a chroot,
the execution path for system binaries is no longer
A chroot /opt would cause
references to /usr/bin
to be translated to /opt/usr/bin. Likewise,
chroot /aaa/bbb /bin/ls would
redirect future instances of ls
to /aaa/bbb as the base directory,
rather than / as is
normally the case. An alias XX 'chroot /aaa/bbb
ls' in a user's ~/.bashrc
effectively restricts which portion of the filesystem
she may run command "XX" on.
The chroot command is also handy
when running from an emergency boot floppy
(chroot to /dev/fd0),
or as an option to lilo when recovering
from a system crash. Other uses include installation from a
different filesystem (an rpm
option) or running a readonly filesystem from a CD ROM.
Invoke only as root, and use with
It might be necessary to copy certain system
files to a chrooted directory,
since the normal $PATH can no longer
be relied upon.
This utility is part of the procmail
It creates a lock file, a
semaphore that controls access to
a file, device, or resource.
The lock file serves as a flag that this particular
file, device, or resource is in use by a process (and
is therefore "busy"). The presence of a
lock file permits only restricted access (or no access)
to other processes.
# Creates a write-protected lockfile prefixed with the name of the script.
# A safer version of the above, as pointed out by E. Choroba.
Lock files are used in such applications as protecting
system mail folders from simultaneously being changed
by multiple users, indicating that a modem port
is being accessed, and showing that an instance of
Firefox is using its cache.
Scripts may check for the existence of a lock file created
by a certain process to check if that process is running.
Note that if a script attempts to create a lock file that
already exists, the script will likely hang.
Normally, applications create and check for lock files
in the /var/lock
A script can test for the presence of a lock file by
something like the following.
# Application "xyzip" created lock file "/var/lock/xyzip.lock".
if [ -e "/var/lock/$appname.lock" ]
then #+ Prevent other programs & scripts
# from accessing files/resources used by xyzip.
Much less useful than the lockfile
command is flock. It sets an
"advisory" lock on a file and then executes
a command while the lock is on. This is to prevent
any other process from setting a lock on that file until
completion of the specified command.
flock $0 cat $0 > lockfile__$0
# Set a lock on the script the above line appears in,
#+ while listing the script to stdout.
flock does not
automatically create a lock file.
Creates block or character device files (may be
necessary when installing new hardware on the system). The
MAKEDEV utility has virtually
all of the functionality of mknod,
and is easier to use.
Utility for creating device files. It must be run as
root, and in the /dev directory. It is a sort
of advanced version of mknod.
Automatically deletes files which have not been accessed
within a specified period of time. Usually invoked by
cron to remove stale log