How to mount a Windows samba share with FreeBSD automatically

A problem at work the other day was every time our virtual machine cluster went down we had a bunch of virtual FreeBSD boxes that had to be manually connected one by one to our windows share to offload reports. I wanted to make this happen automatically on boot since it was always the same window samba share.

1) You need to add a line to your fstab file located in /etc/fstab

//USERNAME@SERVER/Mount_Folder /blah smbfs rw,noauto 0 0

2) create a file called .nsmbrc located here /root/.nsmbrc
and add your credentials like the example file states in /usr/share/examples/smbfs/dot.nsmbrc (discovered by doing a “man smbfs”)

[SERVER]
addr=x.x.x.x

[SERVER:USERNAME]
password=xxxx

3) finally we need to create a script that runs this on boot, fortunately someone has already done this on the FreeBSD project
the file is located here: http://www.freebsd.org/cgi/cvsweb.cgi/src/contrib/smbfs/examples/smbfs.sh.sample. This file simply parses through your fstab and mounts each one (fairly easy to figure out, especially if you know awk)

you need to put the file in /usr/local/etc/rc.d/ and give the file executable permissions

here is the code

#!/bin/sh
#
# $Id: smbfs.sh.sample,v 1.3 2001/01/13 04:50:36 bp Exp $
#
# Location: /usr/local/etc/rc.d/smbfs.sh
#
# Simple script to mount smbfs file systems at startup.
# It assumes that all mount points described in fstab file and password
# entries listed in /root/.nsmbrc file. See mount_smbfs(8) for details.
#

mount="/sbin/mount -o -N"
umount=/sbin/umount
HOME=/root; export HOME
vols=`awk -- '/^\/.*[[:space:]]+smbfs[[:space:]]+/ { print $2 }' /etc/fstab`

case "$1" in
start)
	echo -n "smbfs: "
	for vol in ${vols}; do
		$mount $vol
		echo -n "$vol "
	done
	;;
stop)
	echo -n "unmounting smbfs mount points: "
	for vol in ${vols}; do
		$umount $vol
		echo -n "$vol "
	done
	;;
*)
	echo "Usage: `basename $0` {start|stop}" >&2
	exit 64
esac

echo "Done"

4) Now when you reboot you should be good to go!

#reboot

Notes:
1) the SERVER name is entirely dependent on the .nsmbrc file, my server did not have its name in DNS so it was confusing me that it wanted a server name for this config file…I was previously using the IP address of my server and it kept confusing it, I am sure there are other ways to fix this by just using the IP but this was a simple fix.

2) I read from a couple places that the username had to be capitalized in the .nsmbrc (just like the server), what I found out that the username for samba shares is not case-sensitive so just go ahead and capitalize it (for me it was ADMINISTRATOR versus what I was trying Administrator)

So basically make sure the SERVER and USERNAME is the .nsmbrc and fstab file match exactly and are both capitalized. Someone comment if they know another way around this.

2 Replies to “How to mount a Windows samba share with FreeBSD automatically”

  1. ee or vi /etc/nsmb.conf

    Add appropriate server username and passwd info. You can hash the passwords with a samba tool, but can’t recall off the top of my head. chmod 700 and chown root the file.

    edit crontab

    @reboot root mount_smbfs //UserInNSMB.confFile@ServerName/share /path/to/mount/point

    If you have some netbios problems you can use

    $reboot root mount_smbfs -I Server.ip.addy //UserInNSMB.confFile@ServerName/share /path/to/mount/point

    Cheers,

Leave a Reply

Your email address will not be published. Required fields are marked *

*