Monday, March 21, 2011
8:00 AM

2.5 Methods to Mount a Windows Share

If you have more than one computer at your home, there is a chance that one of them runs Windows and another - Linux. Then, you are more than likely to have a necessity to share files between your computers. Say, you have something on your Windows-driven computer that you desperately need to work with on your favourite Linux computer.


Process overview

The process of sharing consists of 2 steps:

  1. First, you need to define your Windows folder as shared one. I will not describe this process here. It is described one million and one time on other resources. For example, you can read Windows XP manual here.
  2. Second step is actual mapping of shared folder to some location in Linux. I should admit that this topic is also discussed many times on different resources. 
What I also noticed that most of the methods go deep into understanding of user access rights, user IDs and other stuff which you might not know or need with simple network. If you shared your Windows folder the straightforward way, most likely it has no restrictions in access rights, does not require any user/password. That's why here I will show you how to simply use Windows share from Linux.
Process of connection to remote or local drive in Linux is called mounting.

Preparation

Before we start actual mounting of our remote folder, we need to have 2 things:
  1. Samba. Simply speaking, this is software package which allows you to access remote locations as local. If you do not have Samba installed on your computer, you need to get it. Most convenient way is to use your package manager, graphical or command line based. For example, if you use Debian or Debian-based application, you may type
    sudo apt-get install samba
  2. Folder (directory) where you want to see your remote drive in your Linux system. Linux does not have "disks" like Windows, so C:, D: or Z: are not available here. Instead, you have some directory tree which always starts with / and every single file can be access there. For mounted partitions you can use folder /mnt and create subfolders there. But nothing forbids you to use any other location. You can create your folder in file broswer (Nautilus, Dolphin or other) or from command line. Say, I want to create a folder at the top level of the tree:
    sudo mkdir /fnd
    For second level of the tree it should be
    sudo mkdir /mnt/fnd
Once preparation steps are finished, we can start mounting.

Method 0.5

First, let's try to mount remote drive manually. This is done with command mount.
sudo mount -t cifs //fnd/public /fnd -o guest,nolinux,iocharset=utf8
Let's see what we have asked Linux to do here.
  • sudo - all mounting operations are done from user root. So, if you're not working as root, you need to use sudo command. Of course, you'll be asked for root password.
  • mount - command for mounting
  • -t cifs - here we explain what filesystem type we are going to connect to. CIFS is filesystem for Samba.
  • //fnd/public - remote computer name and path to folder on it. In this case FND is my remote computer where I have folder shared as PUBLIC.
  • /fnd - local folder which we have created before. This is out mounting point.
  • -o guest,nolinux,iocharset=utf8 - parameters for mounting. In particular, first option tells Linux to connect to remote system as guest, i.e. without asking for username and password. Next option tells that remote system is not Linux system, so Linux will not expect user access rights assigned there the Linux way. Last of listed options is specifying which charset to use when mounting remote drive, and utf8 is usually good choice if you use non-Latin characters.
Once this command is executed, your remote drive is mounted. Of course, you may get some error messages. In this case please ask Google or any other search engine for error codes you get. I would also recommend you to check your system log with command
dmesg | tail
Do check there whether you have any messages related to your last mounting command.
Once you have folder mounted, you can start using it. Read files from there. Create new files. Move them around - to and from that folder. Delete unnecessary files and folders. In other words, test what you have just done. If you are unsuccessful in some of the operations, read on. I discuss some other mounting parameters later.
If you need to change some mounting parameters, please remember that you need to disconnect your remote folder first. In Linux terms - unmount.
sudo umount /fnd
This is the way to unmount anything connected to /fnd. You can do another mount command after that.

Of course, this is the way to relatively quickly mount network folder to your Linux machine. And this is OK if you only need to mount once. But what if you need to have your remote partition mounted every time you start your Linux computer? Manual mounting is still an option, but not the most convenient. Let's count it as a half-option.

Method 1

