Skip to content

Raspberry Pi NAS Setup: Share Files with Samba

A Raspberry Pi NAS gives other computers a folder they can read and write over your home network. Start with one password-protected Samba share on the Pi's existing storage, then connect from a second computer and save a test file. Once that works, you can move the share to a USB drive.

What you need

  • A Pi 4 or 5 running Raspberry Pi OS with an ordinary login that can use sudo.
  • A Windows, Mac, or Linux computer on the same trusted local network. Use Ethernet if convenient.
  • Enough space for a small test file. An external drive is optional until you want to store real data.

Samba is the Linux server used here. SMB is the file-sharing protocol your other computer uses to connect. Installing Samba starts a network service on a standard Raspberry Pi OS setup, so use a trusted LAN and keep TCP port 445 off the internet. On a Pi that already serves files, check its existing configuration and pick a new share name and directory.

First check What success looks like
Server Samba accepts its configuration and lists the new share
Login Your Samba username and password open the share
Write A small file saved on the client appears on the Pi
Optional USB drive The share's parent directory is on the expected mounted partition

Create a private Samba share

Install the server and register your login

Run these on the Pi:

sudo apt update
sudo apt install samba smbclient

Run the next commands from your ordinary Pi login. They use your account as the owner of a new directory. If /srv/pi-nas already exists, choose a new path and use it consistently throughout the exercise.

1
2
3
4
5
6
nas_user=$(id -un)
printf 'Samba username: %s\n' "$nas_user"

# Continue only if this command creates a new directory.
sudo mkdir /srv/pi-nas &&
  sudo install -d -m 0700 -o "$nas_user" -g "$(id -gn)" /srv/pi-nas/shared

The 0700 mode gives only the owner access through normal local file permissions. Create the corresponding Samba password interactively. Samba has its own password database and this prompt does not change your Linux login password. Samba password command reference

sudo smbpasswd -a "$nas_user"

If this user already has a Samba password, keep it and skip the command unless you intend to change it.

Add just one share

Save a copy of the server configuration before editing. The backup command prints its path.

1
2
3
nas_backup=$(sudo mktemp -d /root/pi-nas-backup.XXXXXX) &&
  sudo cp -p /etc/samba/smb.conf "$nas_backup/smb.conf" &&
  printf 'Backup: %s/smb.conf\n' "$nas_backup"

Review the current shares with sudo testparm -s. If [nas-lab] already exists, choose a unique share name and use it in the connection addresses below. Open the configuration with sudoedit /etc/samba/smb.conf and append this section. Replace alex with the username printed earlier.

1
2
3
4
5
6
7
[nas-lab]
    path = /srv/pi-nas/shared
    read only = no
    guest ok = no
    valid users = alex
    create mask = 0600
    directory mask = 0700

valid users controls access to this share. A user who passes that check still needs permission to access the underlying Linux directory. The masks restrict the permission bits on files and directories created by SMB clients. Samba share settings reference

Check the saved configuration and reload Samba only if it passes:

sudo testparm -s && sudo systemctl reload smbd
systemctl is-active smbd

Look for [nas-lab] with the correct path and user, then expect active. If the service is stopped, inspect systemctl status smbd --no-pager before starting it. Check the share locally first. The command prompts for the Samba password:

smbclient //127.0.0.1/nas-lab -U "$(id -un)" -c 'ls'
ip -brief address

A directory listing from the first command confirms that Samba accepted your login for this share. In the address output, find the IP on your active LAN interface. Use that address on the client.

Connect from Windows, macOS, or Linux

Use your Pi's actual LAN IP. The address 192.168.1.50 below is an example.

Client Open the share
Windows In File Explorer's address bar, enter \\192.168.1.50\nas-lab.
macOS In Finder, choose Go → Connect to Server and enter smb://192.168.1.50/nas-lab.
Linux Use smb://192.168.1.50/nas-lab in a file manager with SMB support, or use smbclient //192.168.1.50/nas-lab -U alex. Use your Pi's Samba username in place of alex.

Choose the Pi's Samba username and password when prompted. Save a small disposable file named nas-check.txt on the share, reopen it from the client, and check on the Pi:

ls -l /srv/pi-nas/shared/nas-check.txt

