The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.

Sunday, January 17, 2016

Setting up Raspberry Pi with Samba and external hard drive - AT&T U-verse router configuration to make it remotely accessible

Raspberry Pi 2 Model B (I bought it with a case and charger - Amazon link)
with 64 GB Micro SD card -  Amazon link
AT&T U-verse router 5031NV

Once I finished OS installation and SSH and VNC configuration, I wanted to access the files I stored in an external Hard drive using Samba on Raspberry Pi.

Set up the external hard drive

Connect the external drive to RP, and run

1. $ sudo fdisk -l

and find the device and note the "Type". If the type is FAT32, proceed to the next step. But if it's "NTFS", install the NTFS driver using

2. $ sudo apt-get install ntfs-3g

The fdisk command will also print the device e.g. /dev/sda1 that we will use in this step. This is to make sure the device is automatically mounted on a pre-decided location e.g. /media/HD500G. In order to do that, I created a new directory

3. # mkdir /media/HD500G

and added a new line at /etc/fstab

5. /dev/sda1    /media/HD500G    ntfs-3f    defaults,noatime    0    0

If your filesystem is FAT, use "vfat" instead of "ntfs-3g".

Then unmount and mount the drive.

$ sudo umount /dev/sda1
$ sudo mount /dev/sda1

And make sure all your files and directories are there.

$ cd /media/HD500G
$ cd ls -la

It might be good idea to spindown the external drive if it's inactive for a while. Use hdparm to do so once it's inactive for 20 minutes.

$ sudo apt-get install hdparm
$ sudo hdparm -S 240 /dev/sda1

Install and configure Samba

Installing Samba is easy: $ sudo apt-get install samba samba-common-bin

In order to configure it to access /media/HD500G, I added the following to /etc/samba/smb.conf:

comment = Movies and Music
path = /media/HD500G
valid users = pi
force group = users
create mask = 0660
directory mask = 0771
read only = no

The next step will be adding a user account and setting up a password.

$ sudo smbpasswd -a pi

After that, restart the samba services:

$ sudo service smbd restart
$ sudo service nmbd restart

Open Samba ports on your router

This is required to access the Samba share outside my home-network i.e. from anywhere on the internet. The default ports are TCP 139,445 and UDP 137,138.  You can verify the ports by using

# netstat -plunt | grep smbd

Router Configuration:

Go to the configuration page using the web browser. The page URL is usually printed on the router with the default password. If it's not there, run # netstat -rn and look at the default gateway and try that address. It can be something like 192.168.1.xx. If you don't know the password, try username "admin" and password "password" (I know, very poor choice of credentials from router manufacturers).  If nothing works, contact the internet service provider and ask for the details.

Locate the Firewall settings (usually under settings) and find the page that allows you to make changes to applications, pinholes and DMZ. Select the raspberry pi device and proceed to editing the firewall settings for the device. There should be few applications already listed there, if you see Samba server there, select that and add it as hosted applications. Check what ports were opened by that service. If they are same as above (137, 138, 139, 445),then you are all set. You can now use the public IP of the router to access Samba from anywhere on the internet.

In my case, there was no such Samba server listed in the default applications. So I had to add a new user-defined application under firewall settings. I named it "Samba" and associated it with UDP ports 137, 138 and TCP ports 139 and 445. Then I added Samba to the hosted applications list. Now my RP is open to Samba ports and I can use the samba share from anywhere on the internet.

Access Samba share

The following examples assume my public IP address is 111.222.333.444.

From Unix/Mac OS X:
$ sudo mkdir -p /smb/hdd5
$ sudo mount -t smbfs //pi@111.222.333.444/media /smb/hdd5

In this example "media" is what you wrote under [] at /etc/samba/smb.conf

From Windows:

Type the following in the address bar of your web browser -


File Manager -> Connect to Server

No comments:

Post a Comment