Setup NAS on Raspberry Pi

I got my Raspberry Pi 4 8GB model a few months ago. But I only used it for testing Linux system. This powerful toy should be used for something more useful. So I decided to set up a NAS (Network Attached Storage) on it.

Setup static IP address

There are multiple ways of doing this. I am going to use interfaces file to set up static IP address.

Go to /etc/network/interfaces and add the following lines at the end of the file:

1
2
3
4
5
6
7
iface eth0 inet static
address 192.168.1.100
network 192.168.1.0
netmask 255.255.255.0
broadcast 192.168.1.255
gateway 192.168.1.1
dns-nameservers 1.1.1.1 1.0.0.1

Add NAS IP address to local NAS lookup

Go to /etc/hosts and add the following line at the end of the file:

1
192.168.1.100 nas.local

Then you can connect to your NAS with ssh user@nas.local

Mount drives

This is optional if you have a large SD card on the Raspberry Pi. I have a 128GB SD card on my Raspberry Pi. But I also have a 1TB external hard drive. So I want to mount the external hard drive to the Raspberry Pi.

Create a mount point

1
2
sudo mkdir /mnt/disk1
sudo chown -hR $(whoami):$(whoami) /mnt/disk1

Add the UUID of the drive

1
sudo blkid

Then find the device’s UUID. For me, UUID is 6489-3C57:

1
2
/dev/sda2: LABEL="data1000" UUID="6489-3C57" BLOCK_SIZE="512" TYPE="exfat" PARTLABEL="Basic data partition" PARTUUID="dfef2622-070d-43bb-8503-9ac8fffdb6cd"
/dev/sda1: PARTLABEL="Microsoft reserved partition" PARTUUID="130fa0da-5d4f-41a0-a315-193ca186d94d"

Then Add the UUID to fstab:

1
2
sudo vim /etc/fstab
UUID=6489-3C57 /mnt/disk1 exfat user,rw,auto 0 2

Explanation of the options:

  • exfat: the file system of the drive
  • user: allow any user to mount the drive
  • rw: mount the drive as read-write
  • auto: mount the drive automatically when the system boots
  • 0: dump. 0 means the drive will not be backed up. 1 means the drive will be backed up (Deprecated).
  • 2: fsck order. 0 means the drive will not be checked. 1 means the drive will be checked first. 2 means the drive will be checked after all the drives with 1 are checked.

Mount driver

1
2
3
4
# Check users first
grep ^"$USER" /etc/group
# mount
sudo mount -o rw,user,uid=1000 /dev/sda2 /mnt/disk1

Setup Samba

Install Samba

1
sudo apt-get install samba samba-common-bin

Add the following lines to /etc/samba/smb.conf:

1
2
3
4
5
[mynas]
comment = Samba on My NAS
path = /mnt/
read only = no
browsable = yes

This creates a share called “mynas” allowing access to all the drives mounted under the /mnt folder.

Read-only is set to no, which permits modifying and writing data to the share.

Browsable allows the share to be seen by a Linux file manager or Windows Explorer.

Add user account

1
2
sudo smbpasswd -a $(whoami)
sudo smbpasswd -a anotheruser

Restart Samba

1
sudo systemctl restart smbd

Finally, you can connect to your NAS with \\nas.local in Windows Explorer.

Comments