How to Mount_Unmount Local and Network (Samba _ NFS) Filesystems in Linux?


Mounting and unmounting filesystems are essential operations in Linux that allow users to access and manage different storage devices and network resources. Whether it's local storage or network shares, properly mounting and unmounting filesystems ensures seamless data exchange and efficient utilization of resources.

In this blog post, we will explore the process of mounting and unmounting both local and network filesystems in Linux. We'll delve into the steps required to mount local filesystems and cover the configuration and mounting of network filesystems using Samba and NFS protocols.

Mounting Local Filesystems

Before we delve into mounting network filesystems, let's first understand how to mount local filesystems in Linux. Local filesystems refer to the storage devices directly connected to your Linux machine, such as hard drives or solid-state drives.

Understanding Local Filesystems

In Linux, local filesystems are represented by block devices, which can be identified using device files under the /dev directory. Common device file names include /dev/sda1, /dev/nvme0n1p1, and so on. Each device file corresponds to a specific storage device or partition.

Mounting Local Filesystems in Linux

Using the mount Command

The mount command in Linux allows you to mount a filesystem to a specified mount point. To mount a local filesystem, you need to specify the device file and the mount point. Here's the basic syntax of the mount command 

sudo mount <device-file> <mount-point>

For example, to mount the partition /dev/sda1 to the mount point /mnt/data, you can use the following command 

sudo mount /dev/sda1 /mnt/data

This will mount the filesystem to the specified mount point, allowing you to access its contents.

Mounting Filesystems at Boot with /etc/fstab

To automatically mount local filesystems at system startup, you can make use of the /etc/fstab file. This file contains information about filesystems to be mounted at boot time.

Open the /etc/fstab file using a text editor with administrative privileges, such as 

sudo nano /etc/fstab

In this file, you can add an entry for each filesystem you want to mount automatically. The entry includes details like the device file, mount point, filesystem type, and mount options.

For example, to automatically mount the partition /dev/sda1 to the mount point /mnt/data at boot time, you can add the following entry to the /etc/fstab file 

/dev/sda1 /mnt/data ext4 defaults 0 0

Save the changes and exit the text editor. From now on, the specified filesystem will be automatically mounted during system startup.

In the next section, we will explore the process of mounting network filesystems using the Samba protocol.

Mounting Network Filesystems (Samba)

In addition to local filesystems, Linux provides the ability to mount network filesystems using different protocols. One widely used protocol is Samba, which enables file and printer sharing between Linux and Windows systems.

Introduction to Samba

Samba is an open-source software suite that implements the SMB/CIFS networking protocol. It allows Linux systems to interact with Windows systems, accessing shared files and resources seamlessly.

Installing Samba Packages

Before you can mount Samba shares in Linux, you need to ensure that the Samba packages are installed on your system. Use the package manager specific to your Linux distribution to install the required packages. For example, on Ubuntu, you can use the following command:

sudo apt-get install samba

Configuring Samba Shares

To mount Samba shares, you need to configure the Samba server on the remote Windows machine or Linux machine acting as a Samba server. This involves setting up shared directories and defining access permissions.

Detailed instructions on configuring Samba shares are beyond the scope of this blog post. However, once the shares are properly configured, you can proceed with mounting them in Linux.

Mounting Samba Shares in Linux

Mounting Samba Shares Temporarily

To mount a Samba share temporarily, you can use the mount command. The basic syntax is as follows −

sudo mount -t cifs //<server>/<share> <mount-point> -o <options>

Replace <server> with the IP address or hostname of the Samba server, <share> with the name of the shared directory, and <mount-point> with the directory where you want to mount the share.

For example, to mount a Samba share located at 192.168.0.100/shared to the /mnt/samba directory, you can use the following command −

sudo mount -t cifs //192.168.0.100/shared /mnt/samba -o username=<username>,password=<password>

Replace <username> and <password> with the appropriate credentials.

Automounting Samba Shares at Boot

To automatically mount Samba shares at system startup, you can modify the /etc/fstab file. Open the file using a text editor with administrative privileges, such as 

sudo nano /etc/fstab

Add an entry for each Samba share you want to mount automatically. The entry should follow the syntax 

//<server>/<share>  <mount-point> cifs  <options> 0 0

For example, to automatically mount the Samba share //192.168.0.100/shared to /mnt/samba at boot time, add the following entry −

//192.168.0.100/shared /mnt/samba cifs username=<username>,password=<password> 0 0

