File sharing is one of the primary use of networking which is done by hosting a file or folder on a server that is then made accessible to network members. One of the protocols used for this is the Common Internet File System of CIFS. It is used for sharing files over a network. CIFS shares are typically used in Linux environments to allow users to access files stored on remote servers. To access the files and folders on the CIFS share, users need to mount the CIFS share on Linux.
This guide is about mounting the CIFS Windows share from the Linux OS.
- Install CIFS
- Mount CIFS Share
- Auto Mount CIFS Share
- Unmount CIFS Share
How to Install CIFS Package?
The Linux CIFS filesystem access utility is a part of the package called “cifs-utils,” which is not included in all major distributions of Linux. To install it, use the command per your Linux distribution:
$ sudo apt install cifs-utils #Ubuntu/Debian $ sudo dnf install cifs-utils #Fedora/CentOS
After entering “Y,” the installation process will continue:
The above image confirms the installation of the cifs-utility package on Ubuntu 22.04 LTS.
How to Mount CIFS Share on Linux?
Before mounting, it is necessary to have a mount point, so let’s create that first.
Create a Directory to Mount a CIFS Share: Mounting
A CIFS share requires a mount point. To create a mount point, use the mkdir command like this:
$ sudo mkdir /mnt/cifs-share
If you don’t encounter any error and the output is blank, such as above, it means the directory is created.
To confirm the creation, use the cd command like this:
$ cd /mnt/cifs-share
The above image confirms the creation of a new directory (/mnt/cifs-share).
To mount a CIFS share on Linux, the mount command is used in this way:
$ sudo mount -t cifs //SERVER_IP_ADDRESS/Windows_SHARE_NAME /mnt/cifs-share -o user=USERNAME,password=PASSWORD
Here is the breakdown of the above command:
- The mount invokes the command.
- -t indicates the filesystem type.
- cifs is the file system.
- SERVER_IP_ADDRESS defines the IP of the hosting server of the CIFS share.
- Windows_SHARE_NAME specifies the share name.
- /mnt/cifs-share is the local directory where the share is mounted.
- -o is used to specify the options separated by commas.
- USERNAME is the user name that has access to the CIFS share.
- PASSWORD is the password for the user.
How to Auto Mount CIFS Share on Linux?
The CIFS share requires re-mounting upon system reboot, and if you want to mount a CIFS share permanently, enter the details in the “/etc/fstab” file. It stores the mount information and is loaded during boot, applying the changes.
To open the “/etc/fstab” file, use any editor (nano editor is recommended) like this:
$ sudo nano /etc/fstab
At the bottom of the text, add this line:
//winserver/share /mnt/cifs-share cifs gid=users,file_mode=0664,dir_mode=0775,auto,username=USERNAME,password=PASSWORD 0 0
Here is a breakdown of the above statement:
- The //winserver/share is the IP address or a DNS hostname
- /mnt/cifs-share is the mount point as created earlier
- cifs is the file system type
- gid=users tells that all the directories on the remote system are assigned to the Linux group users.
- file_mode=0664 specifies the permissions (rw-rw-r–) on the files in the mounted directory.
- dir_mode=0775 specifies that all the directories are assigned to the users with permissions (rwxrwxr-x).
Note: Here, you can specify all the permissions.
- auto stands for automatic mount on system boot
- username=USERNAME is the user who has access
- password=PASSWORD is the password for the user
- 0 is there to ensure not to dump the system
- Following 0 tells that the system is not to be checked by fsck (checks and generates reports on disk-related errors)
And that is how a CIFS share can be mounted automatically.
How to Unmount CIFS Share on Linux?
To unmount the CIFS share after the required data is shared, use this command:
$ sudo umount /mnt/cifs-share
The above command unmounts the directory “/mnt/cifs-share” on the system and is no longer accessible remotely.
If a remote user uses the mounted directory, a lazy unmount can be performed using the “-l” flag. Once the use is over, the directory is automatically mounted using this command:
$ sudo umount -l /mnt/cifs-share
The above command unmounts the directory when it is no longer used remotely.
The CIFS share is mounted when the cfis-utility is used with the mount command. Users must add the credentials in the/etc/fstab file to mount the directory on boot automatically. This file should not be tempered as it stores all the information about the filesystems and their mount points.
This guide explained how to mount a CIFS share on Linux.