Overview
CIFS, also known as SMB, is a popular network file sharing protocol primarily used by Microsoft Windows servers and desktops. Although, in a mixed Microsoft and Linux\Unix environment, it can also be found on Linux\Unix servers.
Mounting a CIFS shares onto a CentOS box is very similar to any other network mount done in Linux, such as NFS. So if you have experience doing that, than this will be a very easy tutorial to follow along with.
Mounting can be done using two methods. You can make it a persistent mount by adding an entry into the fstab, or you can make it a temporary mount by invoking the mount command.
Simple Mount as Guest
This is a very simple way of mounting a CIFS share onto CentOS. This method is used for CIFS shares that allow guest access and don’t require an account on the file server.
mount.cifs <file-server-share> <mount-path>
In the example below, I am mounting a share called Dept from a file server called fileserver1.serverlab.intra. The share will be mounted in /mnt/dept on the local computer.
mount.cifs //fileserver1.serverlab.intra/dept /mnt/share
The example above will cause a password prompt. Being that we are attempting the mount the share as a guest, chances are there is no requirement to provide a password. To avoid having to enter a password, we can use the guest option.
mount.cifs //fileserver1.serverlab.intra/dept /mnt/share -o guest
Mount as User
This method mounts the share using credentials of a user with permissions to access the share.
mount.cifs <em><file-server-share> <mount-path></em> -o user=<em><username></em>
If the user is in a workgroup, we can specify that along with their username.
mount.cifs <em><file-server-share> <mount-path></em> -o user=<em><workgroup>\<username></em>
When we execute the mount command using the examples above you will be prompted to enter the user’s password. We can, if wanted, enter the password next to the user name to avoid the prompt; however, this method is very insecure.
mount.cifs <em><file-server-share> <mount-path></em> -o user=<em><workgroup>\<username></em>%<em><password></em>
The following example mounts a share called Dept hosted by a file server named fileserver1.serverlab.intra using a user named charlie. Charlie is located in a workgroup named Workgroup. The share will be mounted in the /mnt/dept directory.
mount.cifs //fileserver1.serverlab.intra/dept/ /mnt/dept -o user=workgroup\charlie
Mounting CIFS using Fstab
Shares mounted by executing the mount.cifs command are non-persistent. This means they will not survive a system reboot. If you want the share to be persistent, we need to add it to the fstab of the client computer. The syntax used to add the mount to the fstab is shown below.
<share-path> <mount-path> cifs <option>,<option>,<option> 0 0
Using the examples above for the mount.cifs command, the following entry would be an example of mounting a share called Dept from a file server called fileserver1.serverlab.intra. The share will be mounted as a user named charlie.
//fileserver1.serverlab.intra/Dept /mnt/dept cifs user=charlie 0 0
Additonal Mounting Options
This tutorial showed you how to use the user option. There are many other options that can be used, and some of them are listed below.
ro | Mounts the share as read-only. |
---|---|
rw | Mounts the share as writable. |
uid=arg | Sets the ownership of newly created files to a specific Linux user id. |
gid=arg | Sets the ownership of newly created files to a specific Linux group id. |
soft | Allows a file on the share to remain open and prevents the client from hanging if the file server goes offline. Attempts to read additional data from the file or to write additional data to the file will result in an error. |
cache=none | Do not cache file data on the client. |
cache=strict | Strictly follows the SMBv2 caching protocol. Useful for allowing a client to cache shared files locally, to improve access times and decrease bandwidth. |