Saturday, November 15, 2014

Using a Raspberry Pi as a Low-Powered Server

If you're thinking about setting up a computer or NAS (network-attached storage) drive as a local server, consider using a Raspberry Pi, instead. The savings alone, both upfront and in electricity costs makes it a viable alternative. They're also really portable and are great for hooking up to wall-mounted screens you have throughout your house. They also don't take as much power to run so you can feel better about them running all day.

Prerequisites: I'll link you to some specific hardware at the end of this article, but here's a rough list:
  • A Raspberry Pi
  • A monitor
  • An HDMI cable
  • A 32GB micro SD card
  • A micro USB power supply
  • A wifi dongle
  • A way to connect directly to your modem through an Ethernet cable
  • A keyboard

First thing you're going to want to do is set up your SD card with a proper OS. I went with Raspbian, but you have a few different options. If you're on a Mac, you can run the following commands. Otherwise, check out this page.

diskutil list
    identify the disk (not partition) of your SD card. e.g. disk4 (not disk4s1)
diskutil unmountDisk /dev/
    e.g. diskutil unmountDisk /dev/disk4
sudo dd bs=1m if=.img of=/dev/
    e.g. sudo dd bs=1m if=2014-09-09-wheezy-raspbian.img of=/dev/disk4

Note: The last process will take a long time. Let it take its course and press CTRL + t from the same shell window at any time to view its progress.

After this process completes, slide the SD card into your Pi, connect your monitor, keyboard, Ethernet cable, WiFi dongle and plug your power supply into it. It will turn on by itself and you'll be presented with the configuration screen. You may return to this screen at any time by running sudo raspi-config.

You'll want to set up a few things at this point:

  • Enable the SSH server (found in the Advanced Options)
  • Expand the root partition so you can utilize all of the space on the SD card.
  • Set the correct timezone.
  • Indicate the boot mode:
    • Enabling boot to desktop will load a GUI-based (Graphical User Interface) operating system
    • Disabling boot to desktop so booting takes you directly to the command line
    • Enabling boot to scratch opens up scratch, directly
  • Reboot your Raspberry Pi

The next thing you're going to want to do is set up WiFi access. First make sure your WiFi dongle is being detected by running lsusb. If it doesn't show up in the output, make sure it's plugged in properly.

Now edit your interfaces file at /etc/network/interfaces

auto lo
iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
auto wlan0

iface wlan0 inet dhcp
   wpa-ssid "Your Network SSID"
   wpa-psk "Your Password"

Now run sudo service networking reload. You may have to restart if the ip address doesn't show up under wlan0 after running ifconfig. SSH into the machine using that inet ip address. You also may have to wait a while to be able to ssh into the machine, again. Make sure your sshd service is still running if all else fails with sudo service sshd start or sudo service ssh start.

If you want to rotate the display 90 degrees for a vertical monitor, add the following line to /boot/config.txt:

display_rotate=3

If you want to keep the raspberry pi from going to sleep, uncomment and update the following line in your /etc/lightdm/lightdm.conf file.

[SeatDefaults]
xserver-command=X -s 0 -dpms

And there you go. You should now be able to disconnect your keyboard and ssh into the machine from any laptop by running ssh pi@<ip address>. Follow this guide to make it easier to ssh into the Pi in the future. You can use your server to ssh into other servers and monitor uptime, tail logs, and a host of other things through the shell. If you open the config screen again, you can also set your Pi to boot into the desktop instead of the shell and run client side applications like Kibana. If you need to leverage a more powerful server, you can set up a machine in the cloud and use the Pi to ssh into it.

Before we wrap up, here's where you can find all of the peripherals:

No comments:

Post a Comment