Save the changes and exit the text editor. The specified Samba share will be automatically mounted during system startup.

In the next section, we will explore the process of mounting network filesystems using the NFS protocol.

Mounting Network Filesystems (NFS)

In addition to Samba, another popular protocol for mounting network filesystems in Linux is NFS (Network File System). NFS allows file sharing between Unix-like systems over a network.

Introduction to NFS

NFS enables remote mounting of directories on a network, allowing users to access files as if they were stored locally. It provides a convenient way to share resources and collaborate across multiple Linux systems.

Setting Up NFS Server

To mount NFS shares, you first need to set up an NFS server. This involves configuring the server-side machine to export directories that can be mounted by remote clients.

Setting up an NFS server requires installing the necessary packages and configuring the exports file. The process may vary depending on the Linux distribution you're using. Refer to the documentation specific to your distribution for detailed instructions.

Exporting NFS Shares

Once the NFS server is set up, you can export directories to be mounted by remote clients. The /etc/exports file controls which directories are shared and the permissions granted to remote clients.

To export a directory, add an entry in the /etc/exports file with the following syntax 

<directory> <client>(<options>)

Replace <directory> with the path of the directory you want to export, <client> with the IP address or hostname of the client machine or a wildcard * to allow access from any client, and with any desired NFS export options.

For example, to export the directory /shared to a client with IP address 192.168.0.200, add the following entry to the /etc/exports file 

/shared 192.168.0.200(rw,sync,no_subtree_check)

Save the changes and restart the NFS server for the new configuration to take effect.

Mounting NFS Shares in Linux

Mounting NFS Shares Temporarily

To mount an NFS share temporarily, you can use the mount command. The basic syntax is as follows 

sudo mount -t nfs <server>:<remote-directory> <mount-point>

Replace <server> with the IP address or hostname of the NFS server, <remote-directory> with the directory to be mounted, and <mount-point> with the local directory where you want to mount the share.

For example, to mount an NFS share located at 192.168.0.100:/shared to the /mnt/nfs directory, you can use the following command 

sudo mount -t nfs 192.168.0.100:/shared /mnt/nfs

Automounting NFS Shares at Boot

To automatically mount NFS shares at system startup, you can modify the /etc/fstab file. Open the file using a text editor with administrative privileges, such as 

sudo nano /etc/fstab

Add an entry for each NFS share you want to mount automatically. The entry should follow the syntax 

<server>:<remote-directory> <mount-point> nfs <options>  0  0

For example, to automatically mount the NFS share 192.168.0.100:/shared to /mnt/nfs at boot time, add the following entry 

192.168.0.100:/shared /mnt/nfs nfs defaults 0 0

Save the changes and exit the text editor. The specified NFS share will be automatically mounted during system startup.

In the next section, we will explore the process of unmounting filesystems in Linux.

Unmounting Filesystems in Linux

Unmounting filesystems is an important task to safely remove mounted filesystems from your Linux system. Whether it's local or network filesystems, unmounting ensures that all pending changes are written to the disk and resources are released properly.

Unmounting Local Filesystems

To unmount a local filesystem in Linux, you can use the umount command followed by the mount point. Here's the basic syntax 

sudo umount <mount-point>

For example, to unmount the filesystem mounted at /mnt/data, you can use the following command 

sudo umount /mnt/data

Make sure to replace <mount-point> with the actual mount point of the filesystem you want to unmount.

Unmounting Network Filesystems (Samba & NFS)

Unmounting network filesystems such as Samba and NFS shares follows the same principle as unmounting local filesystems. You use the umount command, but instead of the mount point, you specify the share or server name.

For example, to unmount a Samba share that is mounted at /mnt/samba, use the following command −

sudo umount //192.168.0.100/shared

Similarly, to unmount an NFS share that is mounted at /mnt/nfs, use the following command −

sudo umount 192.168.0.100:/shared

Remember to replace the share or server name with the actual share or server you want to unmount.

It's important to note that you should unmount filesystems before removing storage devices or disconnecting from network shares to avoid data loss and maintain the integrity of the filesystem.

Conclusion

Mounting and unmounting filesystems are fundamental operations in Linux that allow users to access and manage various storage resources. In this blog post, we explored the process of mounting and unmounting both local and network filesystems in Linux.

We learned how to mount local filesystems using the mount command and configuring /etc/fstab for automatic mounting at boot. Additionally, we explored the process of mounting network filesystems using Samba and NFS protocols.

Updated on: 09-Aug-2023

192 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements