How to set up Raspberry Pi as a tiny NAS server

Today I will shortly describe how to set up Raspberry Pi as a tiny NAS server. I am using one at home on a daily basis and it is very useful appliance.

Wireless throughput will be significantly lower then Ethernet so please take this into consideration.

Prerequisites

  • Raspberry Pi including case and Power Supply Unit
  • D-Link DWA-121 Wireless N 150 Micro USB Adapter
  • 4GB SD Card
  • External USB Hard Drive

Step 1

Download Raspbian image and install it on the SD Card.

Insert SD Card and verify device name.

$ dmesg | tail
[32820.816330] usb 1-1.5: new high-speed USB device number 9 using ehci_hcd
[32821.164495] usb 1-1.5: New USB device found, idVendor=0bda, idProduct=0138
[32822.185501] sd 15:0:0:0: Attached scsi generic sg3 type 0
[32822.865983] sd 15:0:0:0: [sdc] 7744512 512-byte logical blocks: (3.96 GB/3.69 GiB)
[32822.866925] sd 15:0:0:0: [sdc] Write Protect is off

Write image file directly to the SD Card.

$ sudo dd if=2013-09-25-wheezy-raspbian.img of=/dev/sdc

Step 2

Mount previously created SD Card.

$ sudo mkdir /mnt/raspi
$ sudo mount /dev/sdc1 /mnt/raspi

Configure network interfaces. I will use 192.168.1.3 as a static wlan0 IP address and 10.10.10.3 as an administration eth0 IP address.

$ cd /mnt/raspi
$ cat << EOF | sudo tee etc/network/interfaces 
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static 
address 10.10.10.3
netmask 255.255.255.0

auto wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface wlan0 inet static
address 192.168.1.3
gateway 192.168.1.1
netmask 255.255.255.0

EOF
The above configuration will throw couple of warnings/errors at boot time but do not worry as it will definitely work.

Define wireless network settings.

$ cat << EOF | sudo tee etc/wpa_supplicant/wpa_supplicant.conf
network={
  ssid="wireless-home-network"
  psk="wireless-home-network-pass"
}
EOF

Modify default ifplugd configuration to use both of the interfaces (eth/wlan) at the same time.

$ cat << EOF | sudo tee etc/default/ifplugd 
INTERFACES=""
HOTPLUG_INTERFACES=""
ARGS="-q -f -u0 -d10 -w -I"
SUSPEND_ACTION="stop"
EOF
Alternatively you can use sudo dpkg-reconfigure ifplugd command to modify this file using nice and well-described user interface.

Unmount and remove SD Card.

$ cd
$ sudo umount /mnt/raspi

Step 3

Assembly Raspberry Pi, connect Wireless USB Adapter, and PSU.

Device will boot after a while.

Step 4

Connect to the configured Raspberry Pi device using SSH.

$ ssh 192.168.1.3 -l pi
All further steps will be performed on the configured Raspberry Pi device.

Step 5

Execute Raspberry Pi Software Configuration Tool.

$ sudo raspi-config

Change default user password, expand filesystem and reboot device afterwards.

$ sudo reboot

Step 6

Upgrade the installed system.

$ sudo apt-get update
$ sudo apt-get -y upgrade
$ sudo reboot

Step 7

Prepare external USB Drive connected to the Raspberry Pi.

$ sudo fdisk /dev/sda

Command (m for help): o
Building a new DOS disklabel with disk identifier 0x887fe78d.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
Partition number (1-4, default 1): 
Using default value 1
First sector (2048-976773167, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-976773167, default 976773167): 
Using default value 976773167

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
$ sudo mkfs.ext4 /dev/sda1

Step 8

Create /media/smb directory.

$ sudo mkdir /media/smb	

Configure system to automatically mount external USB Drive at boot.

$ cat << EOF | sudo tee -a /etc/fstab
/dev/sda1       /media/smb      ext4    defaults          0       3
EOF

Mount configured filesystem.

$ sudo mount -a	

Step 9

Install samba with an additional utilities.

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

Step 10

Prepare Samba configuration.

$ cat << EOF | sudo tee /etc/samba/smb.conf
[global]
   disable netbios = yes
   workgroup = HOME
   server string = Home NAS server
   dns proxy = no
   interfaces = 127.0.0.0/8 wlan0
   bind interfaces only = yes
   syslog = 0
   printing = bsd
   security = user
   encrypt passwords = true
   passdb backend = tdbsam
   obey pam restrictions = yes
   unix password sync = yes
   passwd program = /usr/bin/passwd %u
   passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssuccessfully* .
   pam password change = yes
   map to guest = bad user
   usershare allow guests = no

[applications]
   comment = Applications
   path = /media/smb/applications
   valid users = @samba_users
   public = no
   writable = yes

 [backups]
   comment = Backups
   path = /media/smb/backups
   valid users = milosz
   public = no
   writable = yes

 [projects]
   comment = Projects
   path = /media/smb/projects
   valid users = milosz
   public = no
   writable = yes	
EOF

Step 11

Create required Samba users.

$ sudo useradd -s /usr/sbin/nologin -M milosz
$ sudo smbpasswd -a milosz
$ sudo useradd -s /usr/sbin/nologin -M agnes
$ sudo smbpasswd -a agnes

Create samba_users group (it will contain all Samba users).

$ sudo groupadd samba_users
$ sudo usermod -G samba_users milosz
$ sudo usermod -G samba_users agnes

Create required directories (shares) and set permissions.

$ sudo mkdir /media/samba/{applications,backups,projects}
$ sudo chown milosz /media/samba/backups
$ sudo chown milosz /media/samba/projects
$ sudo chown :samba_users /media/smb/application
$ sudo chmod 770 /media/smb/{applications,backups,projects}	

Step 12

Restart Samba service.

$ sudo service samba restart

Ending notes

Currently I am using two external USB Drives connected to the Raspberry Pi device and it is working without any hassle. I have also switched from Wireless to Ethernet as it is fast enough for daily usage.

Milosz Galazka's Picture

About Milosz Galazka

Milosz is a system administrator working for a successful Polish company and a long time supporter of Free Software Foundation and Debian operating system.

Gdansk, Poland https://sleeplessbeastie.eu