In this tutorial we are going to be setting up a FreeBSD file server using NFS and/or Samba. There are many reasons to use FreeBSD for a file server, a fe examples are its unmatched stability and native ZFS. We will be doing this on a fresh install of FreeBSD 13.


Preliminary:

First of all we need to do a few things to prepare our system. Fully update the system and the ports tree.

Use su to become root since this entire process will require root privileges.

vulnifo@freebsd-test:~ % su
Password:
root@freebsd-test:/home/vulnifo #

Use these commands to update FreeBSD.

# freebsd-update fetch
...
# freebsd-update install
...

If any kernel modules were updated, a reboot will be required. Do this with the following commands.

# freebsd-version -k
13.0-RELEASE-p4
# uname -r
13.0-RELEASE

Since the outputs of the two commands don’t match, we need to reboot.

# shutdown -r now

After the machine reboots we need to install the ports tree. Use the following commands

# portsnap fetch
...
# portsnap extract

Please note: If this is the first time you have run this, it is going to take a while to complete!

With an updated system and ports tree, we can start installing software.

Samba on FreeBSD:

In this section we are going to be installing and configuring samba, which is the unix/linux equivalent of Windows SMB. This software is best used when trying to share files with Microsoft Windows or MacOS, but is easily used to share files with linux OS’s as well.

Installing Samba

First, let’s search the ports tree for samba.

# pkg search samba
p5-Samba-LDAP-0.05_2           Manage a Samba PDC with an LDAP Backend
p5-Samba-SIDhelper-0.0.0_3     Create SIDs based on G/UIDs
samba-nsupdate-9.16.5          nsupdate utility with the GSS-TSIG support
samba412-4.12.15_1             Free SMB/CIFS and AD/DC server and client for Unix
samba413-4.13.8                Free SMB/CIFS and AD/DC server and client for Unix

Now we can install samba.

# pkg install samba413

...

Message from samba413-4.13.8:

--
How to start: http://wiki.samba.org/index.php/Samba4/HOWTO

* Your configuration is: /usr/local/etc/smb4.conf

* All the relevant databases are under: /var/db/samba4

* All the logs are under: /var/log/samba4

* Provisioning script is: /usr/local/bin/samba-tool

For additional documentation check: http://wiki.samba.org/index.php/Samba4

Bug reports should go to the: https://bugzilla.samba.org/

Samba Configuration

Either create a directory to share or note an existing directory that you would like shared. Change *username* to your user.

# mkdir /smbshare
# chown *username* /smbshare

Now lets edit the samba configuration file.

Use whatever text editor you like, I will use nano. (If you don’t have nano simply use the command “pkg install nano”)

# nano /usr/local/etc/smb4.conf

Add this to the file (again, change *username* to your user).

[global]
workgroup          = WORKGROUP
netbios name       = freebsd-test
server string      = samba
security           = user
wins support       = yes
passdb backend tdbsam


# Following is the data directory you want to share.

[smbshare]
  path = /smbshare
  valid users = *username*
  public = no
  writable  = yes
  browsable  = yes
  read only = no
  guest ok = no
  create mask = 0755
  directory mask = 0755

We need to create a specific samba user. This can be the same user as your system user, but still must be done to allow samba to have its own user. Note this user should match with the username used in the previous config file!

 # pdbedit -a -u vulnifo
new password:
retype new password:

Now we need to add samba to the system config and enable it.

# sysrc samba_server_enable=YES
samba_server_enable:  -> YES
# sysrc samba_server_enable
samba_server_enable: YES

Now lets make sure it made it into the rc.conf file, which is the main configuration file for FreeBSD.

# cat /etc/rc.conf

hostname="freebsd-test"
ifconfig_vtnet0="DHCP"
sshd_enable="YES"
ntpdate_enable="YES"
ntpd_enable="YES"
# Set dumpdev to "AUTO" to enable crash dumps, "NO" to disable
dumpdev="AUTO"
zfs_enable="YES"
samba_server_enable="YES"

You can see that the samba server is enabled with the bottom line. Now we can start the samba service.

# service samba_server start
Performing sanity check on Samba configuration: OK
Starting nmbd.
Starting smbd.

Testing Samba

That’s it, now we can make sure it’s working by trying to access the shared folder from another machine on the network. I am going to test it on macOS.

Connect to Server MacOS from samba on FreeBSd
macOS connection screen

A username and password window appears. Be sure to use the username and password you configured.

FreeBSD samba username and password
username and password dialogue

At this point the shared folder should be visible.

d

FreeBSd samba shared folder
Our share is there

Now samba should be all setup on your server and you can utilize it!


NFS on FreeBSD

Now let’s install and configure NFS. NFS stands for Network File System. NFS is best used when sharing folders between Linux machines. Although, the setup/exports file is different on FreeBSD compared to setting up NFS on a Linux OS, the concept is the same.

First, create a folder that you want to be shared, or use an existing folder.

# mkdir /nfsshare

Make sure you the permissions of the folder match the UID and/or GID of the system that is connecting. In my case, I have a user vulnifo with UID 1000 and a group with a GID of 100 on my opensuse machine. Set permissions accordingly to your shared folder. If the permissions/UID/GID don’t match, you will run into a problem of not being able to either mount the folder or write in the folder.

# chown vulnifo:100 /nfsshare
# chmod 775 /nfsshare

Add the following lines to /etc/rc.conf

rcpbind_enable="YES"
nfs_server_enable="YES"
mountd_enable="YES"
mountd_flags="-nr"

Now we need to create a file named exports. The path should be /etc/exports. This file will contain information about what hosts have access to which files.

# nano /etc/exports

Here you will want to add which hosts have access to which files. The format for this is “Folder” “Options” “Hosts”

/nfsshare -maproot=root -alldirs 172.16.1.150

In this example I am sharing /nfsshare and all of its directories with the host 172.16.1.150 (which is an opensuse TW machine).

Start the nfs server.

# service nfsd start
NFSv4 is disabled
Starting rpcbind.
Starting mountd.
Starting nfsd.

If you make any changes to the exports file you must restart mountd so it can read the changes.

# service mountd reload

Testing NFS

Now switching over to my opensuse machine, I can see which shares the FreeBSD machine has. The IP address of my FreeBSD is 172.16.1.32 and the IP address of my opensuse machine is 172.16.1.150.

:~> sudo showmount -e 172.16.1.32
Export list for 172.16.1.32:
/nfsshare 172.16.1.150

I can see that the FreeBSD machine (172.16.1.32) is exporting /nfsshare to address 172.16.1.150 (opensuse). So now I can mount the folder on the opensuse machine using the following command.

:~> sudo mount -t nfs 172.16.1.32:/nfsshare /home/vulnifo/nfs

The first address is the share and the second directory is the mount point on my opensuse machine. Now if I use the df command I can see that it is indeed mounted.

:~> df -h
Filesystem             Size  Used Avail Use% Mounted on
...
...
...
172.16.1.32:/nfsshare   12G  1.7G  9.8G  15% /home/vulnifo/nfs

Let’s make sure our permissions are set correctly by creating a folder in the share.

:~> mkdir /home/vulnifo/nfs/test
:~> ls /home/vulnifo/nfs
test
:~> 

Conclusion

There you have it. You should now have successfully shared folders from FreeBSD using Samba and/or NFS.

If you have any questions or feel you can better this content, please get ahold of us and let us know on social media or our contact page.

As always, follows us @vulnifo for updates and new stories!

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply