Mount a Windows/ Samba Share in Linux
Category : How-to
Given the amount of trouble you can have getting Linux and Windows to play nicely together, you may be surprised to find out that it’s easy to mount either a Windows file share on Linux. You can also use the same method to mount a Samba share.
This article assumes you already have a Windows or Samba share set up which is protected by a username and password. The user could be part of a domain or local to the machine which presents the storage.
The first step is to create a credentials file which will contain the username and password for accessing the file share. This needs to be in a protected area so that the details cannot be viewed. For this example, I will put the file in the root home directory.
Add the following details, substituting the below details
- [DOMAIN] – this is the domain for the file share user. This may not be required in all environments.
- [USER] – this is the user name for the file share.
- [PASSWORD] – this is the password of the above user.
Before continuting with the configuration, we need to make sure that the required packages are installed. On Ubuntu, you can use the apt-get command. Use your distributions package manager for other Linux distributions.
apt-get install cifs-utils
The next step is to create a folder where you will mount the Windows or samba share to in Linux. I will use the mount point /mnt/winshare.
Now open fstab and add a new row at the bottom of the file to mount the remote share to the local folder. Add the below line to your fstab and substitute the values for your environment.
- [SHARE_LOCATION] – is the remote host name/ DNS and shared folder name.
- [MOUNT_LOCATION] – is the local path which will be used as the mount point for the Windows or Samba share.
- cifs – is the type of mount to use. For older Linux systems you may need to use smbfs.
- [CREDENTIALS_FILE] – is the location of the local credentials file.
//[SHARE_LOCATION] [MOUNT_LOCATION] cifs credentials=[CREDENTIALS_FILE],iocharset=utf8,uid=1001,_netdev,file_mode=0770,dir_mode=0770 0 0
//windowsbox/sharename /mnt/winshare cifs credentials=/root/.smbcreds,iocharset=utf8,uid=1001,_netdev,file_mode=0770,dir_mode=0770 0 0
Finally we need to create the mount which will use the details in fstab. Change [MOUNT_POINT] to match the mount point you created.