Raspberry Pi: serve files on your network with Samba
Turn a Raspberry Pi into a small NAS by setting up Samba file sharing. Access the Pi's files from any computer on your network.
A Raspberry Pi with a USB hard drive attached and Samba running is the smallest useful NAS. It will not compete with a Synology, but for a few hundred GB of family photos and a place for backups to land, it is perfect.
This tutorial walks through installing Samba, configuring a shared folder, and accessing it from Windows, macOS, and Linux.
What you need
- Raspberry Pi (4 or 5 recommended; the older ones work but are slow for large file transfers)
- USB hard drive or SSD (an SSD is faster; a USB 3 enclosure is what you want)
- Powered USB hub if the drive needs more than 1.2 A
Step 1: mount the drive
Plug in the drive. Find its device name:
lsblk
You will see something like:
sda 8:0 0 1.8T 0 disk
└─sda1 8:1 0 1.8T 0 part
/dev/sda1 is the partition. Format it as ext4 (Linux-native; best
performance) or NTFS (if you need Windows compatibility):
sudo mkfs.ext4 -L "storage" /dev/sda1
Mount it:
sudo mkdir -p /mnt/storage
sudo mount /dev/sda1 /mnt/storage
sudo chown -R brian:brian /mnt/storage
Make the mount persistent across reboots. Get the drive’s UUID:
sudo blkid /dev/sda1
Note the UUID="..." value. Add to /etc/fstab:
UUID=your-uuid-here /mnt/storage ext4 defaults,noatime 0 2
Test:
sudo umount /mnt/storage
sudo mount -a
If the mount comes back, the fstab entry is right.
Step 2: install Samba
sudo apt install -y samba samba-common-bin
The default config starts Samba as a service. We will overwrite the config in a moment.
Step 3: configure the share
Back up the default config:
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak
Edit /etc/samba/smb.conf. Replace the whole thing with:
[global]
workgroup = WORKGROUP
server string = Pi NAS
security = user
map to guest = bad user
dns proxy = no
[storage]
path = /mnt/storage
browseable = yes
writable = yes
read only = no
guest ok = no
create mask = 0644
directory mask = 0755
valid users = brian
workgroup = WORKGROUP is the default Windows workgroup. Change if yours
is different.
Step 4: add your user to Samba
Samba has its own password database (separate from the system password):
sudo smbpasswd -a brian
Enter a password. This is the password you will use when connecting from other computers.
Step 5: restart Samba
sudo systemctl restart smbd
Step 6: connect from another computer
Windows:
- Open File Explorer
- Type
\\pi-pihole.local\storagein the address bar - Enter username
brianand the Samba password - (Optional) Right-click the share and “Map network drive” for permanent access
macOS:
- Finder >> Go >> Connect to Server
- Enter
smb://pi-pihole.local/storage - Enter username and password
- The share appears in Finder under Locations
Linux:
sudo apt install -y cifs-utils
sudo mount -t cifs //pi-pihole.local/storage /mnt/remote -o username=brian,password=your-samba-password
Or use the GNOME/KDE file manager’s “Connect to Server” option.
Performance
A Raspberry Pi 4 with a USB 3 SSD can do about 100 MB/s read and 50 MB/s write over gigabit ethernet. A USB 3 HDD is bottlenecked at about 100 MB/s by the drive itself. A USB 2 drive tops out at about 30 MB/s.
For most home uses (file backups, photo storage), this is more than enough. If you need 1 GB/s, you need a real NAS.
Accessing from outside the home network
For most people, do not. Exposing Samba to the internet is a bad idea. If you need remote access, set up a VPN (WireGuard is the easy pick) or use Tailscale for a peer-to-peer VPN with no port forwarding.
The WireGuard setup is in the book Self-Hosted with Raspberry Pi.
Backing up the share
The whole point of a NAS is having backups. Two layers:
Local backup (rsync to a second drive):
rsync -av /mnt/storage/ /mnt/storage-backup/
Offsite backup (rsync to a cloud storage provider):
rclone sync /mnt/storage/ remote:my-bucket/backup/
rclone supports Google Drive, Dropbox, Backblaze B2, S3, and many more.
When the share is slow
- Network bottleneck. If you are on Wi-Fi, switch to ethernet. Large file transfers over Wi-Fi are 5-10x slower than ethernet.
- Drive bottleneck. A USB 2 drive tops out at about 30 MB/s. Check
with
hdparm -t /dev/sda1. - Pi bottleneck. The Pi 4 has USB 3. The Pi 3 has USB 2 (max 30 MB/s). An SSD on USB 3 is much faster.
What to build next
- A Plex media server (uses the same share).
- A photo backup script that runs when your phone joins the Wi-Fi.
- An automatic rsync to a cloud backup service.
The Plex server is in the book Self-Hosted with Raspberry Pi. The photo backup is one of the next tutorials on this site.