# Mount Samba Share automatically on boot



## sammy (Dec 27, 2010)

Hi everyone and Merry Christmas.

I searched the forum for my problem but I stil can't solve it.

Please can someone help me with this problem.

 I would like to mount a samba share on an mac machine from FreeBSD. The following is my entry in the /etc/fstab file.


```
//username@host/share    /mnt/share      smbfs   rw           0       0
```

The above works but I have to enter a password every time freebsd restart.

Then I created a /root/.nsmbrc file with the following content, but it doesn't work for me

```
[host]
password=secret
```


Can someone please tell me first how to get rid of password prompt at boot and how to automate mounting the share.

I don't mind putting password in the /etc/fstab file. But it seems it doesn't work in FreeBSD. I have searched online and seen that people can put password in linux but not in FreeBSD.

I would prefer password in /etc/fstab than on /root/.nsmbrc file.

Any help is very much appreciated.

Thanks
SaMMY


----------



## SirDice (Dec 27, 2010)

sammy said:
			
		

> Then I created a /root/.nsmbrc file with the following content, but it doesn't work for me
> [host]
> password=secret


Both the hostname and the username need to be in uppercase.


----------



## sammy (Dec 27, 2010)

Hi SirDice

Thanks for your advice.

I made the username and hostname uppercase as follow, still does not  work for me.

```
[HOSTNAME]
password=mysecret
```


I have also added a noauto to the /etc/fstab options so it won't prompt me for a password after a reboot.


```
//username@host/share /mnt/share smbfs rw,noauto 0 0
```

By adding the credentials in the /root/.nsmbrc file, does not it mean that I have to login as a root each time so it will mount the samba share?

Is it possible to add password in the /etc/fstab file itself?

Thanks


----------



## vermaden (Dec 27, 2010)

@sammy

From man mount_smbfs page:

```
-N      Do not ask for a password.  At run time, mount_smbfs reads the
             ~/.nsmbrc file for additional configuration parameters and a
             password.  If no password is found, mount_smbfs prompts for it.
```

So Your /etc/fstab file needs to look for example like that:


```
client% cat /etc/fstab | grep -B 1 smbserver
#DEV                     #MOUNT   #FS     #OPTS   #PASS/DUMP
//user@smbserver/share   /share   smbfs   rw,-N   0 0
```

... Your client ~/.nsmbrc file like that:

```
client% cat ~/.nsmbrc
[SMBSERVER]
password=SeC4eT
```

... and You need to create an user with pdbedit command:

```
smbserver# pdbedit -a -u user
```


----------



## jnbek (Dec 27, 2010)

Seems to me you can do in /etc/fstab:


```
//user:password@host/share /local/share smbfs rw 0 0
```

to have it mount on boot. It's been quite a while since I've mounted Windows shares from BSD, but I'm pretty sure that's how I did it.


----------



## sammy (Dec 27, 2010)

Adding the -N switch, made freebsd go into single user mode where I had to remove the -N option and rebooted again.


```
//username@host/share /mnt/share smbfs rw,-N 0 0
```

I have already tried the jnbek advice and simply adding a password to /etc/fstab won't work. I still get prompted for a password.


```
//username:secret@host/share /mnt/share smbfs rw,noauto 0 0
```

I am using FreeBSD 8.1. maybe in the older versions you could add password or -N switch but not in this version.

I have used the -N switch before with FreeBSD 7 and had no problem with it. but in FreeBSD 8.1 the system goes into single user mode.

As for adding a user with pdbedit. I already have a user name in the samba server (mac) because I can connect to it by manually entering a password at boot.


----------



## sammy (Dec 27, 2010)

If I remove the rw option and just leave the -N switch in /etc/fstab file, the system boots fine but still the share is not mounted.


```
//username@host/share /mnt/share smbfs rw,-N 0 0
```

changed to 


```
//username@host/share /mnt/share smbfs -N 0 0
```


----------



## DutchDaemon (Dec 28, 2010)

Sammy, format your posts.


----------



## sammy (Dec 28, 2010)

Making some progress

The .nsmbrc file is correct and working because now when I issue the following command manually, I can mount the share without being prompted for password



```
mount -t smbfs //username@host/share /mnt/share
```

The above command mount the share without asking for password. because it is now reading the password from /root/.nsmbrc file.



