A guide on how to build your very own NAS (Network Attached Storage)

Daniel Wilkinson
7 min readApr 26, 2021

--

So… I hear you, why why why are you building one when you can get one at a reasonable price? 💭 Well, the challenge but also the practical use of a few spare drives and a heavy passion for photography, not only that but since lockdown started the price of those things has skyrocketed to the point that buying the equipment to do this yourself is a lot easier and more maintainable.

So, let's create our VERY own!

Wait, Dan… What prerequisites do I need? Let me tell you:

  • 1 Mouse, Keyboard & Monitor
  • 1 Spare Hard Disk Drive (HDD) or Solid State Drive (SSD)
  • 1 Raspberry Pie (I used the Model B 😎)
  • 1 HDMI Cable
  • 30 minutes of time set aside.

You’re going to need a setup. A monitor to be able to see what the heck is going on when you’re running all of these whacky commands, a keyboard to smash, and a mouse too well… You do very little with those on your PI but I copied and pasted a few commands from the web — so I guess they’re always handy. It is best practice to learn and write these yourself though, definitely do as I say and not as I do! 😆

If you have an HDMI you can jimmy that into your PI and shove the other side into your monitor ensuring the thing is turned on, mine had a switch on the AC Adaptor that was turned off. It took 30 seconds of investigation to work out why the heck it wasn’t coming on… but once it did I noticed the Model B has an LED indicator to prove it’s working. You’ll find that LED right near the display port.

LED showing the PI is on

Once you’ve got the PI powered on slot in your drive and run the following command in a terminal window: df -hthis puppy is just to make sure that we can see your dive has mounted. This means “recognized” by the PI. If it doesn’t show, there are loads of online supporting documentation to troubleshoot. Give this a good google. You should see something like below as a result of this command:

What we’re looking for here are drives. Usually, they appear under ‘Device’ or ‘dev’ for short, ah yes… you see it? /dev/sda Let’s chat about these file paths so we have an understanding.

Typically you’d find all your mounted drives cycling through the alphabet so for example if we had two drives the paths would be mounted at /dev/sda & ‘ /dev/sdbthen on through to /sdcyou get the point the last letter cycles.

Anyhow; in this picture, I’ve previously mapped this drive to the location at the end of that same line ‘/2TB’ that's my HDD baby, a full 2TB of pictures ready to be Instagrammed. Would have a shameless plug here but decided against it. 😉

Back on with the show; Let's begin to clean our drive using the following command sudo fdisk /dev/sda . You’re now in what I like to call DISK EDIT MODE! Warning — We are about to delete the content of the drive. You can choose to skip this next part.

The o the option will nuke your partition table, destroying all of your partition definitions. This can be useful if it's corrupted or you just want to start fresh with your disk. Enter o when prompted for a command.

Next, click n for partition type then p for primary and 1 for to finally create one new primary partition on your drive. Hit enter until prompted for another command.

Finally, hit w for the final part of EDITOR MODE and that will sync to your disk.

Now you have a fresh disk however it has to be in one of the following formats;

  • NTFS (I chose this as I had data on mine.)
  • EXT4 (Recommended by everyone — I have no idea why — actually a quick google tells me it’s because it’s the default Linux format, well gosh darn use this.)
  • exFAT.

Use the following command to format the disk into your choice of format;sudo mkfs -t ext4 /dev/sda1 What you’re seeing here is that we have created a disk with one big partition on it. What does that mean? We can use the drive to store data soon. Woop!

A common problem however is that a drive is not auto-mounted when we restart our PI. If your cat or child gets in at your cables — nightmare! Oh no. You’d have to remap the drives. Let's absolutely defend against those vicious attacks.

So, how do we do this? We’re gonna grab the id of your drive, we’re then gonna edit a file and ensure we’ve added the ‘auto’ command. Let's do it!

Firstly, type the following command? ls -la /dev/disk/by-uuid/ and let's grab our id. We’re looking for the one with ‘sda1’ in the name. Ah yes, he’s present. Grab that beautiful long random blue number.

Showing ids of drives

Side note; ‘blue number’ always reminds me of this scene: youtube.com/watch?v=1wxmjARMwgc

Anyhooo, we need to map a folder to our mounted drive lets create that folder; as you’ve seen I created mine /2TB to do this type sudo mkdir /2TB you can use whatever directory name you like.

Next, map it using sudo vi /etc/fstab so what we’re gonna click o to start a new line and then type UUID=YOURDRIVENUMBER /2TB ntfs auto 0 0 . Once you’ve typed that out save the file and return to mount the drive by typing sudo mount -a which means to run through each drive and ensure everything from that table is mounted correctly.

Adding auto to drive and mapping to /2TB folder

To double-check you can type DF -H again to check if your drive is mapped to our new directory /2TB or whatever you called yours.

Now you have a shared folder, let's create a space for you! Just you! sudo mkdir dan Here I am creating my space, I called my folder dan.

We’re then gonna create permissions of admin and give them to you for your space!

Head to the terminal and type sudo adduser danyou can name your user as you see fit but I have created mine and named his dan. Give him a full name and then click enter to get through the next few options. They’re pretty… Optional. and then type y when prompted to finish adding the user. Next type sudo chown dan:dan dan… Hmm we may need to break this down…

So — yes, this is me creating my dan vault (https://www.youtube.com/watch?v=Bjl6E9X0XA8) for me and only me to access. Sudo chown means to cho > change and own > owner. We are saying we’d like the directory dan to be own by the group and person more specifically dan. Confused? Yeah, it’s pretty confusing. Think of it like this ‘admin I’d like to change the owner of the dan directory to the group to dan.’ Onward we go!

Now you will need to install Samba which is a third-party library for sharing files. To do so run this command; sudo apt-get install sambaand once you’ve completed this run the command sudo vi /etc/samba/smb.confto edit the sharing options. We’re gonna put in here the name of your space right at the bottom of this file and it should look something like below.

[dan]
Comment = Dans space
Path = /2TB/dan
Browseable = yes
Writeable = Yes
read only = no
valid users = dan

You’ll need to remove some default ‘home’ options samba installed. Find the ‘Homes’ section and comment it all out using the # on each line.

Save the file and now it’s time to assign your user to the file that's been shared run the command sudo smbpasswd -a danand enter the same password you created for the user.

Right, this is it. Restart the Samba service using the command sudo service smbd restart . This command will apply all changes and reboot the service so the changes take effect.

WE DID IT. Let's find it in windows using map network drive in the file explorer. You may need to enter your username and password that you just set for Samba.

Daniels Directory

Boom we made it. Begin transferring your data and you can use this whenever you’re on your local network. If you would like to see how to add more drives or expose this shared directory to the outside world let me know in the comments below.

--

--

Daniel Wilkinson

Daniel Wilkinson is my name and Software Engineering is my game. I'm also a coffee Enthusiast but I couldn't find a way to make that rhyme. 😂