How to mount/unmount Local and Network Filesystems in Linux

Mount/Unmount Local and Network Filesystems in Linux

A mount point is a directory that is used to access the filesystem on the partition. This will be attained on a one-time basis by using tools like mount or persistently across reboots to edit the file /etc/fstab. In this article we will discuss the steps to Mount/Unmount Local and Network File system in linux.

To view the mounted files

Run the following command to shows the currently mounted file systems.

[root@linuxhelp ~]# mount
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=930624k,nr_inodes=232656,mode=755)
.
.
.
(rw,relatime,seclabel,attr2,inode64,noquota)
fusectl on /sys/fs/fuse/connections type fusectl (rw,relatime)
gvfsd-fuse on /run/user/0/gvfs type fuse.gvfsd-fuse (rw,nosuid,nodev,relatime,user_id=0,group_id=0)
/dev/sr0 on /run/media/root/CentOS 7 x86_64 type iso9660 (ro,nosuid,nodev,relatime,uid=0,gid=0,iocharset=utf8,mode=0400,dmode=0500,uhelper=udisks2)

To Mount/Unmount Local File Systems

Create a partition in /dev/sdb hard drive to mount locally as follows.

[root@linuxhelp ~]# fdisk -l

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b5211

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048     5122047     2048000   82  Linux swap / Solaris
/dev/sda3         5122048    41943039    18410496   83  Linux

