Monday, May 19, 2014

A new configuration...

I didn't like the set up I had with my raspberry pi on the public wifi network and hard-wired to a private mesh network to the internal private network.  Something was slowing it down and not sure what, but I suspect the private mesh network.

Anyway, I managed to get the PI connected to two wireless networks at the same time.  Here's what I did:

1)  Reformat the SD card with Wheezy
2)  Plug in both wifi dongles into the externally powered USB hub and plug the hub into the pi.  (Note: Make sure the two wifi networks are on different channels.  Not sure what will happen with the channels overlap)
3)  Make /etc/network/interfaces look like this:

auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_clevelandst.conf
iface private inet static
  address 192.168.2.1
  network 255.255.255.0

allow-hotplug wlan1
iface wlan1 inet manual
wpa-roam /etc/wpa_supplicant/wpa_philly.conf
iface public inet dhcp


4) Make /etc/wpa_supplicant/wpa_supplicant.conf:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

source wpa_public.conf
source wpa_private.conf

5) wpa_public.conf:
network={
ssid="..."
id_str="public"
key_mgmt=NONE
auth_alg=SHARED
wep_key0=..
}

6) wpa_private:
network={
ssid="..."
psk="..."
id_str="private"
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP
auth_alg=OPEN
}

7) sudo apt-get purge ifplugd (Note: For some reason, ifplugd would disconnect one of the networks with the other was connected.  Didn't look at config files to see why as this was recommended on raspberry pi forum.)

8) Reboot

9) Now running iwconfig should show both wlan[01] connected and ifconfig should show IP addresses

10) Set up DHCP on private network.  Install dhcp server:
sudo apt-get install isc-dhcp-server

11) Edit /etc/dhcp/dhcpd.conf:
#option domain-name "example.org";
#option domain-name-servers ns1.example.org, ns2.example.org;

default-lease-time 21600;
max-lease-time 43200;

authoritative;

subnet 192.168.1.0 netmask 255.255.255.0 {
}

subnet 192.168.2.0 netmask 255.255.255.0 {
        range 192.168.2.50 192.168.2.75;        
        option routers 192.168.2.1;
        option domain-name-servers 8.8.8.8, 8.8.4.4;
}

12) Edit /etc/default/isc-dhcp-server:
DHCPD_CONF=/etc/dhcp/dhcpd.conf
DHCPD_PID=/var/run/dhcpd.pid
INTERFACES="wlan0"


13) Start DHCP server (it will automatically start at boot, but this step will check for errors in config file):
sudo /etc/init.d/isc-dhcp-server start


14) Try and connect to private network and see that DHCP IP is issued.

15) Update timezone data:
sudo mv /etc/localtime /etc/localtime.old
sudo ln -s /usr/share/zoneinfo/US/Eastern /etc/localtime


16) Set up ip forwarding (aka establish pi as router).  Edit /etc/sysctl.conf and uncomment:
net.ipv4.ip_forward=1

17) Enable the IP forwarding change without rebooting:
sudo echo 1 > /proc/sys/net/ipv4/ip_forward

18) Set up IP Masquerading:
sudo iptables -t nat -A POSTROUTING -o wlan1 -j MASQUERADE
sudo iptables -A FORWARD -i wlan1 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o wlan1 -j ACCEPT

19) Try browsing from the private network.  All should work at this point.

20)  Save iptables
cd
sudo iptables-save > iptables
sudo mv iptables /etc/

21)  Make the file /etc/network/if-up.d/iptables:
#!/bin/sh
iptables-restore < /etc/iptables

22)  Make the file /etc/network/if-up.d/iptables executable
sudo chmod +x /etc/network/if-up.d/iptables

23) Try rebooting and see that you can browse from the private network.

Saturday, November 2, 2013

Initial setup again...

Since I moved to my new house, I find myself having to set up from scratch because my network set up here is different.  I have access to a public WiFi network that I've been connected to on my laptop.  However, I now want to set up a home network but don't want it on the public Wifi, but I want to use the public Wifi to get access to the Internet ie I don't want to have to pay for an Internet connection and instead piggy back off the public WiFi.  I've decided that using my Raspberry Pi is perfect for this.  The Raspberry Pi will act as the DHCP server, and NAT router for my private home network and will route Internet traffic to the public WiFi.  Basically my set up will be as follows:

Public Wifi <-----> Pi <-------> Hub <-------> Private WiFi Access Point <---------> Private Wifi

The public Wifi network is 192.168.1.0 and my private Wifi network is 192.168.2.0.  The public Wifi interface on my Pi is wlan0 and the private wired interface is eth0.

So, To set up my RPi:

I'm not going to describe these first two steps in detail as there is a lot of information out there already.
1)  Make an image of Debian wheezy on a 4GB SD card.
2)  Boot the Pi, log in.

