How To Use Cifs To Mount A Windows Share On Linux

Photo of fin-expert

Made by fin-expert / Weather

About the project

The Common Internet File System (CIFS) is a mechanism for exchanging files across a network. SMB is a kind of CIFS.

Project info

Difficulty: Easy

Platforms: Raspberry PiWindowsLinux

Estimated time: 1 hour

License: GNU General Public License, version 3 or later (GPL3+)

Items used in this project

Software apps and online services

Linux Linux

Story

A Windows share can be mounted on a specific mount point in the local directory tree using the cifs option of the mount command on Linux and UNIX operating systems.

The Common Internet File System (CIFS) is a mechanism for exchanging files across a network. SMB is a kind of CIFS.

In this guide, we'll show you how to mount Windows shares on Linux computers both manually and automatically.

CIFS Utilities Packages Installation

You must first install the CIFS utilities package before mounting a Windows share on a Linux machine.

  • CIFS utilities installation on Ubuntu and Debian:
sudo apt updatesudo apt install cifs-utils
  • CIFS utilities installation on CentOS and Fedora:

sudo dnf install cifs-utils

The name of the package may vary depending on the Linux distribution.

Mounting a Windows CIFS Share

Mounting a remote Windows share works in the same way as mounting a standard file system.

Create a directory to act as the remote Windows share's mount point first:

sudo mkdir /mnt/win_share

To mount the share, run the following command as root or as a user with sudo privileges:

sudo mount -t cifs -o username=<win_share_user> //WIN_SHARE_IP/<share_name> /mnt/win_share

You will be asked to enter the following password:

Password:

There is no output when you succeed.

Use the mount or df-h commands to check that the remote Windows share has been mounted successfully.

Once the share is mounted, the mount point becomes the mounted file system's root directory. You can work with distant files in the same way that you would with local ones.

The password can be specified on the command line as well:

sudo mount -t cifs -o username=<win_share_user>,password=<win_share_password> //WIN_SHARE_IP/<share_name> /mnt/win_share

If the user is a member of a Windows workgroup or domain, you can set it to:

sudo mount -t cifs -o username=<win_share_user>,domain=<win_domain> //WIN_SHARE_IP/<share_name> /mnt/win_share

A credentials file containing the share username, password, and domain is recommended for improved security.

This is the format of the credentials file:

/etc/win-credentials

username=user
password=password
domain=domain

Users should be unable to access the file. Run the following command to set proper rights and ownership:

sudo chown root: /etc/win-credentialssudo chmod 600 /etc/win-credentials

If you want to use the credentials file, set it up like this:

sudo mount -t cifs -o credentials=/etc/win-credentials //WIN_SHARE_IP/<share_name> /mnt/win_share

The mounted share is owned by root by default, with 777 rights.

Set directory permissions using dir_mode, and file permissions with file_mode:

sudo mount -t cifs -o credentials=/etc/win-credentials,dir_mode=0755,file_mode=0755 //WIN_SHARE_IP/<share_name> /mnt/win_share

The uid and gid parameters can be used to alter the default owner of a user or a group:

sudo mount -t cifs -o credentials=/etc/win-credentials,uid=1000,gid=1000,dir_mode=0755,file_mode=0755 //WIN_SHARE_IP/<share_name> /mnt/win_share

After the -o option, add any other options as a comma-separated list. In your terminal, run man mount to receive a list of all mount options.

Mounting on its own

When using the mount command to manually mount a share, it does not remain after a reboot.

The /etc/fstab file provides a series of entries that specify where and how the filesystem will be mounted when the system boots up.

Define a mount in the /etc/fstab file to automatically mount a Windows share when your Linux system boots up. The line must include the Windows PC's hostname or IP address, the share name, and the local machine's mount point.

With your text editor, open the /etc/fstab file.

sudo nano /etc/fstab

To the file, add the following line:

/etc/fstab

# <file system>             <dir>          <type> <options>                                                   <dump>  <pass>
//WIN_SHARE_IP/share_name /mnt/win_share cifs credentials=/etc/win-credentials,file_mode=0755,dir_mode=0755 0 0

Copy

To mount the share, use the following command:

sudo mount /mnt/win_share

Mount the share using the mount command, which reads the contents of the /etc/fstab file.

The Windows share will be mounted automatically the next time you reboot the machine.

Windows Share Unmounting

The umount command removes the mounted file system from the directory tree (unmounts it).

Use the umount command, followed by either the directory where the share was mounted or the remote share, to remove a mounted Windows share:

sudo umount /mnt/win_share

Remove any entries for the CIFS mount from the fstab file.

When the share is in use, the umount command will fail to remove it. Use the fuser command to see which processes are accessing the windows share.

fuser -m MOUNT_POINT

Once you've located the processes, use the kill command to terminate them and unmount the share.

If you're still having trouble unmounting the share, try using the -l (--lazy) option, which allows you to unmount a busy file system after it's no longer busy.

sudo umount -l MOUNT_POINT

Conclusion

The mount command in Linux with the cifs argument can be used to mount a Windows shared folder.

Posts You May like: 


Credits

Photo of fin-expert

fin-expert

Tech Writer | Maker of Random Contraptions | Education | Learning — #Robotics #RaspberryPi #DIYElectronics

   

Leave your feedback...