Raspberry Pi Setup

From Wiki
Jump to: navigation, search

This describes the complete setup of a Raspberry Pi.

Preparation

At minimum you will require:

  • A Raspberry Pi
  • A micro USB lead and a powered hub, computer, or a micro USB charger capable of supplying at least 800mA
  • An SD card of at least 2GB
  • A network connection and a working DHCP server
  • A non conductive surface upon which to place the Raspberry Pi

It's nice to have:

  • A display capable of accepting HDMI input
  • A USB keyboard
  • A case or project box to house the Raspberry Pi

Obtaining the Operating System

Fetch the latest version of Raspbian, a release of Debian designed for the Raspberry Pi. The latest version can usually be found here: http://downloads.raspberrypi.org/images/raspbian/

This image must be unzipped and written to the SD card. I will be using a Mac to do this, but there are many other ways of doing it. Plug the SD card into your card reader. If the Mac complains that the disk can't be read, choose 'Ignore'. If the Mac automatically mounts any partitions on the card, use Disk Utility to unmount (not eject) them or you will have problems.

From the terminal, run:

$ diskutil list

to find the device name of the SD card. It will be something like /dev/diskn. If you're not sure, run diskutil list before and after inserting the SD card to make sure. Please be certain that you have the right disk name, otherwise you could overwrite the wrong disk with disastrous consequences! Now, use dd to write the image to the card. Note that the device becomes /dev/rdiskn*:

$ sudo dd if=/home/user/path/to/image.img of=/dev/rdiskn bs=1024 ; echo Done | mail -s "Your SD card is done" your@email.addr

* rdisk is a different, faster special device for this type of disk operation

This will take a while, how long depends on the speed of the SD card. You will receive an email notification when it's finished, provided you have a working mailer and firewall access etc. Eject the disk and insert it into the Raspberry Pi's SD slot.

Booting

Plug in the Raspberry Pi's network connection and power it on. sshd is enabled by default in Raspbian, so provided you can discover its IP address, you can ssh to it with a username of 'pi' and a password of 'raspberry'. Otherwise, connect a display and keyboard, then log in locally.

raspi-config will run at the first boot, which will give you the opportunity to set up some commonly changed features of the Raspberry Pi. It can be called by just running sudo raspi-config at any time. At minimum, I would set the following:

  • expand-rootfs: Expand the filesystem to fit the whole SD card (good idea for cards over 2GB)
  • overscan: If you're using a monitor/TV and there's a black border around it, disable this
  • change_timezone: Self explanatory
  • memory_split: For a server it should be 240:16 to give as much memory to the CPU as possible

Reboot the Raspberry Pi when prompted.

Slimming Down

Raspbian comes with a pretty sensible selection of packages installed by default, but it doesn't leave much room for manoeuvre on a 2GB card. Provided that you don't want a GUI or any other unnecessary trimmings, here's a list of packages that I've found can be uninstalled if desired. There is no need to do this unless you are desperately short of disk space:

$ sudo apt-get --purge remove alsa-base alsa-utils aptitude aptitude-common \
aspell-en binutils blt cifs-utils console-setup console-setup-linux consolekit dbus \
dbus-x11 debian-reference-common debian-reference-en desktop-base \
desktop-file-utils dictionaries-common dillo dpkg-dev fakeroot \
fontconfig fontconfig-config fuse gconf2 gconf2-common gdb gksu \
gnome-themes-standard idle idle-python2.7 idle-python3.2 idle3 \
info leafpad libasound2:armhf libaspell15 libasyncns0:armhf \
libatasmart4:armhf libatk1.0-0:armhf libaudit0 libavahi-client3:armhf \
libavahi-common3:armhf libboost-iostreams1.46.1 libboost-iostreams1.48.0 \
libboost-iostreams1.49.0 libboost-iostreams1.50.0 libcaca0:armhf \
libcairo-gobject2:armhf libcairo2:armhf libck-connector0:armhf \
libcolord1:armhf libcroco3:armhf libcups2:armhf \
libcurl3:armhf libcwidget3 libdatrie1:armhf libdbus-1-3:armhf \
libdbus-glib-1-2:armhf libdconf0:armhf libdevmapper-event1.02.1:armhf \
libdevmapper1.02.1:armhf libdirectfb-1.2-9:armhf libdrm2:armhf \
libept1.4.12 libevent-2.0-5:armhf libexpat1:armhf libffi5:armhf \
libflac8:armhf libfltk1.3:armhf libfm-data libfm-gtk-bin libfm-gtk1 \
libfm1 libfontconfig1:armhf libfontenc1:armhf libfreetype6:armhf \
libfuse2:armhf libgail-3-0:armhf libgail18:armhf libgconf-2-4:armhf \
libgdk-pixbuf2.0-0:armhf libgeoclue0 libgfortran3:armhf libgif4 \
libgksu2-0 libgl1-mesa-glx:armhf libglade2-0 \
libglapi-mesa:armhf libglib2.0-0:armhf libgmp10:armhf \
libgnome-keyring0:armhf libgomp1:armhf libgssglue1:armhf \
libgstreamer-plugins-base0.10-0:armhf libgstreamer0.10-0:armhf \
libgtk-3-0:armhf libgtk-3-bin libgtk-3-common libgtk2.0-0:armhf \
libgtk2.0-common libgtop2-7 libgudev-1.0-0:armhf libhunspell-1.3-0:armhf \
libice6:armhf libicu48:armhf libid3tag0 libimlib2 libiw30:armhf \
libjasper1:armhf libjavascriptcoregtk-1.0-0 libjavascriptcoregtk-3.0-0 \
libjbig2dec0 libjson0:armhf liblapack3 liblcms2-2:armhf \
libldap-2.4-2:armhf liblightdm-gobject-1-0 liblvm2app2.2:armhf libmad0 \
libmagic1:armhf libmenu-cache1 libmikmod2:armhf libmpc2:armhf \
libmpfr4:armhf libmtdev1:armhf libnfsidmap2:armhf libnl-3-200:armhf \
libnl-genl-3-200:armhf libnotify4:armhf libobrender27 libobt0 \
libogg0:armhf libopenjpeg2 liborc-0.4-0:armhf libpango1.0-0:armhf \
libparted0debian1:armhf libpci3:armhf libpciaccess0:armhf \
libpcsclite1:armhf libpixman-1-0:armhf \
libpng12-0:armhf libpolkit-agent-1-0:armhf libpolkit-backend-1-0:armhf \
libpolkit-gobject-1-0:armhf libportmidi0 libproxy0:armhf libpulse0:armhf \
libpython2.7 librsvg2-2:armhf librtmp0:armhf libsamplerate0:armhf \
libsasl2-2:armhf libsdl-image1.2:armhf libsdl-mixer1.2:armhf \
libsdl-ttf2.0-0:armhf libsdl1.2debian:armhf libsgutils2-2 libsm6:armhf \
libsmpeg0:armhf libsndfile1:armhf libsoup2.4-1:armhf libsqlite3-0:armhf \
libssh2-1:armhf libstartup-notification0 libsystemd-login0:armhf \
libtalloc2:armhf libthai0:armhf libtiff4:armhf libtirpc1:armhf \
libts-0.0-0:armhf libunique-1.0-0 libvorbis0a:armhf libvorbisenc2:armhf \
libvorbisfile3:armhf libvte9 libwbclient0:armhf libwebkitgtk-1.0-0 \
libwebkitgtk-3.0-0 libwebp2:armhf libwnck22 libx11-6:armhf \
libx11-xcb1:armhf libxapian22 libxau6:armhf libxaw7:armhf \
libxcb-glx0:armhf libxcb-render0:armhf libxcb-shm0:armhf \
libxcb-util0:armhf libxcb1:armhf libxcomposite1:armhf libxcursor1:armhf \
libxdamage1:armhf libxdmcp6:armhf libxext6:armhf libxfixes3:armhf \
libxfont1 libxft2:armhf libxi6:armhf libxinerama1:armhf \
libxkbfile1:armhf libxklavier16 libxml2:armhf libxmu6:armhf \
libxmuu1:armhf libxpm4:armhf libxrandr2:armhf libxrender1:armhf \
libxres1:armhf libxslt1.1:armhf libxss1:armhf libxt6:armhf \
libxtst6:armhf libxxf86vm1:armhf lightdm lightdm-gtk-greeter \
lxappearance lxde-common lxde-icon-theme lxmenu-data \
lxpolkit lxrandr lxtask lxterminal man-db menu-xdg midori mime-support \
mupdf netsurf-gtk nfs-common obconf omxplayer openbox pciutils \
pcmanfm penguinspuzzle policykit-1 python python-support python2.7 \
python2.7-minimal python3 python3.2 python3.2-minimal rpcbind samba \
sgml-base shared-mime-info squeak-vm tasksel tcl8.5 tk8.5 tsconf udisks \
wireless-tools wpasupplicant x11-common xarchiver xinit xml-core \
xserver-xorg xserver-xorg-core aptitude-common fonts-freefont-ttf \ 
libavahi-client3:armhf libavahi-common-data:armhf \
libavahi-common3:armhf libcups2 libsqlite3-0 lua5.1 luajit manpages-dev \
sgml-base strace tdb-tools xdg-utils && \
sudo apt-get --purge auto remove && sudo apt-get clean

