How to Mount Windows Share on Linux via CIFS

July 20, 2023

Introduction

A Windows share is a folder on a Windows machine that can be accessed by other machines within a network. The contents can be modified or retrieved by authorized users on other machines connected to the same network.

On the other hand, CIFS (Common Internet File System) is a network file-sharing protocol that allows different operating systems, including Linux, to access files and services on Windows and vice versa.

In this tutorial, you will learn to mount a Windows share on a Linux machine using CIFS.

How to mount Windows Share on Linux using CIFS - a tutorial.

Prerequisites

  • A system running Linux.
  • Root or sudo access.
  • A shared folder or drive on a Windows machine.

How to Mount CIFS Share

Mounting a remote Windows share via CIFS is similar to mounting regular file systems. Follow the steps below to mount a CIFS share on Linux.

Step 1: Install CIFS Utilities Packages on Linux

The CIFS-Utils package provides the necessary utilities for mounting SMB/CIFS shares on a Linux system. The installation steps differ depending on which Linux distribution you are using.

For Debian-based systems:

First, update the local package repository to ensure the latest available package gets installed:

sudo apt update

Run the following command to install the CIFS-Utils package:

sudo apt install cifs-utils -y

For RHEL-based systems:

First, update the system repository:

yum update

Next, install the CIFS-Utils package by running the following command:

yum install cifs-utils

For Fedora:

Run the following command to update the package repository:

sudo dnf update

Install CIFS-Utils by running:

sudo dnf install cifs-utils

After the package installs, move on to the next section to create a mount point for the Windows share.

Step 2: Create a Directory to Mount Windows Share

To mount the Windows Share, create a directory on the Linux machine to act as a mount point for Windows Share. You can create this directory anywhere you want.

For this tutorial, we'll create a directory named winshare in the /mnt directory:

sudo mkdir /mnt/winshare

Step 3: Mount a CIFS Windows Share

After creating a directory, mount the Windows share. Use the following syntax:

sudo mount -t cifs //[IP_Address]/[share_name] /mnt/winshare -o username=[username]

Replace the [IP_Address], [share_name], and [username] with your details.

When prompted, enter your password and hit Enter.

Mounting a Windows share on Linux using CIFS.

After running the command and mounting the share, you should be able to access it from the /mnt/winshare directory on your Linux machine. Use the cd command to move to that directory and list the contents using ls:

Listing the contents of a directory in Ubuntu.

If you don't have access to the share, check if it is mounted correctly by running:

df -h
Checking which filesystems are mounted in Ubuntu.

If the share is correctly mounted, the output shows an entry for the remote Windows share and its mounted location on your Linux system.

Note: Check out our tutorial for mounting remote file systems over SSH.

Step 4: Make the Windows Share Automatically Mount at Boot

Make the Windows share mount automatically at boot to avoid mounting it after each system reboot. To do so, edit the /etc/fstab file using a Linux text editor. In this tutorial, we will use nano.

Follow the steps below:

1. Store your login credentials in a separate file for security reasons. Use this command to create a new file:

sudo nano /etc/cifs-credentials

2. Add your username and password to the file using the following format:

username=[username]
password=[password]

Replace [username] and [password] with your credentials.

For example:

Creating CIFS credentials.

Save and close the file.

3. Change the file permissions to prevent unauthorized changes or access:

sudo chmod 600 /etc/cifs-credentials

4. Open the /etc/fstab file:

sudo nano /etc/fstab

5. Add the following line at the end of the file:

//[IP_address]/[share_name] /mnt/winshare cifs credentials=/etc/cifs-credentials 0 0

Replace [IP_address] and [share_name] with your information.

Save and close the file. Your Windows share will now automatically mount when the system boots.

How to Unmount CIFS Windows Share

Unmount the Windows share from your Linux machine with the umount command:

sudo umount /mnt/winshare

The command unmounts the Windows share from the /mnt/winshare directory. If you used a different directory as the mount point during setup, specify that directory in the command.

If errors arise when unmounting the share, it may be because a user is currently accessing the share or a file is open.

Force unmount the share by running the following command:

sudo umount -f /mnt/winshare

The -f (--force) flag force unmounts the share from your system.

Conclusion

This guide showed how to mount a Windows share on a Linux machine using CIFS and how to unmount it. The CIFS Windows is handy when you need to access files on a Windows machine using Linux.

Next, see how to install Samba on Ubuntu, an open-source Linux utility that enables file sharing, or install an NFS server on Ubuntu and view, store, share, or update files on remote computers.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
How to Mount NTFS Partition in Linux
October 8, 2020

In Linux, an NTFS partition is accessed in dual-boot setups where file exchange between drives is required. This article provides a detailed explanation of the NTFS partition mounting process.
Read more
How to Format Disk Partitions in Linux
December 2, 2020

Formatting and mounting disk partitions in Linux is common because partitions cannot be used without this process. In this tutorial you will learn how to format and mount disk partitions.
Read more
Linux Commands Cheat Sheet: With Examples
November 2, 2023

A list of all the important Linux commands in one place. Find the command you need, whenever you need it or download our Linux Commands Cheat Sheet and save it for future reference.
Read more
How to Transfer Files with Rsync over SSH
January 31, 2020

Rsync is a Linux tool that allows you to transfer data over SSH to a remote server securely. Use the options for the rsync command to adapt the tool for your specific use case.
Read more