pi sampler

12
EVERYTHING YOU NEED TO KNOW… Tips | Tweaks | Fixes | Hacks | Advice Build stuff Write code Have fun Learn Linux Get started Turn your Pi into a games console and media centre Master the fine art of telling the Pi what to do Fire the imagination with our guides and tutorials Dive into the world’s best operating system Our non-jargon approach is perfect for beginners Raspberry Pi: The Essential Manual

Upload: future-plc

Post on 09-Mar-2016

230 views

Category:

Documents


0 download

DESCRIPTION

http://www.myfavouritemagazines.co.uk/computing-bookazines/raspberry-pi-the-essential-manual/

TRANSCRIPT

Page 1: Pi sampler

everything you need to Know…

Tips | Tweaks | Fixes | Hacks | Advice

Build stuff write codehave fun Learn LinuxGet startedTurn your Pi into a

games console and media centre

Master the fine art of telling the Pi

what to do

Fire the imagination with our guides

and tutorials

Dive into the world’s best operating

system

Our non-jargon approach is perfect

for beginners

Raspberry Pi:The Essential Manual

Rasp

berry P

i: Th

e Essen

tial Man

ual

TuxRadar.com CBN52 2013PRINTED IN THE UK £12.99

Page 2: Pi sampler

Contents

4

Get started

TutorialsCode with Scratch ............................................................................................46Network tunnelling with SSH ............................................................... 52Build an IRC chatbot .......................................................................................56Emulate vintage games machines ................................................60A clutch of projects for rainy days ..................................................64Assemble your own Raspberry Pi distro .................................72Serve files to your smartphone ..........................................................76Get started with programming ..........................................................80Build a media centre .......................................................................................84

ProgrammingWhat is the Raspberry Pi? ...........................................................................8Get connected ........................................................................................................12Install from Windows, Mac OS X and Linux .........................16First boot .......................................................................................................................22Introducing the command line ...........................................................24The Raspbian config tool ...........................................................................28Storage and the filesystem .....................................................................32The Raspbian desktop .................................................................................34How to install software ................................................................................36Default applications .........................................................................................38

ALERT!We’ve put all the code for all the tutorials online. Just download and unzip the following archive: www.linuxformat.com/files/rpi2013.zip

CBN52.contents.indd 4 9/4/13 11:52 AM

Page 3: Pi sampler

Contents

5

Anatomy of Linux ............. ..............................................................................122Hardware ..................................................................................................................130The boot process ...........................................................................................134Filesystem, partitions and libraries ..........................................138Package management ............................................................................142The command line ........................................................................................146Advanced shell ..................................................................................................150Processes and Vi ............................................................................................154Links, permissions and quotas .....................................................158

Programming

LPI

Python primer ......................................................................................................90Build a chatbot ......................................................................................................94Connect to Twitter ...........................................................................................98Use XML and Digg .........................................................................................102Access Flickr ........................................................................................................106Geolocation.............................................................................................................110OAUTH interaction .........................................................................................114

CBN52.contents.indd 5 9/4/13 11:52 AM

Page 4: Pi sampler

28

Get started!Get started!

Now that you’ve acquired all the connectors needed to get your Raspberry Pi plugged in, and got the operating system transferred onto an SD card and

installed, you’re ready to go. But for a little bit of extra effort

Setup: Run the config toolYour Raspberry Pi is a wonderfully versatile machine, so let’s look at some of its options and use it to its full potential.

1 Menu listThe menu screen lists the steps that are available to us as we configure our Raspbian installation. You don’t have to change them all, and you don’t have to do them in any order, but we’re going to step through them one by one. Use the arrow keys to move up and down the list, and the Enter key to make your selections.

2 Expand fi lesystemIf you followed the default Raspbian partitioning schema, your operating system will occupy roughly 1.6GB of space on your SD card, which is why a minimum of 2GB is recommended. But if you’ve bought a bigger card and you want to use the space you’ve paid for, use this option to expand the filesystem to fit your SD card.

Step by step: Configuration

you can configure the system to your needs, giving you a much more useful and personalised setup.

The tool you use to do this is called raspi-config, and is included in the basic Raspbian distribution, so you don’t need to install it before you use it.

