CIFS is a network-based file-sharing protocol which is used to share file, folders, and data over the network. The Windows, Linux, MacOSX, and other operating systems support the CIFS which makes it very useful to share files and folders over different operating systems. Both the SMB and CIFS protocols are developed by Microsoft and they work together without a problem. A Windows SMB share can be accessed with a CIFS client which is installed on a Linux system. In this tutorial, we will learn how to access and use SMB or CIFS Windows share from the Linux system.
Install CIFS Client For Linux
Linux distributions like Ubuntu, Debian, CentOS, Fedora, RHEL do not provides the CIFS client and tools by default. The CIFS should be installed explicitly. The CIFS client and tools are provided with the package named cifs-utils.
Debian, Ubuntu, Mint, Kali:
sudo apt install cifs-utils
Fedora, CentOS, RHEL:
sudo dnf install cifs-utils
If you are using different distributions than above the package name for the CIFS can be different. so use the term “cifs” to search and find the exact package name for your distributions.
Mount Windows SMB Share
After installing the CIFS client and tool we can use it to mount a Windows share or SMB share. The CIFS is a file system and can be used with the mount command. The mount command is used to mount different types of file systems. The -t option is used to mount Windows share by using the CIFS file system. The “cifs” is the file system name for the mount command. In the following example, we mount the windows share located in the host named FILESERVER in the path “/D/Backup” into the local Linux path “/mnt/backup”.
sudo mount -t cifs -o username=ismail //FILESERVER/D/Backup /mnt/backup
Windows shares can be accessed by using credentials which are username and password. In order to mount a windows share the username can be specified by using the “-o username=” option. In this case, we use the username “ismail”. After running this command the password is prompted with the following line. While typing the password the characters are do not shown for security reasons. So just type and press Enter.
Password:
Alternatively the remove Windows share hostname can be specified as IP address. In the following example the share is located in 192.168.1.10.
sudo mount -t cifs -o username=ismail //192.168.1.10/D/Backup /mnt/backup
List Mounted Windows Shares
After mounting a Windows share with the CIFS we can check the mount and list mounted paths in different ways. The most basic way is listing the local mount path by using the ls command.
ls /mnt/backup
Alternatively GUI based file managers can be used to list mounted local paths.
Another command to list currently monuted paths is the df command which also lists all local partitions and mount. Use following df command to list mounted windows shares.
df -l
Provide Password To Mount Windows Share
The windows share can be access with a username and password. The username and password can be provided in different ways. Generally the username is provided via the command line and password is entered into the password prompt. If we need to mount windows share in a batch mod or non-interactive mode we should provide the password with the mount command. The password can be provided with the mount command via command line interface with the password option.
sudo mount -t cifs -o username=ismail,password=Pss123 //FILESERVER/D/Backup /mnt/backup
Set Domain Name or WorkGroup Name
Windows share can be access with diffrent accounts and different authentication sources. Active Directory/Domain or WorkGroup can be used to specify user. The user domain or workgroup can be specified with the domain option like below.
sudo mount -t cifs -o username=ismail,domain=windowstect //FILESERVER/D/Backup /mnt/backup
Read Credentials From File
The windows share access crendentials can be specified via the command line interface. But this is not so secure. The executed commands are stored in the history file or logged in to a file. The credentials can be easily grabbed by others. There is a more secure way where crendetials (username and password) are stored in a file. The credential file format is like below.
username=ismail password=Pass123 domain=windowstect
The credential file is provided with the credentials option like below. There is no need to add a username, password, or domain explicitly. All of these parameters read from the credentials file.
sudo mount -t cifs -o credentials=/home/ismail/cifs_credentials //FILESERVER/D/Backup /mnt/backup
Specify the Access Permissions
The windows share is mounted with the sudo command this makes the local share path can be read only by the current user. If we want to make the share available for all other Linux users we can use the access permissions option named dir_mode and file_mode.
- dir_mode is used to specify the directories access permissions like 0755.
- file_mode is used to specify the files access permissions like 0755.
sudo mount -t cifs -o user=ismail,dir_mode=0755,file_mode=0755 //FILESERVER/D/Backup /mnt/backup
Specify User and Group ID
While mounting via the CIFS protocol the mounted partition is owned by its current owner user and group. This may create some problems with the access, modification permissions. Different user and group ID can be specified for the local share path. This changes the local share path to the specified user and group as the owner. In the following example, we set the user ID as 1001 and group ID as 1001.
sudo mount -t cifs -o user=ismail,uid=1001,gid=1001 //FILESERVER/D/Backup /mnt/backup
Auto Mount SMB Share with CIFS
Upto now we have used the mount command in order to mount a windows share with the CIFS. The mount command is generally used to mount files shares and file systems interactively via the command line interface. If we need to mount a windows share automatically when the Linux system starts and do not want to type commands the /etc/fstab can be used. The /etc/fstab configuration file stores mount information about the current Linux system which is applied during boot. We can also add network based shares to mount via CIFS.
Open the /etc/fstab file with your favouritte text editor with root privileges by using sudo command. In this case we use nano text editor. Alternatively you can be GUI based text editor like Kwrite.
sudo nano /etc/fstab
Add the following line to the fstab file.
#WINDOWS SHARE LOCAL DIR TYPE OPTIONS
//FILESERVER/D/Backup /mnt/backup cifs username=ismail,uid=1001
Unmount Windows Share
After mounting a windows share we may need to unmount this share too. Unmounting detaches the mounted windows share and can not be used locally unless mounted again. The umount command is used to unmount windows share. Just provide the local mount point or path to the umount command. The unmounting operation also requires root privielges which is provided with the sudo.
sudo umount /mnt/backup
In some cases the share can allready used by other users. In this case it can not be unmounted to prevent data corruption. First the allready used files can be listed with the fuser command.
fuser -m /mnt/backup
If you do not care for data corruption or other proeblems you can use the -l option which is used to lazy unmount for the windows share. This unmounts the file system when the usage is over.
sudo umount -l /mnt/backup