Pages

Tuesday, 29 May 2012

Raspberry Pi - Connect NAS / windows share automatically at start-up


Anyway, as good (or quite frankly brilliant) as the Pi is, the capacity of an SD card isn't going to go far; so connecting to my trusty NAS drive (e.g. shared folder) was going to be a neccessity and as I was going to be using it a lot, automatically mounting it when the Pi started was also needed.


I was going to be need Samba to connect to my NAS, which can be installed using:

Install/Update Samba
(using Debian Squeeze)
Smbfs is required and while smbclient is not, its a useful tool that I thought I might need in the future.

sudo apt-get install smbfs smbclient

(using Raspian)
Samba is already installed and smbfs has been depreciated in wheezy.

(using Raspbmc)
Samba is already installed but I needed to installed cifs-utils in order to mount my NAS drive.


sudo apt-get install cifs-utils


Make a directory to mount your NAS too
I choose to put it in the pi user's home directory using the structure /home/pi/server/share:

cd /home/pi
mkdir myNAS
cd myNAS
mkdir myShare

Edit fstab file
I needed to edit the fstab file to mount the NAS drive at startup:

sudo nano /etc/fstab

I added the following line to the bottom of the file:

//myNAS/myShare /home/pi/myNAS/myShare cifs username=your_username,password=your_password,workgroup=your_workgroup,vers=1.0,users,auto,user_xattr 0 0

And saved the changes.

WARNING - this will mean your username & password is stored in plain text viewable to all on the device, if this is going to be a problem you can use a credentials file, see http://anothersysadmin.wordpress.com/2007/12/17/howto-mount-samba-shares-in-fstab-using-a-credential-file/

Test
I tested the change by mounting the NAS drive using the command:

sudo mount -a

I then navigated to the mount directory and successfully retreived a directory listing from my NAS:

cd /home/pi/MyNAS/MyShare
ls

Success!

Reboot
To be double sure, reboot and make sure your NAS is connected:

sudo shutdown -r now