On the first run, it will load automatically, but if you’ve skipped past it, or you just want to go back and use it later on, you can get to it by opening a terminal window and typing sudo raspi-config. You’ll be prompted for a password, which at first will be ‘raspberry’.

To get to a terminal, you can either double-click on the LXTerminal logo in the bottom-left of the desktop, or press Ctrl+Alt+T on your keyboard to achieve the same thing.

One of the brilliant things about the Raspberry Pi is that if you’re unsure about any of the steps involved in using the configuration tool, you can leave them at their default setting and everything will still work; on the other hand, if you do make a mistake you can always go back and fix it later, which, as you’ll find with Linux, is still the best way to learn.

CBN52.start_ras_conf.indd 28 9/4/13 12:08 PM

Page 5: Pi sampler

29

Get started! Get started!

Setup: Run the config tool

3 OverscanThe Raspberry Pi is meant to be plugged into a television set or a monitor. Depending on what device you’re using, there can sometimes be a thick black line around the edge of the screen. Toggle the overscreen option to get rid of this.

5 Keyboard layoutThe QWERTY keyboard layout was invented to break up the most frequently used pairs of letter, so that mechanical keyboards wouldn’t get snagged by typists writing too quickly. It’s a throwback to the old days of typewriters, but most people using this guide will still want to stick with English (UK).

4 Identify keyboardAs it doesn’t come with a keyboard, the Pi has to be able to respond appropriately when one gets plugged in. It will give you a list of keyboard makes and models, and it’s up to you to identify the one you’re using. We just chose Dell as a generic option.

6 Modifier keyIf you use foreign characters a lot, (such as ã, ä, è, ë, õ, û and ž), you may want to specify your own choice of key to access them. Otherwise, just go with the default for the model keyboard you’re using.

CBN52.start_ras_conf.indd 29 9/4/13 12:08 PM

Page 6: Pi sampler

56 

Tutorials

with a tool such as dd or the Fedora ARM Installer (which is compatible with any disc image you throw at it). Make sure you’ve resized the partitions on your card so that you’re making the most of all the available space.

Since we won’t need LXDE or any other GUI to run an IRC server, it’s safe to start by making sure we can run our server headlessly – that is, without a monitor or input peripherals of its own. Under Raspbian, SSH and networking will be running by default unless you’ve switched them off previously, so you should be able to find the IP address of your Raspberry Pi through your router. If this is the case, you can leave all of the ports (bar the network and power) empty before moving on to the next step.

If you can’t find the address by other means, plug in a screen and keyboard for the initial setup procedure. When boot has completed, log in with the usual credentials (username pi, password raspberry), exit the initial configuration application if it pops up, then try pinging a site on the internet to make sure you’ve got an active network connection. Then, to discover your RPi’s local address, type ip addr in to the Raspberry Pi shell. Write it down – you’ll need it for the next step.

Setting upGo to a machine you’re comfortable using – it doesn’t matter what OS it’s running – and open a terminal window. If you’re using a Windows client, you’ll find it doesn’t come with SSH by default, so get hold of PuTTY from www.chiark.greenend.org.uk/~sgtatham/putty and use that for this process. Connect to the user ‘pi’ at the local IP address you just wrote down by typing, for example, ssh [email protected], or use PuTTY to connect to the address using the SSH protocol and port 22.

If you’re asked for a password at this point, you’re connected. We’re going to presume that you’ll keep your Raspberry Pi running from now on; if it’s going to undergo frequent reboots, its address on the network may change, so make sure you set up a static IP address if this is the case. You’ll find a full guide to doing this at http://elinux.org/RPi_Setting_up_a_static_IP_in_Debian.

The installLog in to the RPi using the usual details, then begin the process of installing the required packages on the system. Run: sudo apt-get update

to update to the most recent package directory, then run:sudo apt-get install ircd-hybrid

to get the latest ARM-compiled version of the base Hybrid IRC server. It creates a new user account – irc – with limited privileges, and runs itself as that user immediately. It’s by far the easiest IRC server we’ve come across; there are other options, of course – things such as inspircd, ratbox, dancer

