Accessing Windows Directories from Linux: A Guide to Mounting



This content originally appeared on DEV Community and was authored by Raphael Coelho

In a mixed operating system environment, such as one that includes both Linux and Windows machines, users and teams often need a seamless way to access and share files across platforms. However, the inherent differences in file systems and protocols between Linux and Windows can complicate this process. For example, Linux typically uses ext4 or similar file systems, while Windows relies on NTFS, making direct compatibility a challenge. To bridge this gap, network file-sharing protocols become essential, allowing files to be accessed and modified as if they were on the same system.

One effective solution for such cross-platform file sharing is to use the CIFS protocol, which facilitates the mounting of Windows directories onto Linux, creating a unified, accessible file structure for both systems. In this article, we’ll focus on using CIFS to mount a Windows directory onto Linux, providing clear access to Windows files from the Linux side.

Before demonstrating how to set this up, let’s first dive into some foundational concepts to understand the essentials of mounting and the CIFS protocol:

What is Mount on Linux?

In Linux, “mounting” refers to the process of making a file system accessible within the directory tree, creating a link between the operating system and the storage device. After a file system is mounted, it becomes available at a specific location known as the mount point, which serves as a doorway to access its contents.

A good analogy to understand mounting is to think of a house. Imagine the house as the Linux file system, with its main entrance as the / directory (root). Adding a new device or file system to Linux is similar to adding a new room to the house. To access this room, you need a doorway, which is analogous to the mount point in Linux. The mount point is an existing directory path in the Linux file system where you attach, or “mount,” the additional storage.

What is a File System?

A file system organizes and manages how data is stored and retrieved on storage devices. It allows operating systems to create, read, update, and delete files. Different operating systems use various file system formats, like NTFS for Windows and ext4 for Linux. To access files from one system on another, the file system needs compatibility or a protocol like CIFS to bridge the gap.

What is CIFS?

CIFS, or Common Internet File System, is a protocol used for file sharing over networks, functioning as a dialect of the SMB (Server Message Block) protocol. Originally developed by IBM and later expanded by Microsoft, CIFS allows multiple users to access and share files and printers on a network.

CIFS works on a client-server model, where a client requests a file or resource from a server over the network. After an initial request, a session is established, typically requiring authentication to ensure secure access. Once authenticated, the client can perform actions on the shared resources, like reading, writing, deleting, or executing files and directories, as permitted by the server.

Creating the Mount

Step 1: Install CIFS Utilities

To mount a CIFS share, you’ll need the cifs-utils package, which provides the necessary tools to handle CIFS shares. Install it by running:

sudo apt-get update
sudo apt-get install cifs-utils

Step 2: Create a Mount Point

Choose a directory where you want the CIFS share to be accessible. Create this directory as the mount point:

sudo mkdir -p /mnt/path_to_be_shared

Note: Ensure this directory is empty, as mounting a file system over an existing directory will make the directory’s original contents temporarily inaccessible.

Step 3: Mount the CIFS Share

Use the following command to mount the Windows share:

sudo mount -t cifs //WINDOWS_SERVER_IP/SharedFolder /mnt/path_to_be_shared -o username=WINDOWS_USER,password=WINDOWS_PASSWORD
  • The first argument (//WINDOWS_SERVER_IP/SharedFolder) is the path to the Windows shared folder.

  • The second argument (/mnt/path_to_be_shared) is the mount point.

The -t option specifies the type of file system, here cifs.

The -o option allows a list of mounting options specific to the file system, in this case specifying the username and password for Windows server authentication.

Step 4: Verify the Mount

To confirm that the CIFS share is mounted successfully, use the following command:

df -h | grep windows_share

This command lists mounted file systems, filtering the output for entries related to the Windows share.

Step 5: Unmounting the Share

When you no longer need access to the mounted directory, you can unmount it with:

sudo umount /mnt/path_to_be_shared

Creating the Mount with a Credentials File

For security, storing credentials directly in the command isn’t recommended. Instead, create a file with the necessary credentials and set the appropriate permissions:

  1. Create a credentials file:
sudo nano /etc/credentials
  • Inside the file, add:
username=WINDOWS_USER
password=WINDOWS_PASSWORD
  1. Set permissions on the credentials file to protect its contents:
sudo chmod 600 /etc/credentials
  1. Mount with the credentials file:
sudo mount -t cifs //WINDOWS_SERVER_IP/SharedFolder /mnt/path_to_be_shared -o credentials=/etc/credentials

What is fstab?

The fstab file (located at /etc/fstab) is a configuration file that specifies which file systems should be mounted at boot. By adding the CIFS mount to fstab, you can automate the mounting process to ensure the Windows share is available each time the system starts. Here’s an example entry:

//WINDOWS_SERVER_IP/SharedFolder /mnt/path_to_be_shared cifs credentials=/etc/credentials 0 0

Each field specifies the following:

  • File system: //WINDOWS_SERVER_IP/SharedFolder
  • Mount point: /mnt/path_to_be_shared
  • File system type: cifs
  • Options: credentials=/etc/credentials

Conclusion

Mounting a Windows directory on Linux using CIFS is a straightforward yet powerful way to facilitate cross-platform file sharing. By understanding how mounting works in Linux and utilizing tools like CIFS, you can seamlessly integrate Windows shares into a Linux environment. This approach is invaluable for businesses and individuals who require regular, secure access to files across diverse operating systems.


This content originally appeared on DEV Community and was authored by Raphael Coelho