Chapter 7. Mounting
Chapter 7. Mounting
Chapter 7. Mounting
mounting
Table of Contents
7.1. mounting local file systems ........................................................................... 58
7.2. displaying mounted file systems .................................................................... 59
7.3. permanent mounts .......................................................................................... 60
7.4. securing mounts ............................................................................................. 61
7.5. practice: mounting file systems ..................................................................... 63
7.6. solution: mounting file systems ..................................................................... 64
Once you've put a file system on a partition, you can mount it. Mounting a file system
makes it available for use, usually as a directory. We say mounting a file system
instead of mounting a partition because we will see later that we can also mount file
systems that do not exists on partitions.
57
mounting
/bin/mkdir
This example shows how to create a new mount point with mkdir.
/bin/mount
When the mount point is created, and a file system is present on the partition, then
mount can mount the file system on the mount point directory.
/etc/filesystems
Actually the explicit -t ext2 option to set the file system is not always necessary. The
mount command is able to automatically detect a lot of file systems.
When mounting a file system without specifying explicitly the file system, then
mount will first probe /etc/filesystems. Mount will skip lines with the nodev
directive.
/proc/filesystems
When /etc/filesystems does not exist, or ends with a single * on the last line, then
mount will read /proc/filesystems.
58
mounting
/bin/umount
You can unmount a mounted file system using the umount command.
root@pasha:~# umount /home/reet
/bin/mount
The simplest and most common way to view all mounts is by issuing the mount
command without any arguments.
/proc/mounts
The kernel provides the info in /proc/mounts in file form, but /proc/mounts does not
exist as a file on any hard disk. Looking at /proc/mounts is looking at information
that comes directly from the kernel.
/etc/mtab
The /etc/mtab file is not updated by the kernel, but is maintained by the mount
command. Do not edit /etc/mtab manually.
59
mounting
/bin/df
A more user friendly way to look at mounted file systems is df. The df (diskfree)
command has the added benefit of showing you the free space on each mounted disk.
Like a lot of Linux commands, df supports the -h switch to make the output more
human readable.
root@RHELv4u2:~# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
11707972 6366996 4746240 58% /
/dev/sda1 101086 9300 86567 10% /boot
none 127988 0 127988 0% /dev/shm
/dev/sdb1 108865 1550 101694 2% /home/project55
root@RHELv4u2:~# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
12G 6.1G 4.6G 58% /
/dev/sda1 99M 9.1M 85M 10% /boot
none 125M 0 125M 0% /dev/shm
/dev/sdb1 107M 1.6M 100M 2% /home/project55
In the df -h example below you can see the size, free space, used gigabytes and
percentage and mount point of a partition.
/bin/du
The du command can summarize disk usage for files and directories. Preventing du
to go into subdirectories with the -s option will give you a total for that directory.
This option is often used together with -h, so du -sh on a mount point gives the total
amount used in that partition.
root@pasha:~# du -sh /home/reet
881G /home/reet
/etc/fstab
This is done using the file system table located in the /etc/fstab file. Below is a sample
/etc/fstab file.
60
mounting
By adding the following line, we can automate the mounting of a file system.
mount /mountpoint
Adding an entry to /etc/fstab has the added advantage that you can simplify the
mount command. The command in the screenshot below forces mount to look for
the partition info in /etc/fstab.
# mount /home/project55
ro
The ro option will mount a file system as read only, preventing anyone from writing.
root@rhel53 ~# mount -t ext2 -o ro /dev/hdb1 /home/project42
root@rhel53 ~# touch /home/project42/testwrite
touch: cannot touch `/home/project42/testwrite': Read-only file system
noexec
The noexec option will prevent the execution of binaries and scripts on the mounted
file system.
root@rhel53 ~# mount -t ext2 -o noexec /dev/hdb1 /home/project42
root@rhel53 ~# cp /bin/cat /home/project42
root@rhel53 ~# /home/project42/cat /etc/hosts
-bash: /home/project42/cat: Permission denied
root@rhel53 ~# echo echo hello > /home/project42/helloscript
root@rhel53 ~# chmod +x /home/project42/helloscript
root@rhel53 ~# /home/project42/helloscript
-bash: /home/project42/helloscript: Permission denied
61
mounting
nosuid
The nosuid option will ignore setuid bit set binaries on the mounted file system.
Note that you can still set the setuid bit on files.
root@rhel53 ~# mount -o nosuid /dev/hdb1 /home/project42
root@rhel53 ~# cp /bin/sleep /home/project42/
root@rhel53 ~# chmod 4555 /home/project42/sleep
root@rhel53 ~# ls -l /home/project42/sleep
-r-sr-xr-x 1 root root 19564 Jun 24 17:57 /home/project42/sleep
root@rhel53 ~# su - paul
[paul@rhel53 ~]$ /home/project42/sleep 500 &
[1] 2876
[paul@rhel53 ~]$ ps -f 2876
UID PID PPID C STIME TTY STAT TIME CMD
paul 2876 2853 0 17:58 pts/0 S 0:00 /home/project42/sleep 500
[paul@rhel53 ~]$
noacl
To prevent cluttering permissions with acl's, use the noacl option.
root@rhel53 ~# mount -o noacl /dev/hdb1 /home/project42
62
mounting
2. Mount the big 400MB primary partition on /mnt, the copy some files to it
(everything in /etc). Then umount, and mount the file system as read only on /srv/
nfs/salesnumbers. Where are the files you copied ?
3. Verify your work with fdisk, df and mount. Also look in /etc/mtab and /proc/
mounts.
5. What happens when you mount a file system on a directory that contains some
files ?
6. What happens when you mount two file systems on the same mount point ?
7. (optional) Describe the difference between these file searching commands: find,
locate, updatedb, whereis, apropos and which.
63