CompTIA Linux+ Certification Guide
上QQ阅读APP看书,第一时间看更新

The mount command

We can issue the mount command without any arguments to view the current mount points:

root@ubuntu:/home/philip# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
udev on /dev type devtmpfs (rw,nosuid,relatime,size=478356k,nr_inodes=119589,mode=755)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,noexec,relatime,size=99764k,mode=755)
/dev/sda1 on / type ext4 (rw,relatime,errors=remount-ro,data=ordered)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
tmpfs on /run/lock type tmpfs (rw,nosuid,nodev,noexec,relatime,size=5120k)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpu,cpuacct)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup(rw,relatime,user_id=0,group_id=0,default_permissions,allow_other)
tmpfs on /run/user/1000 type tmpfs (rw,nosuid,nodev,relatime,size=99764k,mode=700,uid=1000,gid=1000)
gvfsd-fuse on /run/user/1000/gvfs type fuse.gvfsd-fuse (rw,nosuid,nodev,relatime,user_id=1000,group_id=1000)
root@ubuntu:/home/philip#

Some of the output was omitted for brevity purposes.

From the preceding output, we can see a number of mount points (a mount point is simply associating a partition/drive to a folder/directory). We can filter the mount command to only display the /dev/:

root@ubuntu:/home/philip# mount | grep /dev
udev on /dev type devtmpfs (rw,nosuid,relatime,size=478356k,nr_inodes=119589,mode=755)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
/dev/sda1 on / type ext4 (rw,relatime,errors=remount-ro,data=ordered)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
mqueue on /dev/mqueue type mqueue (rw,relatime)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime)
root@ubuntu:/home/philip#

Based on the filter, we can see the /dev/sda1 is currently mounted on the / directory. As you know, the / directory is the root directory. All other directories fall under the / directory.

We can also use the df command with the -h option to view a more concise output:

root@ubuntu:/home/philip# df -h
Filesystem Size Used Avail Use% Mounted on
udev 468M 0 468M 0% /dev
tmpfs 98M 6.2M 92M 7% /run
/dev/sda1 19G 5.1G 13G 29% /
tmpfs 488M 212K 487M 1% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 488M 0 488M 0% /sys/fs/cgroup
tmpfs 98M 44K 98M 1% /run/user/1000
root@ubuntu:/home/philip#

Great! Now this is presented in a structured format and is easier to read. Based on the output, only the /dev/sda1 partition is currently mounted.

We can now go ahead and mount the /dev/sdb1 on the /mnt. The /mnt is an empty directory which we use whenever we want to mount a partition.

Only one partition can be mounted at any given time.

We will run the mount command as follows:

root@ubuntu:/# mount /dev/sdb1 /mnt
root@ubuntu:/#

Note that without any options, the mount command worked without an error. Now let's rerun the mount command and filter for only the /dev:

root@ubuntu:/# mount | grep /dev
udev on /dev type devtmpfs (rw,nosuid,relatime,size=478356k,nr_inodes=119589,mode=755)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
/dev/sda1 on / type ext4 (rw,relatime,errors=remount-ro,data=ordered)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
mqueue on /dev/mqueue type mqueue (rw,relatime)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime)
/dev/sdb1 on /mnt type ext4 (rw,relatime,data=ordered)
root@ubuntu:/#

Based on the preceding output, we can see that the /dev/sdb1 is currently mounted on the /mnt.

We can also leverage the df command with the h option to see similar results:

root@ubuntu:/# df -h
Filesystem Size Used Avail Use% Mounted on
udev 468M 0 468M 0% /dev
tmpfs 98M 6.2M 92M 7% /run
/dev/sda1 19G 5.1G 13G 29% /
tmpfs 488M 212K 487M 1% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 488M 0 488M 0% /sys/fs/cgroup
tmpfs 98M 44K 98M 1% /run/user/1000
/dev/sdb1 4.8G 10M 4.6G 1% /mnt
root@ubuntu:/#

From the preceding output, we can see the size of the partition along with the mount point associated with the partition.

Let's now create two directories to be used for the /dev/sdb2 and the /dev/sdb4 partitions:

root@ubuntu:/# mkdir /folder1
root@ubuntu:/# mkdir /folder2
root@ubuntu:/# ls
bin dev folder2 initrd.img.old lost+found opt run srv usr vmlinuz.old
boot etc home lib media proc sbin sys var
cdrom folder1 initrd.img lib64 mnt root snap tmp vmlinuz
root@ubuntu:/#

Now we'll mount the /dev/sdb2 and the /dev/sdb4 on the /folder1 and /folder2 directories, respectively:

root@ubuntu:/# mount /dev/sdb2 /folder1
root@ubuntu:/# mount /dev/sdb4 /folder2
root@ubuntu:/#
root@ubuntu:/# mount | grep /dev
/dev/sda1 on / type ext4 (rw,relatime,errors=remount-ro,data=ordered)
/dev/sdb1 on /mnt type ext4 (rw,relatime,data=ordered)
/dev/sdb2 on /folder1 type ext3 (rw,relatime,data=ordered)
/dev/sdb4 on /folder2 type fuseblk (rw,relatime,user_id=0,group_id=0,allow_other,blksize=4096)
root@ubuntu:/#

Great! Now we can see our mount points being displayed with the mount command. Likewise, we can use the df command with the -h option for a readable format:

root@ubuntu:/# df -h
Filesystem Size Used Avail Use% Mounted on
udev 468M 0 468M 0% /dev
tmpfs 98M 6.2M 92M 7% /run
/dev/sda1 19G 5.1G 13G 29% /
tmpfs 488M 212K 487M 1% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 488M 0 488M 0% /sys/fs/cgroup
tmpfs 98M 44K 98M 1% /run/user/1000
/dev/sdb1 4.8G 10M 4.6G 1% /mnt
/dev/sdb2 2.0G 3.1M 1.9G 1% /folder1
/dev/sdb4 2.0G 11M 2.0G 1% /folder2
root@ubuntu:/#

As you can see, the steps involved in mounting a partition are fairly straightforward. However, on some distributions, you will have to specify the filesystem type. In a network, mounting a share is common. An example of mounting a share would be as follows:

root@ubuntu:/#mount //172.16.175.144/share /netshare -t cifs  -o user=philip,password=pass123,uid=1000,gid=1000,rw