HowTo: Mount Remote Windows Partition (Share) under Linux
The article describes how to mount CIFS shares manually. The shares can be on a Windows computer or on a Linux/UNIX server running Samba.
Prerequisites
1. Install cifs-utils , if it hasn’t been installed yet.
This package contains tools for mounting shares on Linux using the SMB/CIFS protocol.
2. Check that NetBIOS service is running and reachable on the remote host.
Port 139 TCP – NetBIOS (Windows File and Printer Sharing).
It allows applications on separate computers to communicate over a local area network.
You can test it with telnet or nmap .
Mounting Remote Windows Share
Run all commands as root (use sudo).
Create mount point
Mount password protected network folder
You can use Computer/Server Name instead of IP Address.
- ‘mount -t cifs’ – mount using the Common Internet File System (CIFS);
- ‘-o’ – options passed to mount command;
- ‘user=’ – username;
- ‘pass=’ – password;
- ‘dom=’ – domain e.g WORKGROUP (if server in domain).
How to Mount Windows Share on Linux using CIFS
Posted Nov 23, 2019
On Linux and UNIX operating systems, a Windows share can be mounted on a particular mount point in the local directory tree using the cifs option of the mount command.
The Common Internet File System (CIFS) is a network file-sharing protocol. CIFS is a form of SMB.
In this tutorial, we will explain how to manually and automatically mount Windows shares on Linux systems.
Installing CIFS Utilities Packages #
To mount a Windows share on a Linux system, first you need to install the CIFS utilities package.
Installing CIFS utilities on Ubuntu and Debian:
Installing CIFS utilities on CentOS and Fedora:
The package name may differ between Linux distributions.
Mounting a CIFS Windows Share #
Mounting a remote Windows share is similar to mounting regular file systems.
First, create a directory to serve as the mount point for the remote Windows share:
Run the following command as root or user with sudo privileges to mount the share:
You will be prompted to enter the password:
On success, no output is produced.
To verify that the remote Windows share is successfully mounted, use either the mount or df -h command.
Once the share is mounted, the mount point becomes the root directory of the mounted file system. You can work with the remote files as if they were local files.
The password can also be provided on the command line:
If the user is in windows workgroup or domain you can set it as follows:
For better security it is recommended to use a credentials file, which contains the share username, password and domain.
The credentials file has the following format:
The file must not be readable by users. To set the correct permissions and ownership , run:
To use the credentials file, define it as follows:
By default of the mounted share is owned by root, and the permissions are set to 777.
Use the dir_mode option to set the directory permission and file_mode to set the file permission:
The default user and group ownership can be changed with the uid and gid options:
To set additional options , add them as a comma-separated list after the -o option. To get a list of all mount options type man mount in your terminal.
Auto Mounting #
When the share is manually mounted with the mount command, it does not persist after a reboot.
The /etc/fstab file contains a list of entries that define where how and what filesystem will be mounted on system startup.
To automatically mount a Windows share when your Linux system starts up, define the mount in the /etc/fstab file. The line must include the hostname or the IP address of the Windows PC, the share name, and the mount point on the local machine.
Open the /etc/fstab file with your text editor :
Add the following line to the file:
Run the following command to mount the share:
The mount command, will read the content of the /etc/fstab and mount the share.
Next time you reboot the system, the Windows share will be mounted automatically.
Unmounting Windows Share #
The umount command detaches (unmounts) the mounted file system from the directory tree.
To detach a mounted Windows share, use the umount command followed by either the directory where it has been mounted or remote share:
If the CIFS mount has an entry in the fstab file, remove it.
The umount command will fail to detach the share when it is in use. To find out which processes are accessing the windows share, use the fuser command:
Once you find the processes, you can stop them with the kill command and unmount the share.
If you still have problems unmounting the share, use the -l ( —lazy ) option, which allows you to unmount a busy file system as soon as it is not busy anymore.
Conclusion #
In Linux, you can mount a Windows shared using the mount command with the cifs option.
If you have any questions or feedback, feel free to leave a comment.
How to Mount a Windows Share in Linux
In this tutorial we will be discussing different ways to connect to a Microsoft share form Linux. Before we dive in I think it is important to understand the different terminology used when talking about Samba, SMB and CIFS shares.
SMB vs CIFS vs SAMBA
SMB (Server Message Block) is a protocol used for network file sharing. It was originally designed at IBM in the 1980’s.
CIFS (Common Internet File System) is a implementation of SMB that was created by Microsoft.
SAMBA is an open source software suite and set of utilities used by UNIX/Linux to communicate with Microsoft Windows systems.
From the average user stand point SMB and CIFS can be used interchangeably. As you will see below, we will connect to the same share using both SMB and CIFS.
Mount a Windows Share Using GUI (Files / Nautilus)
You can easily connect to a Windows share using the Files application (formerly known as Nautilus) that ships with Gnome. For the sake of clarity, we will call it Nautilus.
Open the Nautilus application and select «Other Location» from the left places panel.
At the bottom of the screen you will see «Connect to Server» with a box asking for you to enter the server address. Inside that box is a diamond with a question mark in it that you can click for some help.
As you can see in the pop-up, server addresses are made up of a protocol prefix and an address. On my Windows machine named «MyMuse» I created a share called «SharedDocs». So inside that entry box we will use:
We can also use the IP address:
This will bring up a dialog box asking for authentication credentials.
Select the «Registered User» radio button and enter your details. You have three options for storing your password.
- Forget password immediately — This will log you in and and soon as your session is over you will be required to provide your credentials again.
- Remember password until you logout — Remember your password until you explicitly logout.
- Remember forever — This option will store the credentials and allow you to easily return to the share.
Once you are connected, Nautilus will show the location on the left panel allowing easy access to the share.
Be aware that once that connection is closed you will have to go back to «Other Locations» and use the drop down box to reconnect. If you want to make the share persistently accessible from the left menu you will need to add a bookmark.
NOTE: If you do not want to type your credentials every time ensure that you select «Remember Forever» when authenticating to the share.
To add a bookmark, connect to the share then right click it in the left panel and select «Add Bookmark».
If you have a lot of shares, or multiple shares on the same device, it will be hard to differentiate them. You can rename them to something more user friendly by right clicking on the bookmark and selecting «Rename. «.
Now we can easily navigate to our share as the name is clearly visible and easy to identify.
Mount A Windows Share Using Command Line
Using the mount.cifs Command
You can easily mount a cifs share from the command line using the mount.cifs command. Using the mount command requires root privileges.
First, create a directory to mount the share:
Use the mount.cifs command to mount the share at the created directory.
You can use the IP address as well.
Alternatively, you can use mount -t cifs with the same syntax.
Automatically Mount CIFS Share at Boot
Mount CIFS Share at Boot Using /etc/fstab
You can add your CIFS mount point in /etc/fstab and have the system mount it during boot. However, there are some considerations to make. If the share is not available at boot time the system will hang until the mount attempt times out, or worse, refuse to boot. This will cause a delay in accessing the system. To avoid this you can use autofs (next section). Also mounting it via fstab requires you to put your credentials somewhere in plain text. You have two options when using fstab to mount a CIFS share.
- Put the credentials in the fstab file itself — This is a huge security risk as fstab is readable by any user. Someone can simply cat the file and see your credentials. IT IS NOT RECOMMENDED!
- Use a credentials file — This entails saving your credentials in a separate file which can then be protected by permissions. Although your credentials will still be saved in plain text, someone would need root permissions to read them.
Because of the high risk of the first option, we will not even discuss how to do it. It is easy enough to use a credentials file.
First, create a hidden file in root’s home directory (/root/). You can call this file whatever you like. Most people call it .smbcreds or similar. In this file you need two lines, one containing «username= » and one containing «password= «.
Now set the permissions so it is only readable by root:
Now edit the /etc/fstab file and add the following information:
- Remote server share address (//mymuse/shareddocs)
- Local mount point (/mnt/cifs)
- Filesystem type (cifs)
- options (credentials file=/root/.smbcreds)
- dump option (0)
- check option (0)
Example fstab, last line showing the CIFS mount point.
Mount CIFS Share using AutoFS
The most obvious benefit of using autofs is that the share is only mounted when being accessed. If it is unavailable it will not effect your system when it boots.
Using autofs has the same credentials issues described above with fstab. It is also necessary to store your password in plain text when using autofs for cifs shares.
Installing autofs Package
Install autofs in CentOS 7, or Red Hat 7:
Install autofs in Ubuntu 18:
Let’s configure autofs to mount our Windows share.
Create Credentials File
First, ensure you create the credentials file as described above:
DO NOT forget to set the permissions:
Get UID for User
Now, let’s get our user accounts uid so we can tell autofs to mount the share as our user instead of root.
Edit auto.master Map File
Edit the /etc/auto.master configuration file. In this file we will provide a map of directories to shares for autofs to use. Add the following line to the file:
The first part of the above line is the directory we are using as a mount point (you can create a custom directory if you wish). The second is the file that autofs should look in for instructions on how to connect to the resource and mount it.
Create the Shares File
Next edit or create the /etc/auto.shareddocs file and add the following:
Restart the Service
Now we must restart the autofs service so it reads the new configuration.
Test and Verify
Now, let’s look in the /mnt directory.
The shareddocs directory doesn’t exist until you try to access it (auto fs). If everything above was followed, we should be to cd into that directory even though we do not see it. The directory will be dynamically created as it is accessed.
Conclusion
In this article we covered 3 different ways to mount your Windows shares in Linux. You should now be able to mount an SMB share via the GUI, manually on the command line, automatically on boot and with autofs.
An important point I would like to stress is make sure you protect your credentials. I hope you enjoyed this tutorial, please leave feedback or questions in the comments below.