11.16.07

Mounting Windows SMB File Shares Using Cifs

Posted by ryan in linux


Reference: http://ubuntuforums.org/showthread.php?t=288534

In the world of linux and Samba File Shares, there are essentially two ways to mount your SMB shares. Of course, you can always access your shares via the Places->Network dialog, where you can go to town looking through your shared drives. Of course, this has some major downfalls. Primarily, most applications don't recognize the network folders as legitimately mounted disks (because they're not). In other words, try opening up a shared music file in Audacious (or your favorite music player). Did it work? Well, if you're more clever than me, perhaps. The true way to handle your shared files is by mounting them. Then we'll have a legitimately mounted drive, with which we can do whatever we want.

Like I said, there are two ways to mount your filesystem. The first is the original smbfs, and the latter is the newer and more preferred method of cifs. While I originally used smbfs (it made sense, its name looks just like what I'm trying to mount), today (Nov 16, 2007), when I did the ubuntu system update, everything went crazy. All of a sudden, my smbfs mounted drives ground to a halt - each request took about 1-2 minutes to fulfill. I could still browse the files quickly through Places->Network, but my smbfs shares were crawling.

But then I remembered that there's an alternative, and now that I have it up and running, I can say that it's quite easy and quite fast. Here's what you do:

Firs, we need to install cifs, which comes right alongside smbfs. If you have smbfs, then you have cifs. If you don't, open up a terminal and get it:

sudo apt-get install smbfs

Next, all mounted drives need to mount to some folder on your local drive. I prefer to mount mine in the /media folder, which makes sense, as your cdrom is mounted there along with other drives, including things like external and thumb drives. Type the following, replacing my_share with whatever folder name you want your share to be (this doesn't matter at all, it can be called anything and go anywhere).

sudo mkdir /mount/my_share

Now, while we're going to perform some permanent mounts later (so that the disk mounts after a restart), let's start with a manual mount.

sudo mount -t cifs //192.168.1.102/share_name /media/my_share -o username=theuser,password=thepass,iocharset=utf8,file_mode=0777,dir_mode=0777

Let me explain. The mount -t cifs command simply instructs the system to mount the following parameters via cifs. The next argument: //192.168.1.102/share_name consists of 2 parts. The first is the ip address of the computer with the shares. This can also be "net bios name", but since that was giving me a headache, I simply used the ip address. The second part: share_name is the name of the share on the hosting computer. Under Places->Network, you should see the computer that you are trying to connect to. If you double click into that computer, you will see (you may have multiple) the name of the share (something like my-external-drive perhaps). Use that value in place of share_name.

The next part of the command: /media/my_share is that directory that we created earlier. After the mount is successful, the contents of the share will be accessible via this directory.

Finally, everything that comes after the -o are the options. First, we have the username and password. These should be set to the username and password of the windows computer to which you are trying to connect. For me, my "windows" computer was a windows computer at all, but rather an ubuntu server (we have many types of computers needing to connect to it). In this case, I used the username and password of my ubuntu use. Exact same idea. The things after that shouldn't need modification. They set the charset to utf8 (so that we're nice to foreign character sets), and make the mount read/write.

Now, if it worked, you should see your mount right on the desktop. Browse through it, play music from it, watch movies from it (network speed permitting). Here are some common errors:

  • mount error: can not change directory into mount target... - either you didn't create your /media/my_share directory or specified it wrong in your mount command.
  • mount error 13 = Permission denied - your username and password are being rejected. Try some other combinations
  • mount error 6 = No such device or address - the share_name that you specified in your mount command is not being found on the host computer. Browse the network, find that computer, and check to make sure you see the exact share name.

We now have proof of concept, but we want the drive to mount automatically on restart. First, let's unmount the drive so that we can test our remounting of it later. The following should do it.

sudo umount //192.168.1.102/share_name

The share should've disappeared from your desktop. Important, we will use the /etc/fstab file to mount the share by inserting a similar command as above into that file. This file can be read by anyone on your computer, which would mean that anyone could read the username and password you specify for you mount. If you don't care (come on guys, care a little), then simply do the following:

sudo nano /etc/fstab

...and add the following to the bottom, updating as necessary:

//192.168.1.102/share_name /media/my_share cifs username=theuser,password=thepass,iocharset=utf8,file_mode=0777,dir_mode=0777 0 0

For the rest of us who desire a little more security, the following is the clever way of hiding the username and password.

sudo nano /root/.smbcredentials

and enter in the file:

username=theuser
password=thepass

Now, let's make sure only root can read this file:

sudo chmod 700 /root/.smbcredentials

Now, we simply need to replace the username and password portion of the fstab file with a handly credential option.

sudo nano /etc/fstab

and make your mount command look like this:


//192.168.1.102/share_name /media/my_share cifs credentials=/root/.smbcredentials,iocharset=utf8,file_mode=0777,dir_mode=0777 0 0

And we're done. You can now either restart to test, or simple run the following command, which remounts all the drives in the /etc/fstab file that are not currently mounted (very handy):

sudo mount -a

If you saw your share pop up on the desktop, then you're done. Your brand new cifs file share will mount each time you boot up your computer.

Thanks for the shares!
  • StumbleUpon
  • Sphinn
  • del.icio.us
  • Facebook
  • TwitThis
  • Google
  • Reddit
  • Digg
  • MisterWong
Posted by tf on 2009-01-23
A huge help. Thank you!
Posted by paulinrutland on 2009-02-04
Fantastic for a linux newbie, got all my netwrok drives mounted now on my Acer Netbook. Many thanks for such an easy to follow guide.
Posted by jayk on 2009-03-25
exactly what i was looking for. Thanks!
Posted by Kevin on 2009-04-23
How do you prevent to have duplicate network folders? Since you point the network drive to that local created folder, after a reboot I'm getting duplicate folders with the same name and content. (see: http://kvandenbrande.brinkster.net/linux/networkdrives.jpg)
Posted by Ryan on 2009-04-24
@Kevin - I haven't experienced that problem specifically, although I do experience some weirdness where I have a "local" and a "mounted" version of the same folder. This hasn't caused me enough trouble to really look into it yet though...
Posted by Awesome on 2009-05-07
"# mount error: can not change directory into mount target... - either you didn't create your /media/my_share directory or specified it wrong in your mount command."

Awesome, thank you.
Posted by BCSL on 2009-05-23
That is fantastic, thank you so much! One more reason not to go back to Windows!
Posted by Tao Starbow on 2009-06-14
Thanks, this really helped me figure out what I was doing wrong.
Posted by jwillar on 2009-07-28
I have successfully using this setup in fstab to auto mount a NAS drive connected to my network.

//192.168.0.9/Volume_1 /media/NAS-Shared cifs credentials=/root/.smbcredentials,iocharset=utf8,file_mode=0777,dir_mode=0777 0 0

I run a current instance of Ubuntu 9.04 but lately the NAS refused to mount. I can still mount it by using "sudo mount -a". I've read it is a result of the network not being up before fstab executes. I've used the '_netdev' w/o success. Do you have any thoughts on how to resolve this problem? Thanks for your help.
Posted by jwillar on 2009-07-28
Success! Read about my solution at http://ubuntuforums.org/showthread.php?p=7683313#post7683313

In summary, I switched back to 'NetworkManager' from to 'Wicd', re-booted and now my NAs drive mounts automatically!
Posted by Ghost-1-0-1 on 2010-01-07
you my friend are a genius....after trying loads of methods, this finally worked...wow i love the linux people and the community....amazin....wish everyone else was this helpful in the world...
Posted by robert on 2010-01-12
nice clear description, thanks.
Posted by Jeremiah on 2010-01-24
I can not get my two 9.10 computers to see each other. When I do the above it responds with an error code 13, but as far as I know the password is correct. How do I check or change it?
Posted by Paul Schloemer on 2010-02-05
Thanks!
Posted by Michi on 2010-03-18
Thank you so much. With your instructions I was finally able to mount my usb-drive. No more ugly switching back to windows
Posted by Ody on 2010-08-17
Tried other guides and got horribly confused, your guide was easy to understand and all my network drives now mapped, thanks heaps, you are awesome!
Posted by tyler on 2010-09-24
Thank you so much for your clear guide. I have been looking for a clear, concise, easy to follow mounting guide and you totally blew the topic out of the water. Thanks again, and keep up the good work.
Posted by Ian Gregory, Sydney. on 2010-10-19
Well done dude.
You covered everything and succinct.
Posted by Riki Pribadi on 2010-10-20
Thanx man, it's a real help!!
Posted by hibi on 2010-12-07
It's really helpfull! Thank you very much...
Posted by pd on 2011-07-24
Thanks. Just managed to mount my freenas7 shares on my ubuntu machine thanks to you. Good work Ryan!
Posted by Larry on 2011-07-31
Nothing was working until I found your article. Now I'm able to mount my ReadyNAS NV+ share for Linux Mint backups via MondoRescue. Thanks, Larry
Posted by abt on 2011-08-26
Brill! Just got my ReadyNAS Duo mounted with Ubuntu 11.04

Thanks for the guide.
Posted by kthomas on 2011-10-02
Best info, Thanks!
Posted by Michele in Orlando on 2012-01-21
Excellent thank you!! just used for GoFlex Home. Only addition I would make is use of \040 for spaces (i.e. GoFlex\040Home for GoFlex Home).
In any case, clear, concise and accurate info !!
Posted by Andy on 2012-02-07
Great article. I am using Ubuntu and trying this with a Windows server fileshare. The fileshare shows up in the directory I create. I can browse just fine. If I want to make any modifications such as rename or create I receive a permission error. I've tried with different users such as the server administrator. All users I've tried have full access to the share and are the owner of the share in Windows Server. Any suggestions?
Posted by Andy on 2012-02-07
Nevermind my comment. I got it working. It was a Windows server admin error. lol! I set the correct permissions for the directory in Windows but not for the share itself. I'm sooooooo glad this works!
Posted by Ramon on 2012-04-25
I got stuck. The mount command works, finds the windows share on the office network, mounts all its folders, but I can't access its contents. The error message I get, when I click any folder: "The folder contents could not be displayed.
Sorry, could not display all the contents of "FOLDER_NAME_1": Not a directory"
Do you have any hints or workaround suggestions you can give me? (ubuntu 11.10)