3)  First thing is to set up the public WiFi network.  I bought a nanoUSB WiFi adapter for my Pi.  Edit /etc/network/interfaces (this is assuming WEP encryption on the Public Wifi):

auto lo
iface lo inet loopback

auto wlan0
iface wlan0 inet dhcp
wireless-essid <SSID>
wireless-key <KEY>

iface eth0 inet dhcp

iface default inet dhcp

4)  Reboot.  Run iwconfig and ifconfig to determine if Pi is connected to the public WiFi network.  It should be associated with the SSID and have a valid IP address.

Now that is connected, set up the private Wifi:

We need to set up a static IP address on the private (local) network since Pi will handle DHCP and routing.  Edit /etc/network/interfaces:

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 192.168.2.1
netmask 255.255.255.0
network 192.168.2.0
broadcast 192.168.2.255

auto wlan0
iface wlan0 inet dhcp
wireless-essid <SSID>
wireless-key <KEY>


6)  Pi will also act as a DHCP server on the private network:

sudo apt-get install isc-dhcp-server

7)  Edit /etc/dhcp/dhcpd.conf:

# Comment the following two lines:
option domain-name "example.org";
option domain-name-servers ns1.example.org, ns2.example.org;

# This is authoritative for this network
authoritative;

# Let DHCP know about other networks but don't handle them
subnet 192.168.1.0 netmask 255.255.255.0 {
}

subnet 192.168.2.0 netmask 255.255.255.0 {
  range 192.168.2.50 192.168.2.75;
  option broadcast-address 192.168.2.255;
  option routers 192.168.2.1;
  option domain-name "local";
  # Google name servers
  option domain-name-servers 8.8.8.8, 8.8.4.4;
}

Also edit /etc/default/isc-dhcp-server:

INTERFACES="eth0"

8) Reboot Pi

If all is good, try connecting to private network on another computer.  You should get a DHCP address on the private network.

9)  We need to set up Pi to act as a router.  sudo nano /etc/sysctl.conf and uncomment:
#net.ipv4.ip_forward=1

10) Enable the change, sudo sysctl -p /etc/sysctl.conf

11) Set up masquerading


iptables --table nat --append POSTROUTING --out-interface wlan0 -j MASQUERADE
iptables --append FORWARD --in-interface eth0 -j ACCEPT

Try browsing from the private network to anywhere.  All should be ok.  

12)  Save iptable changes.
iptables-save > /etc/iptables.rules

14)  Load iptable settings on boot.  Edit /etc/network/if-pre-up.d/iptables:

#!/bin/sh

iptables-restore < /etc/iptables.rules

exit 0

15)  chmod +x /etc/network/if-pre-up.d/iptables

16)  Reboot to make sure everything (connectivity) works correctly.  If it does, then all set.

Tuesday, December 4, 2012

XBMC on Raspberry Pi

Now that the initial set-up is done, mostly, I want to now get XMBC running on my RPi so it can play/share the video/music library on my TB Drive.  This way, I can either play/share directly from XBMC or from the Blu-Ray SmartTV player.  The Blu-Ray SmartTV player can read from DLNA sources which XBMC is one.

I think I need to replace the fan component in my TD Drive.  Its making a slightly rattling noise indicating the fan isn't sitting in its case properly, or isn't balanced properly.  This is for another day.

Apparently, it looks like XBMC has been ported to RPi and is called Raspbmc.  The installed insists on download an image that gets written to the SD card.  I guess this is okay.  That's why I'm taking notes and can revert back to what I did and set it up again.  Thankfully, its just the DHCP server.

