How to Turn a Raspberry Pi Into a NAS for Whole-Home File Sharing
Home › Forums › Projects › Raspberry Projects › How to Turn a Raspberry Pi Into a NAS for Whole-Home File Sharing
Tagged: raspberry PI5
- This topic has 0 replies, 1 voice, and was last updated 3 months, 1 week ago by
Sinan.
- AuthorPosts
- January 3, 2025 at 4:54 pm #1287
Sinan
Keymaster1. Install an Operating System
There are special operating systems like Openmediavault that turn your Pi into a NAS, but for a beginner setup, I actually recommend regular old Raspbian—it’s flexible, easy to use, and good enough for sharing a few files over the network. Start by installing Raspbian with the recommended software as described in our beginner’s guide.
I recommend hooking up your Pi to your network via Ethernet for fast file transfer, but Wi-Fi will do in a pinch. Once you have booted up Raspbian for the first time, designated a new password, and downloaded all your updates, connect your drive to one of the Pi’s USB ports. The drive will show up on the desktop, but we will be doing most of our work in the Terminal. (If you prefer, you can SSH into your Pi and perform these commands from another PC.)
2. Unmount Your Drive
Before continuing, we’ll need to erase the drive you attached, so if you have important files on it, you need to store them somewhere else before transferring them to your Pi-NAS. From a Terminal window, run the following command to see the disks connected to your Pi:
sudo fdisk -l
Find the external drive you want to use for your files—in my case, it’s an 80GB drive called “MyExternalDrive”—and note its path. In the screenshot above, the 80GB drive plugged into my Pi corresponds to /dev/sda. (Make absolutely sure you note the correct drive, as we’re about to erase it!) First, you need to unmount the drive. If you’re using the Raspbian interface, you can just click the eject button next to the drive to unmount it. But if you are using a terminal over SSH, you have to run:
umount /dev/sda1
If your drive has multiple partitions, you will have to erase each one separately by using sequential numbers. Run the previous command, but with sda2 and sda3, and so on. Then, to erase and format your drive for Linux usage, run:
sudo parted /dev/sda
3. Partition Your Drive
When you run that code, it will open up a wizard called Parted, which will allow you to create a new partition on the drive. Run the following commands and press Enter after each answer in the wizard:
mklabel gpt
If prompted to erase the drive, type Y and press Enter. Then run:
mkpart
Replace MyExternalDrive with the name you want to use for the drive:
MyExternalDrive
Continue by entering the following:
ext4
0%
100%
Then run the following command to exit the Parted wizard:
quit
Obviously, you can adjust these commands to fit the name of your drive, the number and size of partitions you want to make on it, and so on—but for most basic users just starting out, these commands should work well.
4. Format the Partition
Next, we need to format that partition. If your drive is located at /dev/sda, the new partition will be located at /dev/sda1 (if the drive is /dev/sdb, you will use /dev/sdb1, and so on). Run this code:
sudo mkfs.ext4 /dev/sda1
Press Y and Enter when asked if you want to proceed. Then run and replace MyExternalDrive with whatever you want to name your drive:
sudo e2label /dev/sda1 MyExternalDrive
Formatting will take a few minutes, especially if you have a large drive, so be patient. When it’s finished, run this command to reboot your Pi:
sudo shutdown -r now
When your Pi boots back up, you should find that the external drive appears automatically on the desktop, ready for action. You will, however, have to run one final command to give yourself permission to write new files to the drive. In a Terminal, run:
sudo chown -R pi /media/pi/MyExternalDrive
5. Share the Drive
Now it’s time to share that drive on your network, so you can add your files and access them from any device in the house. To do this, we need a tool called Samba, which is an open-source implementation of Windows’ SMB/CIFS file-sharing protocol.
It’s not your only option for sharing files, but Samba is easy to set up and compatible with just about any system you might have on the network, so it’s what I recommend. Raspbian doesn’t come with Samba installed by default, so you need to make sure your repositories are up to date and install it with the following commands:
sudo apt update
sudo apt upgrade
sudo apt install samba samba-common
The installer will ask if you want to modify smb.conf to use WINS settings from DHCP. Choose Yes and press Enter. Now you edit that configuration file yourself, to share your drive. Run:
sudo nano /etc/samba/smb.conf
Then, from the command-line text editor that appears, use your arrow key to scroll to the bottom of the document. You want to add a block of text that looks something like this:
[MyMedia]
path = /media/pi/MyExternalDrive/
writeable = yes
create mask = 0775
directory mask = 0775
public=no
In your version of this, MyMedia would be the name of your share (name it whatever you want) and /media/pi/MyExternalDrive would be the mounted location of your drive. (You may need to open up the file manager and head to /media/pi/ to figure out what it’s called.)
When finished, press Ctrl-X to exit nano, pressing Y and Enter when asked if you want to save the file.
6. Create a Password and Add Users
Finally, you will need to create a password for Samba so you can see your share from other machines. (There are ways to configure Samba without requiring a password, but this generally isn’t good security practice, so I recommend adding a password.) To add a password to the existing Pi user, run:
sudo smbpasswd -a pi
Enter your desired password when prompted—it doesn’t have to be the same as your user password on the Pi itself, but it can be—and press Enter. You can then add other users with the following code (where jeff is the user you want to add):
sudo adduser jeff
You can also choose to give that user their own password by next running:
sudo smbpasswd -a jeff
This isn’t strictly necessary, but it can be useful if you have multiple people in your household to whom you want to give different read and write permissions on certain shares. Once all users are added, run the following command to restart Samba:
sudo systemctl restart smbd
From here, the setup process is finished. You can now access your media from the network.
7. Access Your Media
On your Windows PC, open File Explorer and type \\raspberrypi\MyMedia into the address bar (replacing MyMedia with whatever your share is called) and press Enter. You should then be able to enter your Samba username (pi) and password to see your shared drive. If you run into trouble, you might have to use the Pi’s IP address, like \\192.168.1.10\MyMedia instead.
Thanks
-
This topic was modified 3 months, 1 week ago by
Sinan.
-
This topic was modified 3 months, 1 week ago by
Sinan.
-
This topic was modified 3 months, 1 week ago by
Sinan.
-
This topic was modified 3 months, 1 week ago by
Sinan.
Attachments:
You must be logged in to view attached files. -
This topic was modified 3 months, 1 week ago by
- AuthorPosts
- You must be logged in to reply to this topic.