But I would like to do it in /etc/fstab.

With the following entry in fstab, the system goes into single user mode.

```
//username@host/share /mnt/share smbfs rw,-N 0 0
```


Wheras the following entry will prompt me for a password at boot

```
//username@host/share /mnt/share smbfs rw 0 0
```

And finally the following entry wont mount anything

```
//username@host/share /mnt/share smbfs rw,noauto 0 0
```


----------



## sammy (Dec 28, 2010)

Thanks DutchDaemon

I will *format* my posts from now on


----------



## sammy (Dec 29, 2010)

Problem Solved

I had to download the following script from FreeBSD website and put it into /usr/local/etc/rc.d/ folder and gave it executable permission.

http://www.freebsd.org/cgi/cvsweb.cgi/src/contrib/smbfs/examples/smbfs.sh.sample

The file need to be renamed to smbfs.sh

Thanks everyone for you help

I love FreeBSD.

SaMMY


----------



## jmn_nsl (May 4, 2011)

Hello dear members,

I use this line in /etc/fstab and use ~/.nsmbrc for credential.

```
//USER@SERVER/SHARE /path/to/mount smbfs rw,-N,-I172.16.1.1 0 0
```

This link helped me in this problem: http://www.mede8erforum.com/index.php?topic=1783.0

Bye.


----------



## norswap (Feb 28, 2012)

The script proposed by sammy is not necessary, it can work with fstab only.

The key is that when fstab is read, the system (FreeBSD-9.0 RELEASE) doesn't look at the files ~/.nsmbrc and /root/.nsmbrc. It looks at the /etc/nsmb.conf and /.nsmbrc files however. Put your password config in one of those files and the system will find the password.

As reminder, the entry in fstab should look like:


```
//user@NETBIOS/share /mnt/something smbfs rw,late,-N,-I<ip>
```

user: username
NETBIOS: windows netbios server name (use "nbtstat -n" on Windows to find it)
user: windows user name
share: the windows share name
<ip>: replace with the IP of your Windows computer, do *not* put a space between "-I" and the IP.

The entry in /etc/.nsmb or /.nsmbrc should look like:


```
[NETBIOS]
password=your-password
```

(It is also possible to have the IP in there, I don't know the specifics.)


----------



## pcfxer (Apr 25, 2013)

norswap said:
			
		

> The script proposed by sammy is not necessary, it can work with fstab only.
> 
> The key is that when fstab is read, the system (FreeBSD-9.0 RELEASE) doesn't look at the files ~/.nsmbrc and /root/.nsmbrc. It looks at the /etc/nsmb.conf and /.nsmbrc files however. Put your password config in one of those files and the system will find the password.
> 
> ...



Worked my first try. 

Thank you sir.


----------



## break19 (Apr 25, 2013)

Ok.. To keep this going, I've got an event in my calendar, to come back in about a year and post to this 3 year old thread, like both of the people above me did.


----------



## jnbek (Apr 26, 2013)

*D*id someone start a bandwagon to jump on?!??!


----------



## oiram (Sep 28, 2016)

Thanks for the thread, helped me get my one sorted out.


----------



## oiram (Sep 28, 2016)

norswap said:


> The entry in /etc/.nsmb or /.nsmbrc should look like:



there is small typo, it shall read as below

The entry in /etc/nsmb.conf or /.nsmbrc should look like:


I have my configuration based on entries from /etc/nsmb.conf only and it works fine. Note that you can add multiple passwords (for different users there), like below:


```
[NAS-01:user1]
password=password1

[NAS-01:user2]
password=password2
```

and then you can reference these accounts in /etc/fstab


----------



## manilaboy1vic (Feb 1, 2021)

norswap said:


> The script proposed by sammy is not necessary, it can work with fstab only.
> 
> The key is that when fstab is read, the system (FreeBSD-9.0 RELEASE) doesn't look at the files ~/.nsmbrc and /root/.nsmbrc. It looks at the /etc/nsmb.conf and /.nsmbrc files however. Put your password config in one of those files and the system will find the password.
> 
> ...


This worked for me. Thank you.  I did have ~/.nsmbrc and on a reboot my NAS was not being mounted.

I then edited /etc/nsmb.conf and rebooted.  NAS is mounted.


----------