Server: Chat with IRCAlex Cox creates an IRC network from scratch and gives it some rather insane life, all in just 224MB RAM.

You probably think the Raspberry Pi is a learning tool. Or maybe you’ve got it pegged as nothing more than a toy. You’d be right, in a basic way, on both counts.

It’s perfect for both of those uses. We’ll embrace them here: we’re going to have fun and we’re going to learn. But we’re also going to prove that the RPi has chops as something more: a (very) low-power server. Most of these techniques will work with any internet-connected box, so you won’t miss out even if you haven’t yet got your hands on a Pi.

We’re going to install an IRC server – a classic, undemanding application which will allow others to connect to your Raspberry Pi and have a good chat – as well as many of the accompanying tools that run alongside it. That’s everything from channel, nickname and memo control to nifty expandable bots that will sit on your server, keeping channels alive, learning – in a very vague sense – and talking nonsense to visitors.

Getting startedWe’d recommend kicking off with a completely clean installation of Raspbian, the RPi skew of Debian Linux. We’ve chosen this mainly because the software you’ll need has

already been tested under Debian for other ARM platforms, such as the Squeezebox, so there won’t be any problems with compatibility. You can download the disk image for Wheezy through the official Raspberry Pi website (www.raspberrypi.org/downloads).

We’ve covered installation in previous Raspberry Pi tutorials, so consult those for a more thorough explanation; but briefly you’ll need to write the disc image to an SD card

Make sure you put an absolute path in the TCL file for egg-fu. The line you need to edit is right at the top.

“You can download the disk image for Wheezy through the official Raspberry Pi site.”

CBN52.tut_163.indd 56 9/4/13 12:38 PM

Page 7: Pi sampler

57

Tutorials

About Eggdrop

and rage – but none of them get you up and running with the same speed. Check it out for yourself: install the package irssi (a terminal-based IRC client), run it, and type /join 127.0.0.1 to connect to the IRC server your Raspberry Pi is now hosting. Type /exit to jump back to the command line.

The next step is a long, tedious one, and we can’t help you with it in any significant detail. You’ll need to edit the Hybrid configuration file, feeding it all the pertinent information about your network. First of all, for some reason, Hybrid puts its configuration files in a folder which denies you access by default, so run: sudo chmod 755 /etc/ircd-hybrid/

to open it up. Then use:sudo nano /etc/ircd-hybrid/ircd.conf

(or your favourite text editor) to start editing; everything you need to tweak is well documented within the file, so it should be fairly plain sailing. Make sure you read it, as there’s a line that will need to be uncommented before the server will run. Pay special attention to the Operator section; this is what defines the people of real power on your IRC server. Ideally, this will be you, of course. Save and exit the configuration file for now, and run: mkpasswd <your intended pass>

to generate an encrypted password. Back in the config file, find the Operator section. Change the user line to read user = “*@127.0.0.1 – this means that only someone logged on to the IRC server from this local machine can get true operator privileges. Paste the encrypted password you just generated in the password slot.

Service industryNow we need to install the services package. Sadly, there’s no support in Wheezy for hybserv, the default ircd-hybrid services engine. In compiling this tutorial, we tried repeatedly to compile it from source, but it was a fruitless task. Instead, we turn to a third-party services component: Anope. We’ll need to compile this one from source as well, but at least it works.

Start by making a fresh directory in your home folder – something like anope – and downloading the Anope archive, using: wget http://sourceforge.net/projects/anope/files/anope-stable/Anope%201.8.7/anope-1.8.7.tar.gz.

Expand the archive with: tar xvfz anope-1.8.7.tar.gz

and change to the directory that’s created. There’s a configuration script built in to Anope. Type ./Config to run it, and consider the options it presents carefully. Realistically, there’s every chance that you can just hit return a few times and be done with it, but it’s worth making sure you’re picking the right options for you. Once the config script has run through, type make and head for another cup of tea. Run: sudo make install

once it’s finished. Time for some more configuration file editing. There’s a lot to set, if you’re inclined, but for our purposes we can tweak a few values just to get Anope up and running. Type cd ~/services to get to the Anope install directory, and run: nano example.conf

