Mounting Windows SMB File Shares Using Cifs
Posted by ryan in linux
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.