If you used a different path, substitute it here. The file confirms both the client write and the server-side destination. Use the direct IP first to rule out hostname lookup issues. Windows SMB overview · Connect to a server on Mac

Use an existing USB drive

Move the share only after the first test works. The example below expects an existing ext4 partition that you own and have backed up. It leaves partitioning and formatting out of the exercise. Connect the drive and identify the target by model, size, filesystem and UUID:

lsblk -o NAME,SIZE,MODEL,TRAN,FSTYPE,UUID,MOUNTPOINTS

If the target partition is already mounted elsewhere, close the files and programs using it and unmount it cleanly before continuing. Set nas_uuid to the observed UUID. Mount it on a new, dedicated directory and confirm the mount before creating a share:

1
2
3
4
nas_uuid=REPLACE_WITH_VERIFIED_EXT4_UUID
sudo mkdir /mnt/pi-nas-disk &&
  sudo mount -t ext4 "UUID=$nas_uuid" /mnt/pi-nas-disk &&
  findmnt --mountpoint /mnt/pi-nas-disk -o SOURCE,FSTYPE,TARGET,OPTIONS

If the mount command fails, stop and inspect the error. Compare SOURCE with the partition in lsblk and check that the options include rw before proceeding. Create a new directory on the mounted drive, keeping the mounted filesystem's existing ownership and data intact:

1
2
3
4
findmnt --mountpoint /mnt/pi-nas-disk &&
  sudo mkdir /mnt/pi-nas-disk/pi-nas-share &&
  sudo chown "$(id -un):$(id -gn)" /mnt/pi-nas-disk/pi-nas-share &&
  chmod 0700 /mnt/pi-nas-disk/pi-nas-share

Change only the path in [nas-lab] to /mnt/pi-nas-disk/pi-nas-share, then validate, reload and reconnect. Repeat the write test and check the file on the new drive. Changing the path leaves the original practice folder in place. Copy any files you want to keep after checking which folder is currently shared.

For a persistent mount, first back up /etc/fstab and check for an existing entry for that UUID or mount point. Add one entry using the verified UUID:

UUID=REPLACE_WITH_VERIFIED_EXT4_UUID /mnt/pi-nas-disk ext4 defaults,nofail 0 2

Use the directory's owner and mode to grant access on ext4. The nofail option allows boot to continue if the disk is absent. Keep the share subdirectory only on the external filesystem so a missing drive leaves that path unavailable. After editing, run sudo findmnt --verify --verbose and then sudo systemctl daemon-reload if verification succeeds. At the next boot, confirm the exact mount and repeat the small write test before relying on the share. Raspberry Pi external storage guide · findmnt reference

Fix connection, login, or write problems

Symptom First check
Client cannot reach the Pi Try the direct LAN IP. Check that both devices can communicate and that the Pi's firewall allows TCP 445 from the trusted LAN.
Local smbclient cannot connect Check systemctl status smbd --no-pager and sudo journalctl -u smbd -b --no-pager -n 30.
Password keeps prompting Confirm the Samba account and password. Disconnect a stale client session to the same server if it is reusing another login.
Login works but writing fails Use namei -l /srv/pi-nas/shared or your actual path. Check ownership, traversal permission on parent directories, share settings and filesystem mount options.
USB share disappears after reboot Run findmnt --mountpoint /mnt/pi-nas-disk. Check the drive, UUID and mount logs before recreating any directories.
Windows reports a guest or signing problem Use an authenticated account and the client's SMB2/SMB3 defaults. Microsoft SMB signing guidance

Finish the exercise and protect your files

To stop sharing the practice folder, remove only the [nas-lab] section you added, check the configuration and reload Samba. Keep the contents until you have checked whether you need them. Unmount an external drive cleanly before unplugging it, after closing clients and checking that the share no longer uses it.

Before moving photos or backups to the NAS, copy one disposable file to a separate destination and verify you can restore it. A second folder on the same drive shares the drive's failure risk. For access from outside your home, plan an authenticated private connection and keep SMB off the public internet. Backup and restore practice · Raspberry Pi remote access options

Checkpoint. Which login does the client use, and how can you prove that a file was saved to the USB drive? Once you can answer both, try a separate media-server project or measure storage and network throughput.

Donate