Monthly Archives: May 2015

Sharing files between the Raspberry Pi and Windows 8

In order to share files between the raspberry pi and Windows we need to install samba:

sudo apt-get install samba samba-common-bin

We are going to use a dedicated user, samba, for all file sharing and will be using their home folder als share location. This means we first need a new unix user

sudo adduser samba

In addition, we also need to set up this user as a samba user

sudo sambapasswd -a samba

Now the tricky part. Basically all samba functionality is controlled via one file, smb.conf. In it, we can specify the security and authentication settings and which folders and services to share. After backing up the file

sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.$(date +%Y-%m-%d)

we are going to create a new file with the following info:

sudo rm /etc/samba/smb.conf
sudo vi /etc/samba/smb.conf
[global]
    workgroup = WORKGROUP
    netbios name = pi
    server string = Pi Samba Share
    encrypt passwords = True
    security = user
    socket options = IPTOS_LOWDELAY TCP_NODELAY
    wins support = no

[PiShare]
    comment = Pi Samba Share
    path = /home/samba
    read only = No
    valid users = samba

All that is left to do is restarting the service.

sudo service samba restart

You should now be able to access the share folder by opening a winwows computer in the same network (and workgroup!), and connecting to //pi/PiShare with the user pi\samba and the password you selected above.

Configuring the USB drive

Although the raspberry pi has an 8GB micro SD card, we want to connect an external USB hard drive and use that as main drive for our files. This is both because 8GB are not a lot when we want to store media, but also because SD cards are not the most reliable storage media and we want to reduce the possibility of losing all our stuff.

You can see all drives connected to the server by entering

sudo fdisk -l

This will show the sdcard (/dev/mmblk0pX) with multiple partitions and the USB drive (/dev/sda1). We want to change the settings such that the USB drive is automatically mounted on /home. This will mean that all user data will be stored on the hard drive.

The fstab file contains the mount points for all drives. Back it up before making any changes:

 sudo cp /etc/fstab /etc/fstab.$(date +%Y-%m-%d) 

and then enter the following line:

/dev/sda1       /media/home     ext4    defaults        0       1

and mount the drive in that folder:

sudo mkdir /media/home
sudo mount -a

We're copying everything to the new drive:

sudo rsync -aXS /home /media
sudo diff -r /home /media/home 

The second command compares the two folders and makes sure the copy was successfull. We can now open /etc/fstab again and modify the mount point from /moedia/home to /home. To be on the safe side, it's best to back up the old home folder just in case:

 cd / && sudo mv /home /old_home && sudo mkdir /home 

After that, we only need to remount all drives:

sudo mount -a

and the hard drive shold be mounted in the home folder. If you are convinced that everything went well, just deleted the /old_home folder.
 

Setting up a static local IP

By default, any computer on the local network gets assigned a new (local) IP address everytime they connect to the system. We can check the local IP address by typing

ifconfig

In my case, the local address is 192.168.0.8. Since we will want to connect to the server from other computers on the network without changing the target IP address every time, we can assign a static IP address to the server.

This is done by changing the DHCP settings in your router. Usually, the router settings can be changed through a web interface, in my case this can be reached by typing 192.168.0.1 into a web browser. Set up DHCP reservation such that the server always gets assigned the same IP address. While we are at it, we'll also set up port forwarding to make the server available for SSH outside the network and forward por 22 (the SSH port) to 192.168.0.8.

The server basics: OS, user and ssh

First we are going to set up the basics: an OS (we're going to be using Raspbian), a new user and ssh. Rasbian by default comes with a user pi (password: raspberry) who has full permissions on the system. This is a problem when we connect the raspberry pi to the internet, as anyone could mess around with the system. We could just change the password, but might as well set up a new user and delete "pi" altogether. We are setting up SSH so that we can control the server remotely, both from within the network or any other computer connected to the internet.

After booting the pi for the first time from NOOBS, we select the Raspbian OS. This will set up the operating system and might take a while to run. After the installation is complete, we reboot the raspberry and find ourselves in the "Raspberry Pi Software Configuration Tool". If this does not come up automatically, start it by typing

sudo raspi-config

In the config tool, we are changing a couple of things:

  1. Change pi password: Even though we are later going to set up a new user, let's change the password from raspberry to something more unique.
  2. Boot to console: This will keep Raspbian from booting into the GUI and instead present us with a console terminal window after boot.
  3. Advanced options - Enable SSH: This will allow us to remotely connect to the raspberry pi.
  4. Advanced options - set hostname: We'll change this from "raspberrypi" to "pi" mostly so that the prompt in the terminal is a bit shorter
  5. Advanced options - memory split: Here we can select how much memory the GPU can have. Since we are going to run the raspberry without graphical interface, we'll set this to the lowest possible option, 16.

After updating all the settings, select finish and reboot the computer. Once prompted, login with the user "pi" and the password we just updated.

We're now going to set up a new user with the same rights as the current pi user. Type

groups

to see a list of groups the user pi belongs to. In my case, this is

pi adm dialout cdrom sudo audio video plugdev games users netdev gpio i2c spi input

In order to add a new user called phil with the same group memberships, type

sudo useradd -m -G adm dialout cdrom sudo audio video plugdev games users netdev gpio i2c spi input phil

and set the password for this user:

sudo passwd phil

After entering the password twice, reboot the computer again:

sudo reboot

Once the raspberry pi has finished it's boot process, login as the new user. The last thing to do is deleting the old pi user account:

sudo deluser --remove-all-files pi

After a while the computer should print "Done." indicating that the user pi and all their files have been removed.

Setting up a raspberry pi as home media server

I recently got my hands on a Raspberry Pi Model B+ V2 and am going to play around with it over the next couple of weeks.

The goal is to set up the following:

  • Set up the pi as a headless server in the home network
  • Mount an external USB hard drive
  • Install samba
  • Setup a VPN
  • Install a torrent daemon
  • Install Sonarr
  • Install Seafile (or other self-hosted storage solution for remote access).

I'll be adding a post for each of these and will be starting off with a clean SD card flashed with the most recent NOOBS version (currently 1.4.1 obtained from here).