[root@linuxhelp ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xfe382f61.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-20971519, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +2G
Partition 1 of type Linux and of size 2 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

[root@linuxhelp ~]# partprobe /dev/sdb
[root@linuxhelp ~]# fdisk -l

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xfe382f61

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux

Now create a file system for the newly created partition.

[root@linuxhelp ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

Mount the local file system by creating a new directory.

[root@linuxhelp ~]# mkdir /disk1
[root@linuxhelp ~]# mount /dev/sdb1 /disk1
[root@linuxhelp ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        18G  5.2G   13G  30% /
devtmpfs        909M     0  909M   0% /dev
tmpfs           918M  140K  917M   1% /dev/shm
tmpfs           918M  8.9M  909M   1% /run
tmpfs           918M     0  918M   0% /sys/fs/cgroup
/dev/sda1       497M  116M  382M  24% /boot
/dev/sr0        3.9G  3.9G     0 100% /run/media/root/CentOS 7 x86_64
/dev/sdb1       2.0G  6.0M  1.8G   1% /disk1

To unmount, run the following command with exact mount point.

[root@linuxhelp ~]# umount /disk1
[root@linuxhelp ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        18G  5.2G   13G  30% /
devtmpfs        909M     0  909M   0% /dev
tmpfs           918M  140K  917M   1% /dev/shm
tmpfs           918M  8.9M  909M   1% /run
tmpfs           918M     0  918M   0% /sys/fs/cgroup
/dev/sda1       497M  116M  382M  24% /boot
/dev/sr0        3.9G  3.9G     0 100% /run/media/root/CentOS 7 x86_64
[section label=" To Mount Network File Systems (NFS, Samba)
" ]

To Mount Network File Systems (NFS, Samba)

Install the required client packages to mount the particular network file system.

For NFS

[root@linuxhelp ~]# yum install nfs-utils -y

For Samba

[root@linuxhelp ~]# yum install cifs-utils samba-client -y

In most of the cases the packages are installed in default on the client side. If it is not installed use the above command to install it.
Create new directories for mount point. Single directory cannot allow multiple mount point.

[root@linuxhelp ~]# mkdir /mountdir
[root@linuxhelp ~]# mkdir /Netmount1
[root@linuxhelp ~]# mkdir /Netmount2

To view available network shares.

[root@linuxhelp ~]# showmount -e 192.168.5.88
Export list for 192.168.5.88:
/netshare 192.168.5.89

To mount a network share temporarily, run the below command.

[root@linuxhelp ~]# mount -t nfs 192.168.5.88:/netshare /mountdir/
[root@linuxhelp ~]# df -h
Filesystem              Size  Used Avail Use% Mounted on
/dev/sda3                18G  5.2G   13G  30% /
devtmpfs                909M     0  909M   0% /dev
tmpfs                   918M  140K  917M   1% /dev/shm
tmpfs                   918M  8.9M  909M   1% /run
tmpfs                   918M     0  918M   0% /sys/fs/cgroup
/dev/sda1               497M  116M  382M  24% /boot
/dev/sr0                3.9G  3.9G     0 100% /run/media/root/CentOS 7 x86_64
/dev/sdb1               2.0G  6.0M  1.8G   1% /disk1
192.168.5.88:/netshare   18G  4.1G   14G  24% /mountdir

Run the following command to unmount.

[root@linuxhelp ~]# umount /mountdir/
[root@linuxhelp ~]# df -h
Filesystem              Size  Used Avail Use% Mounted on
/dev/sda3                18G  5.2G   13G  30% /
devtmpfs                909M     0  909M   0% /dev
tmpfs                   918M  140K  917M   1% /dev/shm
tmpfs                   918M  8.9M  909M   1% /run
tmpfs                   918M     0  918M   0% /sys/fs/cgroup
/dev/sda1               497M  116M  382M  24% /boot
/dev/sr0                3.9G  3.9G     0 100% /run/media/root/CentOS 7 x86_64
/dev/sdb1               2.0G  6.0M  1.8G   1% /disk1

If you want to mount the shares permanently we need to add the entry in /etc/fstab file as follows.

[root@linuxhelp ~]# vim /etc/fstab

Add the following entry into the file.

192.168.5.88:/netshare  /mountdir  nfs  defaults  0 0

Now run the command to mount all the entries in fstab file

[root@linuxhelp ~]# mount -a
[root@linuxhelp ~]# df -h
Filesystem              Size  Used Avail Use% Mounted on
/dev/sda3                18G  5.2G   13G  30% /
devtmpfs                909M     0  909M   0% /dev
tmpfs                   918M  140K  917M   1% /dev/shm
tmpfs                   918M  8.9M  909M   1% /run
tmpfs                   918M     0  918M   0% /sys/fs/cgroup
/dev/sda1               497M  116M  382M  24% /boot
/dev/sr0                3.9G  3.9G     0 100% /run/media/root/CentOS 7 x86_64
/dev/sdb1               2.0G  6.0M  1.8G   1% /disk1
192.168.5.88:/netshare   18G  4.1G   14G  24% /mountdir

To Mount samba share

Run the following command to mount the samba share temporarily.

[root@linuxhelp ~]# mount -t cifs -o username=testuser1 //192.168.5.88/open-share /Netmount1
Password for testuser1@//192.168.5.88/open-share:  *****
[root@linuxhelp ~]# df -h
Filesystem                 Size  Used Avail Use% Mounted on
/dev/sda3                   18G  5.2G   13G  30% /
devtmpfs                   909M     0  909M   0% /dev
tmpfs                      918M  140K  917M   1% /dev/shm
tmpfs                      918M  8.9M  909M   1% /run
tmpfs                      918M     0  918M   0% /sys/fs/cgroup
/dev/sda1                  497M  116M  382M  24% /boot
/dev/sr0                   3.9G  3.9G     0 100% /run/media/root/CentOS 7 x86_64
/dev/sdb1                  2.0G  6.0M  1.8G   1% /disk1
192.168.5.88:/netshare      18G  4.1G   14G  24% /mountdir
//192.168.5.88/open-share   18G  4.1G   14G  24% /Netmount1

To Mount share with user Credentials

Create a new file with credentials of the samba user for the particular share.

[root@linuxhelp ~]# vim usercred

Add the following lines.

Username=testuser2
Password=linux

Then mount the share with specifying the credentials option.

[root@linuxhelp ~]# mount -t cifs -o credentials=/root/usercred //192.168.5.88/protected-share /Netmount2
[root@linuxhelp ~]# df -h
Filesystem                      Size  Used Avail Use% Mounted on
/dev/sda3                        18G  5.2G   13G  30% /
devtmpfs                        909M     0  909M   0% /dev
tmpfs                           918M  140K  917M   1% /dev/shm
tmpfs                           918M  8.9M  909M   1% /run
tmpfs                           918M     0  918M   0% /sys/fs/cgroup
/dev/sda1                       497M  116M  382M  24% /boot
/dev/sr0                        3.9G  3.9G     0 100% /run/media/root/CentOS 7 x86_64
/dev/sdb1                       2.0G  6.0M  1.8G   1% /disk1
192.168.5.88:/netshare           18G  4.1G   14G  24% /mountdir
//192.168.5.88/open-share        18G  4.1G   14G  24% /Netmount1
//192.168.5.88/protected-share   18G  4.1G   14G  24% /Netmount2

To unmount, run the below command.

[root@linuxhelp ~]# umount /Netmount1
[root@linuxhelp ~]# umount /Netmount2

To permanent mount the samba share

[root@linuxhelp ~]# vim /etc/fstab

Add the below lines to mount the samba share.

//192.168.5.88/open-share  /Netmount1      cifs    username=testuser1      0 0
//192.168.5.88/protected-share    /Netmount2    cifs    credentials=/root/usercred    0 0
[root@linuxhelp ~]# mount -a
Password for testuser1@//192.168.5.88/open-share:  *****
[root@linuxhelp ~]# df -h
Filesystem                      Size  Used Avail Use% Mounted on
/dev/sda3                        18G  5.2G   13G  30% /
devtmpfs                        909M     0  909M   0% /dev
tmpfs                           918M  140K  917M   1% /dev/shm
tmpfs                           918M  8.9M  909M   1% /run
tmpfs                           918M   16K  918M   1% /sys/fs/cgroup
/dev/sda1                       497M  116M  382M  24% /boot
192.168.5.88:/netshare           18G  4.1G   14G  24% /mountdir
//192.168.5.88/open-share        18G  4.1G   14G  24% /Netmount1
//192.168.5.88/protected-share   18G  4.1G   14G  24% /Netmount2

You can create the same samba user on your client side to work with the specified share or else we can connect to the network share by running the below command.

[root@linuxhelp ~]# smbclient //192.168.5.88/open-share -U testuser1
Enter testuser1' s password: 
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
smb: >  mkdir test
smb: >  ls
  .                                   D        0  Mon Jun 27 17:14:02 2016
  ..                                  D        0  Mon Jun 27 15:06:26 2016
  test                                D        0  Mon Jun 27 17:14:02 2016

        35938 blocks of size 524288. 27592 blocks available
smb: >  exit

The above share has read and write permissions for “ testuser1” .

[root@linuxhelp ~]# smbclient //192.168.5.88/protected-share -U testuser2
Enter testuser2' s password: 
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
smb: >  mkdir samle
NT_STATUS_MEDIA_WRITE_PROTECTED making remote directory samle
smb: >  exit

Here the above share has only read permission for the user “ testuser2” .

FAQ
Q
How to check iostat of nfs mount points ?
A
For checking iostat of NFS mount points, Using command of ‘nfsiostat‘. For syntax: "# nfsiostat "
Q
How do I permanently mount the filesystem in NFS?
A
Make an entry in the "/etc/fstab", where you could mount the partition with their certain filesystem for "Network FileSystem".
Q
What is the default port of NFS server ?
A
The default port number of NFS server is "2049"
Q
What is the difference between Hard mount & Soft mount in nfs ?
A
There are Difference between soft mount and hard mount is listed below for "NFS",
Soft Mount : Consider we have mounted a NFS share using ‘soft mount’ . When a program or application requests a file from the NFS filesystem, NFS client daemons will try to retrieve the data from the NFS server. But, if it doesn’t get any response from the NFS server (due to any crash or failure of NFS server), the NFS client will report an error to the process on the client machine requesting the file access. The advantage of this mechanism is “fast responsiveness” as it doesn’t wait for the NFS server to respond. But, the main disadvantage of this method is data corruption or loss of data. So, this is not a recommended option to use.
Hard Mount : Suppose we have mounted the NFS share using hard mount, it will repeatedly retry to contact the server. Once the server is back online the program will continue to execute undisturbed from the state where it was during server crash. We can use the mount option “intr” which allows NFS requests to be interrupted if the server goes down or cannot be reached. Hence the recommended settings are hard and intr options.
Q
What are configuration files of NFS server ?
A
‘/etc/exports’ is the main configuration file that controls which file systems are exported to remote hosts and specifies options.
‘/etc/sysconfig/nfs‘ is the file through which we can fix ports for RQUOTAD_PORT, MOUNTD_PORT, LOCKD_TCPPORT, LOCKD_UDPPORT and STATD_PORT