Today I will shortly describe how to set up Raspberry Pi as a tiny NAS server. I am using one at home daily, and it is a handy appliance.
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 the Raspbian image and install it on the SD Card.
Insert the SD Card to identify the 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 an 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
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) simultaneously.
$ cat << EOF | sudo tee etc/default/ifplugd INTERFACES="" HOTPLUG_INTERFACES="" ARGS="-q -f -u0 -d10 -w -I" SUSPEND_ACTION="stop" EOF
sudo dpkg-reconfigure ifplugd
command to modify this file using a nice and well-described user interface.Unmount and remove the SD Card.
$ cd $ sudo umount /mnt/raspi
Step 3
Assembly Raspberry Pi, connect Wireless USB Adapter, and PSU.
The device will boot after a while.
Step 4
Connect to the configured Raspberry Pi device using SSH.
$ ssh 192.168.1.3 -l pi
Step 5
Execute Raspberry Pi Software Configuration Tool
.
$ sudo raspi-config
Change the default user password, expand the filesystem, and reboot it afterward.
$ sudo reboot
Step 6
Upgrade the installed system.
$ sudo apt-get update
$ sudo apt-get -y upgrade
$ sudo reboot
Step 7
Prepare an 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 the system to automatically mount an 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 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. It is working without any hassle. I have also switched from Wireless to Ethernet as it is fast enough for daily usage.