Most Linux systems allow you to mount (not only) network drives automatically. To do so, you need to edit file /etc/fstab. Please note, you should not delete or edit existing records in that file unless you are 100% sure what your are doing. We will add record there which will automatically mount our network drive.
First of all, we need to call file editor. You need to do this as root user, so command like
gksudo gedit /etc/fstab
or
kdesudo kate /etc/fstab
should be used, depending on which Desktop Environment or Window Manager you use. Commands above are for GNOME and KDE respectively. Please refer to your DE/WM documentation for analogue of sudo, and also use your favourite text editor.
New record in the file will look like
//fnd/public /fnd cifs noserverino,nolinux,guest,iocharset=utf8,rw,file_mode=0777,dir_mode=0777 0 0
You see that fstab record is very similar to what we have done for mount. Order of parameters is slightly different. First we specify remote folder, then comes mounting point, and then list of options.
Please remember to leave two zeroes at the end of the record. They are important.
There are no parameters -t and -o in the fstab record. They are not necessary here.
You may notice that my fstab record lists more options compared to what I have used in mount before. Let's see what else is there.
  • noserverino - this parameter tells Linux that it should not expect unique file identifiers from server. Little bit technical, but worth doing with Windows shares.
  • rw - we want to have fully read and write access to the folder, don't we? Otherwise use ro.
  • file_mode=0777,dir_mode=0777 - tells Linux our access rights to the files and folders on remote server. Since our server is not Linux, there might be misunderstanding between computers about who and how controls the files and folders. We eliminate that misunderstanding. We want to control everything and share everything with everybody. 
Once record is in /etc/fstab, do not forget to save the file. In order to check how fstab works, you may wish to reboot. But there is simpler way.
sudo umount /fnd
sudo mount -a

First command unmounts our network drive. Seconds mounts everything from fstab, including our new record.
So, putting just one record into fstab file allows us to automatically mount shared folder to Linux system.
This method works in all Linuxes. Except...

Method 2

Every rule has exceptions. Some Live Linuxes allow you to save user changes, but do not allow to change fstab file. Examples? Puppy Linux, SLAX. Probably their derivatives too, I have not checked. How can we automatically mount shared network folder in there?
Each Linux system allows you to run scripts. They are analogues of .bat files in Windows. So, we need to have a script for drive mounting and then execute it during system start. Most DE/WM have option to start some scripts or programs automatically. This is like Autostart in Windows. For examples given above, Puppy has folder Startup and SLAX has KDE-specific hidden folder ~/.kde/Autostart. Any files placed there will run every time DE/WM starts.
Another problem which I found with different distributives that network can start any time, even after WM/DE. If Linux tries to mount network drive while network is down, result is... no result. That is not what we expect, don't we?
When I was looking for the solution of automatic network drive mounting in those systems, I created following script:
#! /bin/sh
#reset counter and file
counter=0
file="/tmp/file.txt"
echo $counter > $file

until ping -w 1 fnd | grep 'seq'
do
  sleep 1
  let "counter += 1"    #or counter=`expr $counter + 1`
#get results of ping into file and get counter results
  ping -w 1 fnd | grep 'seq' >> $file
  echo "$counter" >> $file
done

#main body
mkdir -p /mnt/fnd >> $file
mount -t cifs //fnd/public /mnt/fnd -o guest,nolinux,iocharset=utf8 >> $file
#control shot
chrome

Let's see what is here.
First part resets counter to 0 and creates a new file with value 0 at first line. That file can be used for debug purposes.
Second part tries to ping network drive. Obviously, ping is only successful if network is up. If ping is unsuccessful, system waits 1 second and repeats attempt. Counter is increased every second, and file is updated with new line with counter value. Successful ping result contains subsctring "seq", which is signal for script to continue.
Once script is sure that network is up and running, i.e. ping returns "seq" substring, actual mounting happens. Here I create folder /mnt/fnd using command mkdir with key -p. This key means that no message is issued if folder is already there. Thus, we are sure that mounting point exists. Once it exists, we can use it in next command. You know everything about this command already, except for the >>$file parameter which adds output of the command into the file for debug information.
And last part of the script is control shot. I start application chrome as last step of the script. When I see Chrome started, I know script has finished its work.
Once you put this script into the autostart folder in your Linux filesystem, couple of additional steps required:
1) Ensure that user root owns the file. You can check in your file browser or using command
ls -l <path_to_folder>
If file is owned by somebody else, use command chown to fix situation. Refer to documentation about chown usage. If you are using Puppy or SLAX, you do not need to do this, as you are always root in these Linuxes.
2) Check that file is executable. Same command as above should have x as parameter in file options. If there is no x, then use command
sudo chmod +x <filename>
Script is ready for use!
This time you need to reboot in order to check that script works.
This is another method to connect to Windows share from Linux.

You can see, Linux is full of options. Which one to use? Depends on your requirements and Linux version you use!

Do you have any other recommendations about usage of Windows shares in Linux? Leave your comments using form below.

0 comments:

Post a Comment