So, following the instructions for downloading and installing Raspbmc (http://www.raspbmc.com/wiki/user/os-x-linux-installation/), I ran install.py with the SD Card inserted in my Mac.  I then inserted the SD Card into my RPi and turned it on.  I had to re-share my Internet connection on my Mac because my Mac needs to run the DHCP server so Raspbmc can connect to the Internet to self-update itself.  After some downloading and installing, it set itself up.  Pretty cool.  I can even control XBMC through a web browser on another computer, so I don't need a keyboard/mouse connected to my RPi.

I currently share my music/movies using Samba.  XBMC is able to read the Samba share just fine.  I can view movies and hear music through XBMC.  This is great!  Adding the music into the library was not straight-foward.  I had to navigate to the folder then press 'C' on the keyboard to get the context-menu, then select Scan.

The only thing left is to reconfigure RPi to have a static IP address again.  This can be done through the XBMC interface, Programs -> Raspbmc Settings.  It doesn't use the /etc/network/interfaces file.  Instead, it uses NetworkManager, I think.

I'll import the movies tomorrow and work on getting wireless up.  I'd like to get the remote control app on my iPhone, instead of having to use my laptop as a remote control.  I don't have to have the keyboard/mouse plugged in if I don't need to.

Monday, December 3, 2012

Initial Setup

This blog is more for me to take notes and remember what I did than anything else.  Hopefully, you can follow along.

I purchased a Raspberry Pi a while ago, but never got around to setting it up for multiple reasons, one mostly (because I'm cheap) and didn't want to buy a monitor.  Almost a year later, I got a 55" HD TV with an HDMI input.  So now is a good time to start playing with my RPi.

The first thing to know sf the Raspberry Pi is a full computer on a chip.  CPU, Memory, etc, etc.  Its running an ARM processor.  It has no harddrive, keyboard, mouse, monitor, etc, etc.  You need to provide all that.  It doesn't even come with a power supply.   It provides jacks to a couple of things:  2 USB ports, 1 HDMI video, Ethernet, and Composite Video/Sound (which I won't use because I have HDMI).  The beauty is you can install whatever OS you want on the OS, and the RPi will boot off the SD Card.  Linux is the preferred OS (of course), so here goes...

I bought an 8 GB Sandisk SD Card to use to hold the OS, an HDMI cable to connect the RPi to my TV, a USB Keyboard, USB Mouse, and I'm using the microUSB adapter from my old Blackberry.

The first thing I wanted to do was just get a basic linux distro running on it.  Raspian Wheezy is the supported distro as of this writing.   I downloaded the .IMG file and burned it to the SD Card using 'dd'.    Next, I plugged the Rpi into the TV, connected the USB keyboard/mouse, inserted the SD Card, and powered it up.  It booted up beautifully.

Resize SD Card

The SD Card is 8GB, but the image I burned to it is only 2 GB, so the OS doesn't see the rest of the space.  To resize the SD card WHILE running Debian on RPi, run the following commands:

df -h


sudo fdisk /dev/mmcblk0

# p to see current start of the main partition
# d, 3 to delete the swap partition (I don't have a swap partition)
# d, 2 to delete the main partition
# n p 2 to create a new primary partition.  Enter the start of the old main
# partition and then the size (enter for complete SD card).
# w to write the new partition table

sudo shutdown -r now

sudo resize2fs /dev/mmcblk0p2

df -h

Networking 

I really want it to act as a DHCP server for my home network, among other things, so I need to get a DHCP server configured.  I plugged in the RPi into a network switch, and connected my Mac to the same switch.  Nothing else is plugged in to the switch, so its a simple 2 computer network.  I couldn't figure out the network aspect at first since its not like Redhat/CentOS, but I did find this:  In /etc/network interfaces, change the line that reads

iface eth0 inet dhcp

to


iface eth0 inet static
address 192.168.2.3
netmask 255.255.255.0
gateway 192.168.2.1

and reboot.  The RPi should now have the IP address 192.168.2.3.  

I'm using 192.168.2.0 as my network because the Wireless LAN I'm running my Mac on uses 192.168.1.0.

Set up Internet Sharing on the Mac.  It will take on the address of 192.168.2.1.  So RPi should see the Mac as the gateway to the rest of the world.  

At this point I can ssh to the RPi from my Mac.  And my RPi can see the rest of the world.  Yeah!

Next thing to do is run 'sudo raspi-config' and update 'raspi-config'.  This will update the installed packages to the latest versions, then set the correct timezone.


DHCP Server

The next step is to get a DHCP server installed and running so my RPi can manage all the devices on my local LAN.  My old DSL router used to handle this for me.  This also gives me a lot more control on how everything is set up.  Run:

sudo apt-get install dhcpd

Edit /etc/default/udhcpd and set:
DHCPD_ENABLED="yes"

This installs and enables the DHCP server daemon.

Edit /etc/udhcpd.conf:
1.  Set the start of the IP lease block to whatever you want.  I'm using the addresses 100-254 as my lease addresses.  Everything below 100 will be for static addresses of my specific devices aka RAID Storage drive, Wireless Access Point, Blu-Ray player, TV, etc:


start           192.168.2.100   #default: 192.168.0.20
end            192.168.2.254   #default: 192.168.0.254

2.  Set the network specific options (at the bottom of the file):

opt     dns     8.8.8.8 8.8.4.4 # Google Public DNS Servers
option  subnet  255.255.255.0
opt     router  192.168.2.1

3.  Then run:

sudo /etc/init.d/udhcpd restart

4.  I then plugged in another laptop configured to acquire a network address via dhcp, and viola! It worked!

TV / Blu-Ray

Both the TV and Blu-Ray player have NIC cards so I plugged them both into the hub.  They automatically acquired DHCP addresses and could connect to the Internet.

TB Drive

This was previously configured using a static IP address.  I update the network settings and can connect to it.  I gave it a new static IP address but also set up the RPi to give it a static lease.  The static lease is more of a place holder so it never gets used for anything else.