59 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi there,
    first of all thanks for your tutorials...they are really useful!
    I'm trying to mount my WD MYBOOKWORLD on the Raspberry, but I'm facing some issues...
    My NAS is on 192.168.1.101, so the line added on /etc/fstab is:
    //192.168.1.101/PUBLIC/backup_raspberry /mnt/NAS username=USERNAME,password=PASSWORD,workgroup=WORKGROUP,users,auto,user_xattr 0 0

    Then I try to mount it, but I'm having this error:
    mount: unknown filesystem type 'user_xattr'

    Any suggestion on that?
    Regards

    ReplyDelete
    Replies
    1. I realise this is a bit late for your purposes, but just in case others come here with the same problem. Assuming the server and share name are correct, the correct line in /etc/fstab is
      \t \t cifs (assuming it was formatted as FAT32) \tusername=USERNAME,password=PASSWORD,workgroup=WORKGROUP,users,auto,user_xattr \t 0 0
      where \t=tab.

      Delete
  3. Thank you for a great tutorial.

    Only thing I want to add, on mine I didn't have write permissions for the shared folder/files. I added....in the fstab statement

    file_mode=0777,dir_mode=0777,

    Regards, Sofaman

    ReplyDelete
    Replies
    1. Hey Nigel I tried this and it comes with an error,
      Option 'file_mode0777' requires a numerical argument
      Any ideas about how to fix this?
      Please advise

      Delete
  4. Hi, thanks for the tutorial. When I type sudo mount -a it asks for my password but then after a pause I'm told "Mount point 0 does not exist". I'm not really sure what the mount point refers to so I'm bereft of ideas to fix it. Can you help?

    Thanks

    ReplyDelete
    Replies
    1. Its not an error ive come across before. Its odd that its asking you for your password, it shouldnt if its in the fstab correctly. Id check there first and make sure the username and password are correct.

      Delete
    2. Any solution to this? I get the same password prompt and then same error but the file system actually mounts, it's just read only... :( Any help?

      Delete
    3. I'm getting the same error, but without it asking for my password. Any advice?

      Delete
    4. It suggests you have got an error in your /etc/fstab file, like its badly formatted, make sure there is no odd characters or line breaks in it and the that mount point you create i.e. the directory exists, remember its case sensitive too. If you send me the contents of the fstab file im happy to have a look.

      Delete
    5. Change
      username=your_username,password=your_password,workgroup=your_workgroup,users,auto,user_xattr 0 0
      TO (change username to user and add uid and sec)
      user=your_username,password=your_password etc..

      Delete
  5. I had the prompting for the password issue. I wound up adding the following to 1 line:
    //myNAS/myShare /home/pi/myNAS/myShare cifs username=your_username,password=your_password,workgroup=your_workgroup,users,auto,user_xattr 0 0

    ReplyDelete
  6. I'm pretty much a noob at command line, but I got this to work. You just have to remember to change //myNAS/myShare, your_username and your_password in the fstab to what those values for the device are. When you're done your fstab additions should look something like this:

    //192.168.xxx.xxx/myLibrary /home/pi/myNAS/myShare cifs username=JoeBob,password=12345,workgroup=Workgroup,users,auto,user_xattr 0 0

    ReplyDelete
  7. Thanks for the help man. I'm happily streaming music from my Nas through my Pi! Cheers!

    ReplyDelete
  8. Hi! It's a good guide. I try it nce, and it works... But I need to re-install my raspbmc, and now it doesn't work...
    i get this message:
    mount error(13): Permission denied
    Refer to the mount.cifs(8) manual page (e.g. man mount.cifs)

    in my fstab:

    //192.168.16.40/public/ /media/NAGYFEHER(this is the name of NAS) cifs username=my_username,password=my_password,workgroup=MSHOME,users,auto,user_xattr 0 0

    what's the problem?
    I try it, with "admin" password too...

    ReplyDelete
  9. my nas does not need a password to login, how should it look in the fstab file?

    ReplyDelete
    Replies
    1. Instead of username=user,password=pass you can substitute it all with guest.
      e.g.
      //192.168.16.40/public/ /media/NasName cifs guest,workgroup=MSHOME,users,auto,user_xattr 0 0

      Delete
  10. Hi,

    I am able to mount my Windows share and set permissions using:

    sudo mount -t cifs -o username=username,password=password,rw,file_mode=0777,dir_mode=0777 //192.168.x.x/share mountpoint

    I need the permissions to be set, otherwise I'm unable to write to the share. Plus, adding this line to /etc/fstab mounts the share at boot, with read/write permissions:

    //192.168.x.x/share home/pi/mountpoint cifs username=username,password=password,rw,file_mode=0777,dir_mode=0777 0 0

    Fill in your own values and you should be OK. Be sure that access permissions are set correctly on the Windows share, including all sub-folders that you wish to share.

    ReplyDelete
  11. had the same issue with the password request. then i stumbled upon Martins advice regarding odd characters or line breaks... I had a line break after the cifs in the fstab like it is shown on the webpage. I removed the line break and everything works fine now. Just thought others might have the issue due to the same mistake. Thanks for the tutorial.

    ReplyDelete
  12. Hi Martin,

    I followed you guide and everything works except after reboot the NAS does not connect. I have to enter the 'sudo mount -a' and it starts to work again. Any idea on how I can automate this?
    Cheers

    ReplyDelete
    Replies
    1. Now thats wierd! If it mounts using sudo mount -a, I have no idea why it doesnt mount at boot, as far as Im aware its basically running the same command. Sorry

      Delete
    2. i have the same problem here?

      Delete
    3. i have the same problem here?

      Delete
    4. This comment has been removed by the author.

      Delete
    5. This comment has been removed by a blog administrator.

      Delete
    6. In order to be sure that the network is ready during the boot, there is an option Wait for Network at Boot in raspi-config.

      - sudo raspi-config
      * 3. Boot Options
      * B2 Wait for Network at Boot

      Found that here: https://raspberrypi.stackexchange.com/questions/34444/cant-get-a-cifs-network-drive-to-mount-on-boot (Not top answer as of now but probably should be)

      Delete
  13. Mine had the "Mount point 0 does not exist" error and I tried rebooting and now it is stuck at "Configuring Network Interface". Any thoughts?

    ReplyDelete
    Replies
    1. Sorry, no none at all. Its not a problem I have seen before.

      Delete
  14. This comment has been removed by the author.

    ReplyDelete
  15. This will make it mount at boot!

    http://raspberrypihelp.net/tutorials/12-mount-a-samba-share-on-raspberry-pi

    ReplyDelete
    Replies
    1. If you add your connection correctly to fstab it will mount the share on boot, you dont need to add it to rc.local

      Delete
  16. When I mount using "sudo mount -a" it works, but after reboot it does not mount automatically.

    ReplyDelete
    Replies
    1. strange, there's no reason why it shouldn't unless for some reason at boot it cant connect to the drive.

      Delete
    2. Found the answer on a forum. If the share is not password protected, it will not be mounted automatically. :)

      Delete
    3. At the boot time the network is not started yet. There is many solutions. Simple one mostly helps, go to PI Menu-preferences-raspberry Pi configuration-System Tab and check "Network at boot"

      Delete
    4. write this to your /etc/rc.local file before the last line:

      sudo mount -a

      additionally, in your /etc/fstab file use this at the end of your mounting path:

      user_xattr 0 2

      (instead of user_xattr 0 0)

      this will check if the mount has been sucessful and if not it'll retry it. It happens when the mount is attempted before the network is operational.

      Delete
    5. write this to your /etc/rc.local file before the last line:

      sudo mount -a

      additionally, in your /etc/fstab file use this at the end of your mounting path:

      user_xattr 0 2

      (instead of user_xattr 0 0)

      this will check if the mount has been sucessful and if not it'll retry it. It happens when the mount is attempted before the network is operational.

      Delete
  17. This comment has been removed by the author.

    ReplyDelete
  18. I get mount error(13): permission denied
    Refer to the mount.cifs(8) manual page
    Mount: mount point 0 does not exist

    Can anyone help?

    ReplyDelete
    Replies
    1. If it helps, I had the same issue and realized that I had entered the username line on a new line. Instead it needs to be on the same line with the path. Hope it helps.

      Delete
  19. my issue was it keep on asking me for the root password when mounting the nas drive. where i have already put in a administrator username and password...

    ReplyDelete
  20. I have the same issue with root password. I don't understand what that is. I also have another thing to add. I don't have password on my network drive so I left the field empty, is that ok or do I need to specify something? Basically it looks like this: password=,workgroup....

    ReplyDelete
  21. After searching a while it seems that my raspbian has root password but since I can't use some commands with the excuse that "only root can use "--options" option". Does this mean I have to get that password or am I doing something wrong?

    ReplyDelete
  22. Very n00b question, but how do I mount a NAS share that has a space in the name? I added a few shares in fstab. All work fine except:

    //NAS/TV Series /home/pi/NAS/TV_Series cifs username=xxxx,password=xxxx,workgroup=xxxx,users,auto,user_xattr 0 0

    This line gives an error telling me:
    'mount: mount point Series does not exist'

    I cannot easily change the name of the NAS folder, since the folder is tied in with other systems.

    ReplyDelete
    Replies
    1. Hey there, did you figure this out. I am also a first time linux user so just trying to teach myself as I go.

      Delete
    2. Space chars in fstab are represented by \040
      eg MY PATH should be MY\040PATH

      Delete
  23. Is there any way to only give writing permission to the mounted folder, without the permission of erasing existing files on the mounted folder?

    For example I want to be able to write to my NAS from my RPi, but I don't want to have the permission to erase these files on the NAS using my RPi afterwards.

    ReplyDelete
  24. Have been trying this for an hour and tried just about all the variations given above.
    My problem is that when I issue the 'mount' command all I get is the error: 'Couldn't chdir to /home/pi/MyNAS....'

    The folder is there and I can 'cd' into it but a manual 'chdir' command returns 'bash: chdir: command not found'

    Huh! Explanation anyone? Please ...!

    ReplyDelete
    Replies
    1. Does it give a reason why it couldnt chdir to /home/pi/MyNAS? Permission denied, directory not found maybe?

      fyi - chdir isnt a command its just shortcut for change directory.

      Delete
    2. If the directory is not found, I would check capitals, files and directories are case sensitive in Linux.

      Delete
  25. Hello,

    I get the following error message:

    mount: special device ./dev/mmcblk0p1 does not exist

    Can someone help me please?

    ReplyDelete
  26. Here's the content of my fstab file:
    roc /proc proc defaults 0 0
    /dev/mmcblk0p5 /boot vfat defaults 0 2
    /dev/mmcblk0p6 / ext4 defaults,noatime 0 1
    # a swapfile is not a swap partition, so no using swapon|off from here on, use $
    //myNAS/myShare /home/myNAS/myShare cifs
    user=username,password=password,workgroup=WORKGROUP,users,auto,user_xattr 0 0


    Getting the following output...
    sudo mount -a
    Password:
    mount error(115): Operation now in progress
    Refer to the mount.cifs(8) manual page (e.g. man mount.cifs)
    mount: mount point 0 does not exist

    ReplyDelete
    Replies
    1. You have to replace the //myNAS/myShare with the location of your NAS drive. You may also need to give a username and password?

      Delete
  27. Hi Martin, thanks for the great guide.
    I have a problem, I can mount a share manually with:
    mount -t cifs //serverIP/sharename /home/pi/myNAS/myShare -o vers=1.0,username=XXXX,password=XXXX,workgroup=XXXX

    But I don't know how the essential -o parameter would look in fstab.

    ReplyDelete
  28. I have Raspian Stretch installed and the above always worked, until today. It appears there has been a change, you now need to specify vers=1.0

    The fstab reference entry should be:

    //myNAS/myShare /home/pi/myNAS/myShare cifs username=your_username,password=your_password,workgroup=your_workgroup,vers=1.0,users,auto,user_xattr 0 0

    ReplyDelete
  29. Thank you!!!

    I am a newbie and I was able to actually make sense of your post, you simplified it enough I could understand. It also helped me realize the big mistake I had been making. One of my home directories was called RetroPie, but I addressed it as retropie, by not using the appropriate capitalization I was getting an invalid or no directory error. I am from the old DOS days where directory capitalization did not matter. Slowly but surely I am picking some Kali.

    ReplyDelete

Note: only a member of this blog may post a comment.