Raspberry PI NAS

In this post we will look at how to configure Raspberry Pi as NAS (Network Attached Storage).


Mounting the external hard drive with the appropriate permissions
  1. Log into raspberry pi (ssh username@ipaddress)
  2. Update Raspbian by executing following commands
  3. sudo apt-get update
    sudo apt-get upgrade
    
  4. Install ntfs support
  5. sudo apt-get install ntfs-3g
  6. Find the external hard drive (like sda1, sdb1, sdc1, etc.)
  7. sudo fdisk -l
  8. Before mouning the drives we will need to create a directory to mount to.
  9. sudo mkdir /media/NASHDD1
  10. now we need to create a user to login as.
  11. sudo useradd username -m -G users
    sudo passwd password
    you will be prompted to enter the password twice
  12. We need to get the gid and the uid (write these down somewhere, as we will need them in the next step). Update username with the user you created above.
    • For the gid enter the following:
    id -g username
    • For the uid enter the following:
    id -u username
  13. Next step is to edit fstab to automatically mount the USB drive automatically with correct permissions when Pi boots up
  14. sudo nano /etc/fstab
  15. Add the following the line to the bottom of the file. Change the /dev/sda1 to whatever your hard drive is and update uid and gid as appropriate. (The code below is all one line)
  16. /dev/sda1 /media/NASHDD1 auto uid=enter_uid_here,gid=enter_gid_here,noatime 0 0
  17. Now reboot the Pi and the drive will be mounted with the correct permissions.

Configure Raspberry Pi Samba Server
  1. Install the samba package
  2. sudo apt-get install samba samba-common-bin
  3. Before start editing the samba config file take a backup
  4. sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.old
  5. Start editing the file by executing the following command
  6. sudo nano /etc/samba/smb.conf
  7. Firstly, find “#security = user” in the file and remove the # from that line.
  8. Next, add the external hard drive by adding the following commands
    [NAS]
    comment = NAS Folder
    path = /media/NASHDD1
    valid users = @users
    force group = users
    create mask = 0660
    directory mask = 0771
    read only = no
    • [NAS]:  This is the name of the share (What you will see in file explorer)
    • Comment: This is a comment for the associated for share.
    • Path: Path to the folder you wish to share.
    • Valid User: A list of users that are allowed to login to this share.
    • Force Group: This specifies a UNIX group name that will be assigned for all users connecting to this share. 
    • Directory Mask: This creates a permission mask for all directories created on the drive.
    • Read Only: This allows you to set the share to be read only.
  1. Now restart the samba server by executing the following command
  2. sudo/etc/init.d/samba restart
  3. Finally connect the user to the samba
  4. sudo smbpasswd -a username
It is established that the Raspberry Pi works well with the Ext4 formatted drives (discussion)

Now we will see how to format in Ext4 format (reference)

Creating data redundancy
This can be achieved in two ways depending on your needs, but both methods will use rsync to sync data between the drives.

Firstly install the rsync package
apt-get install rsync

First method backs up the data at a certain specified time of the day, but the second method will update the backup drive as soon a change happens.

Backup at a defined time:

  • Install crontab
  • sudo apt-get crontab


Backup as soon a change happens



You may also interested in reading:

No comments:

Post a Comment