This, plus Apache, MySQL, PHP and a few other little bits got my system down from 1.5GB to around the 700MB mark.

Securing

Log in with the user pi and password raspberry. If, like a normal person, you prefer to use vi to edit text, set it as your editor:

$ sudo apt-get update && sudo apt-get install vim && sudo update-alternatives --set editor /usr/bin/vim.basic

Create yourself a new user and give it sudo capability:

$ sudo adduser myuser
$ sudo visudo

# Look for this section
pi ALL=(ALL) NOPASSWD: ALL
myuser ALL=(ALL) NOPASSWD: ALL

Now log out and log in as your new user. Delete the pi user and remove it from the sudoers list:

$ sudo userdel pi
$ sudo groupdel pi
$ sudo visudo

# Look for this section
pi ALL=(ALL) NOPASSWD: ALL
# ^ Delete this line
myuser ALL=(ALL) NOPASSWD: ALL

I'm not sure if the root user even has a password set in Raspbian, but let's make sure it can't log in through ssh anyway:

$ sudo vi /etc/ssh/sshd_config

Change

PermitRootLogin yes

To

PermitRootLogin no

Restart SSH

$ sudo service ssh restart

A basic firewall is easy to add.

Create /etc/iptables-rules

# vi /etc/iptables-rules

*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -i lo -j ACCEPT 
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT 
-A INPUT -i eth0 -p tcp -m tcp --dport 22 -j ACCEPT 
-A INPUT -i eth0 -p tcp -m tcp --dport 443 -j ACCEPT
-A INPUT -i eth0 -p tcp -m tcp --dport 80 -j ACCEPT
# Allow ICMP packets necessary for MTU path discovery
-A INPUT -p icmp --icmp-type fragmentation-needed -j ACCEPT
# Allow echo request
-A INPUT -p icmp --icmp-type 8 -j ACCEPT
-A INPUT -j DROP 
COMMIT

Apply the firewall right now:

# iptables-restore < /etc/iptables-rules

Make sure that the firewall is applied when the network interface is brought up at boot:

# vi /etc/network/interfaces

# Remove any existing mention of eth0 and replace with:
auto eth0
iface eth0 inet dhcp
 pre-up /sbin/iptables-restore < /etc/iptables-rules

Performance or Power Saving

Amazingly, the Raspberry Pi includes support for overclocking and underclocking the CPU and even frequency scaling while the system is running. It isn't enabled by default.

Maximum Speed

Easy enough. Edit /boot/config.txt and insert the following values:

# vi /boot/config.txt
force_turbo=1
arm_freq=1000

Reboot your Raspberry Pi and it will now run at 1GHz all the time.

Power Saving

If you want to take advantage of the Raspberry Pi's power saving features, edit /boot/config.txt and insert the following values:

# vi /boot/config.txt
force_turbo=0
arm_freq_min=100
# You could allow it to scale up to 1000 here, but I don't.
#arm_freq=1000

Reboot and install this package, which allows control over the CPU speed:

$ sudo apt-get install cpufrequtils

Finally, set the 'ondemand' governer, which will let the CPU range from 100MHz to 700MHz according to how busy it is:

$ sudo cpufreq-set -g ondemand

Take an Image

When you're happy with the way the Raspberry Pi is set up, it's dead simple to take a backup image of it using dd. Because dd does a block level copy of the SD card, you could end up with an unnecessarily large backup. It's easy to prevent this by filling the free space with zeroes before we shut it down. This has the effect of 'flushing out' the garbage data:

$ sudo -s
# cat /dev/zero > /zeroes ; rm /zeroes ; shutdown -h now

  cat: write error: No space left on device
  ...
  The system is going down for system halt NOW!

So, plug your SD card into your Mac, and as before use Disk Utility to make sure any partitions are unmounted. Next, use dd to read in the card and gzip to compress it as we go along:

$ sudo dd if=/dev/rdisk1 bs=1024 | gzip > ~/rpi.img.gz

This will create an image of your pi called rpi.img.gz in your home directory. Because we went to the trouble of zeroing the free space, the compressed image will be much smaller than 2GB (or whatever the size of the SD card was).