Turn your Raspberry Pi into a NAS with Samba

Setup a Raspberry Pi based NAS using Samba. The below instructions were based on this article. The solution presented below is the easiest way I’ve found to set up network attached storage using a Raspberry Pi; however, if your primary purpose of your Pi is a NAS, you’re probably be better off installing OpenMediaVault or something similar as those solutions will be more resiliant.
Prepare the hard drive
You can format any USB drive to act as your network accessible storage location (you could also use drives connected via SATA if you had a Raspberry Pi hat that provided that connector)
Format the drive
Connect the drive to your pi and run the following command to figure out its identifier
sudo fdisk -l
Note the identifier, ex /dev/sda1
Now use the following commands to format the drive with the ext4
filesystem. You could format to another filesystem (like fat
for example), but ext4
is native to Linux and will be faster than non-native filesystems.
umount /dev/sda1
sudo mkfs -t ext4 /dev/sda1
You’ll notice that a freshly formatted ext4 drive will contain a folder called “lost+found” this is used by the OS for ext4 filesystem and cannot be removed.
Mount the drive
Configure the drive to automatically mount when it is plugged into the Pi.
1. Create a mount directory
The mount directory is where the files from the drive will show up when it mounts. Name the mount directory whatever you want, and create it whereever you want. Ex:
mkdir /media/MY_DRIVE
2. Get the UUID of the drive
sudo blkid
Your UUID might be something like this: d2283f17-8870-490e-8412-db7af4419999
3. Edit the fstab config file
Open the config file
sudo nano /etc/fstab
Add an entry to fstab to tell the system to automatically mount the drive to our chosen mount path when the system boots
UUID=MYUUID MOUNTPOINT FSTYPE defaults,auto,users,rw,nofail 0 0
- Replace MYUUID with the UUID found in step 2 (ex.
d2283f17-8870-490e-8412-db7af4419999
) - Replace MOUNTPOINT with a location where you want the drive to be accessible from step 1 (ex.
/media/MY_DRIVE
) - Replace FSTYPE with the filesystem type of the drive found in step 2 (ex.
ext4
). To have the system automatically assign the filesystem enterauto
Additional details about fstab here
4. Mount the drive
To mount the drive either reboot the system (sudo reboot
), or manually mount the drive by it’s identifier (in the above example: /dev/sda1
)
mount /dev/sda1
You can also mount all drives using the mount -a
(this is what the Pi runs at startup)
5. Verify
You can now go to the mount location of your drive (cd /media/MY_DRIVE
) and see files that are stored on the drive
Give users access to the NAS
To make things easier for permissions purposes, I recommend creating a unix group which has ownership, and read/write permissions over the mounted drive. When you want to give a person access to the NAS, you simply create a unix user on the Raspberry Pi, and add that user to the NAS users unix group. With this workflow permissions for reading/writing the NAS don’t get tangled up, or used for any other purposes.
Create a unix usergroup for NAS users
This step is optional, if you want to just have one username/password for the NAS that you share with others instead of making everyone their own username/password, then you can skip to the next section on creating users.
Create a group (for example nas_users
)
sudo groupadd nas_users
Give our group ownership of the mounted location and give our group read, write and execute permissions to the mount location
sudo chown root:nas_users /media/MY_DRIVE
sudo chmod g+wrx /media/MY_DRIVE
Create a user
Follow these steps anytime you’d like to allow a new user access to the NAS.
Create a new user (ex: jack
), and give that new user a password
sudo adduser jack
sudo passwd jacklot
Optional: If you’re using the group idea from above, add the new user to the group we just created
sudo usermod -a -G nas_users jack
Verify, by typing groups
, what groups the current user is in. Type the username of any user, ie. groups username
, to check a specific user. If you don’t see the new group in the lists, then you may have to reload by logging in as that user su username
Give your user a Samba password
Next we have to create a password for the user we just created so they can see the share from other machines on the network. Note that this password is Samba specific and different from the user’s unix password (though you could make both those passwords the same if you’d like).
sudo smbpasswd -a jack
How to install Samba
Samba is an open-source implementation of Windows’ SMB/CIFS file sharing protocol which is widely used and compatible with the vast majority of systems that may want to interact with the NAS.
sudo apt-get install samba samba-common-bin
The installer will ask you if you want to modify smb.conf to use WINS settings from DHCP. Choose Yes.
Configure Samba
Add a new share
Open the Samba config file
sudo nano /etc/samba/smb.conf
Add the following lines to the end of the file to share the drive across the network
#
# My personal shares
#
[PiNAS]
path = /media/MY_DRIVE/
valid users = +nas_users
force group = nas_users
available = yes
writeable = yes
browsable = yes
public = no
create mask = 0660
directory mask = 0770
- The first line,
[PiNAS]
, specifies the name of your share. In this case I choose to name my sharePiNas
, but you can name it whatever you want. path
: should be set to the mount location of the drivevalid users
: space-separated list of users allowed to login to the service OR a unix groupname with the+
to indicate any member of that unix group has accessforce group
: specifies the group of your choosing as the group owner of files created on the shareavailable
: whether this share is available for usebrowsable
: whether the share is seen in the list of available shares on the networkwriteable
: ensures users can write to the filespublic
: if set toyes
, no password is required to connect to the sharecreate mask
: the chmod permissions code for writing files (0660
means the user and group can read/write, but not execute) documentation heredirectory mask
: the chmod permissions code for writing folders (0770
means the user and group can read/write and delete directories, but not execute) documentation here
Additional documentation on Samba config options
Additional information about group/user access to Samba shares
Note: You can create smb.conf entries for any path you want to share. These paths will be made available across your network when you restart Samba.
Verify the config file
Run the following to test the Samba configuration file. If you don’t see Loaded services file OK
, go back and check that you’ve completed the previous step correctly
testparm
Start (or restart) Samba
After making changes to the config file, restart the Samba daemon. You’ll want to do this after making any changes to the config file.
sudo service smbd restart
Connect to the new share
Connect to a Samba share on MacOs
- Go to Finder
- Dropdown the
Go
menu and selectConnect to a server
(or simplycommand
+k
) - Type
smb://pi1
(wherepi1
is the hostname, or IP address, of your raspberry pi. Optionally specify the share to connect to usingsmb://pi1/myshare
) - Click
Connect
- Login with the appropriate credentials
- Optional but recommended: Add the shared drive to your Finder sidebar. If you don’t see the drive, open up
Finder > Preferences
, then check the boxShow these items on teh desktop: Connected servers
. Now you can grab the drive from the desktop into the Finder sidebar
Note: When you connect to your raspberry pi, you’ll always see an additional shared folder named after the username you provided.