to open the sample configuration file. Scroll through and uncomment the IRCDModule flag, setting it to ‘hybrid’ so that Anope knows what IRCd it’s communicating with. Set a strong password in the Remote Server section, and note it down because you’ll need it when doing final Hybrid configuration. Set the network name and the numeric to be the same as you’ve given your Hybrid installation, and set your oper nickname in the ServicesRoot field. Save the file as services.conf. Now go to www.anope.org/ilm.php?p=lm

Server: Chat with IRC

Chat with your bot and you’ll soon see the results. Get more people in and talk to them so the bot will learn more and more.

Eggdrop should probably have gone rotten by now. It’s almost 20 years old, having been developed in 1993 to safeguard channels (initially Efnet’s #gayteen) from takeover attempts and general griefing.

It’s got powers beyond mucking about with insane AI bots; you can use it to auto-kick people who don’t behave properly, use it to maintain a ban list, and even protect from the ill-effects of netsplits, when one physical server on an IRC network loses contact with

another. The party line is also a vitally important part of an Eggdrop bot. Even if an IRC network goes down, bot operators can hook up via telnet and talk privately. Realistically, you could run an Eggdrop without even connecting it to an IRC network at all, if all you want to do is set up a confidential chat system, although it lacks the flexibility of IRC so it’s best used as a standby. Find out more about its current development at: www.eggheads.org.

CBN52.tut_163.indd 57 9/4/13 12:38 PM

Page 8: Pi sampler

88

Programming

CBN52.code_intro.indd 88 9/4/13 12:56 PM

Page 9: Pi sampler

89

Programming

Python primer ......................................................................................................90Build a chatbot ......................................................................................................94Connect to Twitter ...........................................................................................98Use XML and Digg .........................................................................................102Access Flickr ........................................................................................................106Geolocation.............................................................................................................110OAUTH interaction .........................................................................................114

ProgrammingThe Raspberry Pi was designed to help

improve the quality of computing education in schools, and programming is a big part of

that. In this section, we’ll take you through the basics of programming using Python, a language that comes as standard with the Raspbian distro.

CBN52.code_intro.indd 89 9/4/13 12:56 PM

Page 10: Pi sampler

122

School of Linux

Neil Bothwick delves deep into the world’s best operating system and finds out what makes it tick.

What is an OS?What is a distro?

The word “Linux” is one of the most used in Linux Format magazine, but what does it mean? It means different things to different people, from the purist who considers it to be the kernel to

the GNU advocate who sees it as a part of GNU/Linux and the new user who thinks it is another name for Ubuntu. In truth, Linux is all of these, depending on your point of view. Strictly speaking, the term Linux used alone refers to the kernel of the operating system, while GNU/Linux is the whole operating system, comprising the Linux kernel and GNU tools – either would be useless without the other (or one of its alternatives).

If you then add a collection of application software, along with some tools to manage the whole thing, you have a distro, such as Ubuntu.

Over the years, Linux Format has covered just about all of the individual components that make up the operating system we know as Linux, but they are not that individual – they all have to fit together, so here we will try to explain how the whole is the sum of its parts, and what those parts do.

A n operating system can be defined as the software needed to enable the applications to run on the hardware

– as such, it consists of several interleaved layers. At the heart we have the kernel, that interacts directly with the hardware through its drivers and allows other software to use that hardware. On top of that, we have various layers that handle things like input devices, networking, sound and video.

Normally, you don’t need to know anything about this. It can be helpful to know some of it when things go wrong, but even then it is not essential, especially if you can find someone to fix the computer for you. But if you are reading this bookazine, there is a good chance you are interested in what is happening ‘down below’, so we will try to give you an idea of what goes where, and what it does when it gets there.

A Linux distribution is just that, a way of distributing a Linux-based operating system and accompanying software. At the start, it was just the files the OS needs and a way of installing them to your computer. Along the way, distros acquired package managers, update tools, configuration GUIs and a host of other niceties. However, underneath the user-friendly (or not if you are a Gentoo user) gloss, all distros are still Linux.

What isLinux?

CBN52.lpi_annatomy.indd 122 9/5/13 12:58 PM

Page 11: Pi sampler

123

School of Linux

The number of options when building a kernel is truly staggering, and the majority of them relate to hardware device support. Aren’t you glad we have distro maintainers to work it all out for us?

The kernelThe nerve centre at the heart of your Linux operating system.

with the filesystem module needed to mount it. Connect a 3G dongle, and the serial modem drivers are loaded. This is why it is rarely necessary to install new drivers when adding hardware, they’re all there just waiting for you to buy some new toys to plug in.

Computers that are run on specific and unchanging hardware, such as servers, usually have a kernel with all the required drivers compiled in and module loading disabled, which adds a small amount of security.

If you are compiling your own kernel, a good rule of thumb is to build in drivers for hardware that is always in use, such as your network interface and hard disk filesystems, and build modules for everything else.

Even more modulesThe huge number of modules, most of which are hardware drivers, is one of the strengths of Linux in recent years – so much hardware is supported by default, with no need to

download and install drivers from anywhere else. There is still some hardware not covered by in-kernel modules, usually because the code is too new or its licence prevents it being included with the

kernel (yes ZFS, I’m looking at you). The drivers for Nvidia cards are the best known examples.

Usually known as third-party modules, although Ubuntu also refers to “restricted drivers”, these are installed from your package manager if your distro supports them. Otherwise, they have to be compiled from source, which has to be done again each time you update your kernel as they are tied to the kernel for which they were built.

There have been some efforts to provide a level of automation to this, notably DKMS (Dynamic Kernel Module

Support), which automatically recompiles all third-party modules when a new kernel is installed, making the process of upgrading a kernel almost as seamless as upgrading user applications.

Phrases that you will see bandied about when referring to kernels are “kernel space” and “user space”. Kernel space is memory that can only be accessed by the kernel, no user programs (which means anything but the kernel and its modules) can write here, so a wayward program cannot corrupt the kernel’s operations. User space, on the other hand, can be accessed by any program with the appropriate privileges. This contributes towards the stability and security of Linux, as no program, even running as root, can directly undermine the kernel.

The kernel is the beating heart of the system, but what is it? The kernel is the software interface to the computer’s hardware. It communicates with

the CPU, memory and other devices on behalf of any software running on the computer. As such, it is the lowest-level component in the software stack, and the most important. If the kernel has a problem, every piece of software running on the computer shares in that problem.

The Linux kernel is a monolithic kernel, that is, all the main OS services run in the kernel. The alternative approach is a microkernel, where most of the work is done by external processes, with the kernel doing little more than co-ordinating.

While a pure monolithic kernel worked well in the early days, when users compiled a kernel for their hardware, there are so many combinations of hardware nowadays that building them all into the kernel would result in a huge file. So the Linux kernel is now modular, the core functions are in the kernel file (you can see this in /boot as vmlinuz-version) while the optional drivers are built as separate modules in /lib/modules (the .ko files in this directory).

For example, Ubuntu 12.10’s 64-bit kernel is 5MB in size, while there are a further

3,700 modules occupying over 100MB. Only a fraction of these are needed on any particular machine, so it would

be insane to try to load them all with the main kernel. Instead, the kernel detects the hardware in use and loads the relevant modules, which become part of the

kernel in memory, so it is still monolithic when loaded even when spread across thousands of files. This enables a system to react to changes in hardware. Plug in a USB memory stick and the usb-storage module is loaded, along

“So much hardware is supported, with no need to download drivers.”

CBN52.lpi_annatomy.indd 123 9/5/13 12:58 PM

Page 12: Pi sampler

Get online…… and get more from Linux Format!

www.linuxformat.com

Download complete issues of the magazine Grab the latest LXFDVDs via BitTorrent Get new issues as soon as they go to press!

Get help from other readers Discuss the latest Linux news Read articles from previous issues Follow the Team LXF blog Or just pop in for a chat!

Don’t miss our podcast: www.tuxradar.com

Subscribe to Linux Format and get exclusive extras:

Hear the Linux Format team chew over the

hottest topics in free software!

TuxRadar

CBN52.tuxradar_ad.indd 162 9/5/13 1:29 PM

9000