How to Mount Windows Share on Ubuntu Linux

mount windows share on ubuntu linux

CIFS (Common Internet File System) is a popular file sharing protocol on Internet. It allows users on a Linux system access to a particular mount point on a windows share.

CIFS is an implementation of SMB (Server Message Block) - a protocol used for network file sharing.

This tutorial will go through all steps of installing and configuring relevant utilities in order to mount windows share on Linux system.

Installing CIFS

Install cifs-utils package on Ubuntu Linux by using the following command line

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

Mounting a Windows Share

In this section, the tutorial will show you the way to manually and automatically mount windows share on Linux systems.

Creating a directory on your Ubuntu Linux machine, the name of directory is arbitrary. In the below command line, I created a folder named winshare under /mnt

$ sudo mkdir /mnt/winshare

/mnt/winshare is the mount point of the remote windows share.

Windows share can be mounted on your Ubuntu Linux system mount point using cifs option of mount command

$ sudo mount -t cifs -o username=$windows_user,password=$windows_user_password //WIN_SHARE_IP/$shared_name /mnt/winshare

Where:

WIN_SHARE_IP is the IP address of windows machine.

If the $windows_user is in a windows domain, specify the domain as the following command line

$ sudo mount -t cifs -o username=$windows_user,password=$windows_user_password,domain=$windows_domain_name //WIN_SHARE_IP/$shared_name /mnt/winshare

By default, linux mount windows share with the full permission (rwx or 777). If you want to change the permission on your own, please use the dir_mode and file_mode options to set permission for directory and file.

$ sudo mount -t cifs -o username=$windows_user,password=$windows_user_password,dir_mode=0755,file_mode=0755 //WIN_SHARE_IP/$shared_name /mnt/winshare

You also can change the default ownership of user and group by specify the uid (user id) and gid (group id) options.

$ sudo mount -t cifs -o username=$windows_user,password=$windows_user_password,uid=1000,gid=1000,dir_mode=0755,file_mode=0755 //WIN_SHARE_IP/$shared_name /mnt/winshare

Once the windows share is successfully mounted, using command df -h for verifying the mounting windows share in Linux. In the following example, WIN_SHARE_IP = 192.168.1.8 and $shared_name = sharefolder

$ df -h
Filesystem                 Size  Used Avail Use% Mounted on
udev                       3,9G     0  3,9G   0% /dev
tmpfs                      787M  2,2M  785M   1% /run
/dev/sda2                  450G   23G  405G   6% /
tmpfs                      3,9G  705M  3,2G  18% /dev/shm
tmpfs                      5,0M  4,0K  5,0M   1% /run/lock
tmpfs                      3,9G     0  3,9G   0% /sys/fs/cgroup
//192.168.1.8/sharefolder  300G  5,7G  295G   2% /mnt/winshare

Secure CIFS credential

This section will explain how to use a credential file when your Ubuntu Linux mount the share using command.

Create a cifs credentials file: /etc/cifs-credentials. The file contains the below information:

username = $windows_user
password = $windows_user_password
domain = $windows_domain_name

Grant permission read and write to credentials file:

$ sudo chmod +rw /etc/cifs-credentials

Now, we can mount the share using credentials with command as follows:

$ sudo mount -t cifs -o credentials=/etc/cifs-credentials //WIN_SHARE_IP/$shared_name /mnt/winshare

Auto mount the shares

If you manually mount the shares using mount command, when you reboot your Linux machine, the shares will be lost.

The file /etc/fstab contains the necessary configuration that allows automatically mount cifs permanently.

Edit the /etc/fstab file with your favorite editors (vim, nano,...)

$ sudo vim /etc/fstab

Then add the following line to the file.

//WIN_SHARE_IP/$shared_name /mnt/winshare cifs credentials=/etc/cifs-credentials,file_mode=0755,dir_node=0755 0 0

Run the command to mount all the entries listed in /etc/fstab

$ sudo mount -a

Since then, the mount cifs will be persistent across reboots.

Unmount the shares

In order to unmount a share, you have to determine the mount point. In the above example, mount point is /mnt/winshare. Use the umount command:

$ sudo umount /mnt/winshare

If the mount point is in a busy process and the above command failed, run command with option -l (--lazy)

$ sudo umount -t cifs -l /mnt/winshare

Conclusion

The tutorial has gone through all steps to mount a windows share on Ubuntu Linux using CIFS. If you have any questions, feel free to reach out to us.

Bobbin Zachariah 7:39 am

Comments

Your email address will not be published. Required fields are marked *

2 Comments

  1. I do not understand why you need to specify the Windows machine IP adress. These are constantly changing when using DHCP, which almost everyone uses. Does that mean that one has to go through the above procedure every day if one wants to connect to a Windows machine whose IP address has changed (people shut off their computers at night, likely getting a new IP address asigned the next day when turned on)? Is there a way that automatically detects the target Windows machine, as Windows does in its own networking system?