rss logo

GNU/Linux - How to create a NAS on Debian with mdadm, and Samba

Tux logo

In this article, I'm going to detail a way of creating your own NAS (Network-attached storage) with a Debian GNU/Linux.

The main advantage is to have a NAS (Network-attached storage), i.e. a network-accessible storage space with multiple disks and therefore high capacity, low cost (you can for example consider recycling an old computer), very flexible, redundant and configurable.

I'll be using the Debian Linux distribution with the mdadm utility to manage RAID and samba to provide shared access to files via the smb protocol. All configuration is done from the command line.

Target Architecture

In the end our target architecture will look like this.

Diagram of a NAS setup showing a network share accessible via a TV and a PC. The NAS includes multiple 2TB drives configured for network sharing at \\192.168.1.100\share.

Configuration

Hardware Requirements

As we saw above, it's possible to create a low-cost NAS by recycling old computer hardware.

  • Here is an example of hardware configutation:
    • CPU: > intel dual core
    • RAM: 512 MB
    • HDD: 1x 500 GB (OS) + 3x 2TB (data)
Illustration of NAS hardware setup showing a server and multiple 2TB hard drives being added to create a storage system.

Software Configuration

We're going to install Debian on our first hard disk (/dev/sda) and then create a RAID 5 with our three 2TB disks (sdb, sdc and sdd).

Diagram of NAS setup with RAID 5 using Debian OS. Shows system partition on /dev/sda and a RAID 5 array with /dev/sdb, /dev/sdc, and /dev/sdd providing 4TB of data storage.

Settings

First of all, we need to install Debian, as brilliantly explained here.

Network Configuration

  • Edit the /etc/network/interfaces file (replace ens192 by your own interface):
allow-hotplug ens192
iface ens192 inet static
        address 192.168.1.100
        netmask 255.255.255.0
        gateway 192.168.1.254
	dns-nameservers 192.168.1.254
  • Restart the system or the networking service:
root@host:~# systemctl restart networking

Partition Table

  • Install the gdisk tool, which is the gpt version of fdisk:
root@host:~# apt update && apt install mdadm gdisk
  • Clean partition table for /dev/sdb:
root@host:~# wipefs -a /dev/sdb[1-9]*
root@host:~# wipefs -a /dev/sdb
  • Create a partition table for /dev/sdb:
Let's assume our disks are allocated as follows (use fdisk -l to be sure): sdb, sdc, sdd
root@host:~# gdisk /dev/sdb
GPT fdisk (gdisk) version 1.0.3

Partition table scan:
  MBR: not present
  BSD: not present
  APM: not present
  GPT: not present

Creating new GPT entries.

Command (? for help): n
Partition number (1-128, default 1): 
First sector (34-4194270, default = 2048) or {+-}size{KMGTP}: 
Last sector (2048-3907028991, default = 3907028991) or {+-}size{KMGTP}: 
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): FD00
Changed type of partition to 'Linux RAID'

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!!

Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.
  • Copy the partition table from /dev/sdb to /dev/sdc and /dev/sdd:
root@host:~# sgdisk /dev/sdb -R /dev/sdc
root@host:~# sgdisk /dev/sdb -R /dev/sdd
  • Alternatively, you can copy the partition table using the sfdisk tool:
root@host:~# sfdisk -d /dev/sdb | sfdisk --force /dev/sdc
root@host:~# sfdisk -d /dev/sdb | sfdisk --force /dev/sdd
  • Randomize the GUID disk and all partitions to avoid having the same GUID on all disks:
root@host:~# sgdisk -G /dev/sdc
root@host:~# sgdisk -G /dev/sdd

mdadm

mdadm is a utility used to create, manage and monitor software RAID devices on GNU/Linux.

  • Install the mdadm tool:
root@host:~# apt install mdadm
  • Create a RAID 5 with our three disks:
root@host:~# mdadm --create --level=5 --raid-devices=3 /dev/md0 /dev/sdb1 /dev/sdc1 /dev/sdd1
  • Check RAID status:
root@host:~# cat /proc/mdstat 
Personalities : [raid6] [raid5] [raid4] 
md0 : active raid5 sdd1[3] sdc1[1] sdb1[0]
      418713600 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU]
      
unused devices: <none>
  • Create a file system on /dev/md0:
root@host:~# mkfs.ext4 /dev/md0
  • Create a mount folder:
root@host:~# mkdir /data
  • Mount the file system located on the /dev/md0 device in the /data directory:
root@host:~# mount /dev/md0 /data
  • Show mount points:
root@host:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            983M     0  983M   0% /dev
tmpfs           200M  5.6M  194M   3% /run
/dev/sda2        14G  1.5G   12G  12% /
tmpfs           998M     0  998M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           998M     0  998M   0% /sys/fs/cgroup
/dev/sda1       511M  3.3M  508M   1% /boot/efi
tmpfs           200M     0  200M   0% /run/user/0
tmpfs           200M     0  200M   0% /run/user/1000
/dev/md0        3.8T   16M  3.7T   1% /data
  • Save the RAID configuration so that it is automatically reassembled on system startup:
root@host:~# mdadm --detail --scan >> /etc/mdadm/mdadm.conf
  • Update the initramfs to take this into account:
root@host:~# update-initramfs -u
  • Add an fstab entry to mount the RAID at startup:
root@host:~# echo "/dev/md0 /data ext4 rw,nofail,relatime,x-systemd.device-timeout=20s,defaults 0 2" >> /etc/fstab

Samba

We're now going to install and configure the samba service, which will enable Windows machines to access file sharing.

  • Install the samba service:
root@host:~# apt install samba
  • Creating a samba user:
root@host:~# adduser --home /data --system samba
  • Set rights:
root@host:~# chown samba: /data
  • Create a password to access the share:
root@host:~# smbpasswd -a samba
  • Edit the /etc/samba/smb.conf file:
[global]
   workgroup = WORKGROUP
   server string = nas
[share]
   path = /data
   read only = no
   valid users = samba
  • Restart samba services:
root@host:~# systemctl restart smbd; systemctl restart nmbd
  • From a Windows computer, check that you can access your share:
Step-by-step process to access a Samba network share on Windows, showing entering network credentials and accessing the shared folder.