• Search

  •  

  • Categories

  •  

  • Check out my album!

    Link to my bands website, Digital Penguins, including listening options.
  •  

  •  

  • Meta

  • « | Main | »

    Auto mount NAS or server share to a Raspberry Pi

    By Derek | August 21, 2021

    Recently I had an interesting experience attempting to get a share on my NAS mounted to a folder on my Raspberry Pi.

    Apparently, each step is mostly documented on the internet, but I wasn’t able to find it all in one place, so I thought I’d document it here.

    Here are the steps in detail.

    Step 1

    Change the raspi-config option for networking on boot.

    If you’re running the pi gui, click Menu => Preferences => Raspberry Pi Configuration. On the System tab, ensure that Network at Boot is selected on Wait for Network. Click OK.

    Generally, it’s easier to do steps 2-6 using the terminal, so that’s what I’ve documented.

    Step 2

    Make the NAS pingable by name, aka setup a hosts entry.

    If you haven’t assigned your NAS a static IP address, or more preferably, a DHCP reservation, on your network, you’ll want to do that before you start this step. This prevents your NAS from changing it’s IP address in the future, which DHCP normally allows.

    Check your router documentation or do an internet search on how to reserve a DHCP address for your network gear.

    Save yourself tons of headaches later, use a reserved DHCP address for your NAS or server.

    Add an ip/host entry by editing /etc/hosts. Your EDITOR will be nano, or vim, whatever you are comfortable with.

    $ sudo nano /etc/hosts

    Make a new line after the last one, then add the static or reserved IP of your NAS. If you have multiple NAS devices, you might as well add and test them all right now, each one on a separate line.

    Where I’ve typed <TAB> you’ll actually press the TAB key on your keyboard.

    ip.add.re.ss <TAB> nas-hostname

    In my example, I added the following:

    192.168.1.100 <TAB> drobo

    Save and exit. If you’re using nano, press CTRL+X then the “y” key to save the file.

    $ ping  nas-hostname

    If your ping doesn’t work, check that you have the correct IP address, and that you’ve spelled the nas-hostname correctly.

    Step 3

    Configure a folder to mount your share.

    I used /mnt, so I created my NAS folder there. My folder name is the name of my NAS, which I entered in /etc/hosts.

    I also wanted each “share” from my NAS under the NAS name. If you have multiple shares, create multiple folders here. The only trick is that Linux paths use a standard forward slash “/” instead of a backslash like Windows does.

    $ sudo mkdir -p /mnt/nas-hostname/sharename1
    $ sudo mkdir -p /mnt/nas-hostname/sharename2

    The -p allows the entire path to be created at once.

    In my example, I performed the following:

    $ sudo mkdir -p /mnt/drobo/archives

    Step 4

    Determine your pi username.

    Use the id command to determine your user id and group id.

    $ id

    uid=1000(pi) gid=1000(pi)

    The info returned also includes other text, which we don’t need.

    Generally, if you’ve not customized the user on your Raspberry Pi, your username will be “pi“, and your uid and gid will be 1000, but we need to know for sure.

    Step 5

    Add your mount line to /etc/fstab.

    Use your editor to open the /etc/fstab file.

    You want to be very careful in here, as making a mistake in this file can prevent your Pi from booting.

    $ sudo nano /etc/fstab

    Make a new line after the last one. Everything below goes on a single line, don’t worry about how long it is. Don’t forget to use the TAB key on your keyboard in place of where I’ve typed <TAB>.

    //nas-hostname/sharename <TAB> /mnt/nas-hostname/sharename <TAB> cifs <TAB> username=your-nas-user,password=your-nas-password,uid=your-uid-from-above,gid=your-gid-from-above <TAB> 0 <SPACE> 0

    Remember, all one long line. Let’s break it down.

    The first section, before the first tab, is the network address of your NAS. Again, forward slashes for path entries.

    The second section is the mount point you created on your Pi, forward slashes for path entries.

    The third section is cifs in lowercase.

    I’m kind of assuming that your NAS is running some form of Windows Networking, also called SMB or CIFS.

    CIFS is the protocol, or language, that Linux will talk to your NAS on.

    If you prefer to use a different file sharing protocol, you’ll have to know what it is, and what options to use.

    If you can get to your NAS from a Windows machine, in general use CIFS here.

    The fourth section lists your mount options. Your username, password, uid, and gid go here.

    The fifth section lists the number of times the drive will be allowed to mount before file system checking runs on it. Generally, 0 0 is fine here.

    Double check your work, spelling, format, everything. Make sure you haven’t modified any other lines in this file.

    Save and exit.

    //drobo/archives <TAB> //mnt/drobo/archives <TAB> cifs <TAB> username=pi,password=raspberry,uid=1000,gid=1000 <TAB> 0 0

    Step 6

    Test your mount.

    Let’s be sure and test that it works before we reboot.

    Make sure you aren’t in the directory you made, above. Also, make sure the sharename directory is empty, as the mount will overlay (and hide) any files there.

    Change directory back to your home directory.

    $ cd ~
    $ ls /mnt/nas-hostname/sharename

    No files (except the . and ..) should be listed.

    Run the mount command.

    You’re running the command as root, with sudo, but it will use your uid and gid to assign the mount to you, so that you can both read and write files there. If you left the uid off the mount entry in fstab, the resulting mount would be read-only to your user, and only the root user could change or create files there.

    $ sudo mount -a

    Change to the mounted directory.

    $ cd /mnt/nas-hostname/sharename

    Show the list of files, and make sure what you expect is there.

    $ ls 

    If your NAS share has files on it, you should see them now.

    If the files aren’t there, maybe it didn’t mount? Check your mount line in fstab, or see the Troubleshooting section, below.

    Test that you can create a file.

    $ touch a-test-file-name
    $ ls

    You should see that file created in your list of files.

    If everything worked so far, you should be good to reboot, and ensure the mount gets mounted on boot.

    Note that changing the Wait on Network option may make your Pi take a few more seconds to boot up, but it doesn’t seem to have any ill effect if you don’t have any networking connected, other than, of course, you won’t be able to mount or access your NAS.

    That’s about it. I hope this document helps!

    Troubleshooting

    If you have trouble getting your NAS to mount. Check the system log file.

    $ sudo grep -C4 mount /var/log/syslog

    While describing what you’ll find there is really outside the scope of this article, one of the most common issues has a tendency to be when your NAS or server only supports an older version of CIFS. Sometimes, mount has difficulty negotiating the protocol to talk to your NAS. Look for something like:

    mmm dd hh:mm:ss pi-hostname kernel: [    9.845181] CIFS: No dialect specified on mount. Default has changed to a more secure dialect, SMB2.1 or later (e.g. SMB3.1.1), from CIFS (SMB1). To use the less secure SMB1 dialect to access old servers which do not support SMB3.1.1 (or even SMB3 or SMB2.1) specify vers=1.0 on mount.

    If you see this, but it still mounts, that’s actually good. If you see something like this, and it doesn’t mount, edit your /etc/fstab again, and add:

    ,vers=1.0 

    RIGHT after your uid and gid on the line, like this:

    //drobo/archives //mnt/drobo/archives cifs username=pi,password=raspberry,uid=1000,gid=1000,vers=1.0 0 0

    Using an older protocol can make you less safe, but if you can get your drive mounted, you can always increase the version number until it stops working, then go back one.

    In general, the versions and their compatability are as follows:

    SMB 1 – Windows 2000
    SMB 2 – Windows Server 2008 and Windows Vista SP1
    SMB 2.1 – Windows Server 2008 R2 and Windows 7
    SMB 3.0 – Windows Server 2012 and Windows 8

    (Visited 1,632 times, 1 visits today)

    Topics: Linux, Me, Technology | No Comments »

    Comments

    You must be logged in to post a comment.