Still have a working computer? Good. Now you can stick the SD card in the pi and have another working computer! First though, if we want to be able to do anything useful with it, we need to connect it to the network. The pi’s default networking setup sets it to wait for a piece of software called a DHCP server to give it details about the network it is going to connect to. If you have a standard home network, your router is the DHCP server. If you are on a larger corporate network you might have a standalone DHCP server. In either case, just plug the pi in with a standard network cable, and when the pi is turned on, the DHCP server will do its magic.
If you don’t have a router or a spare network socket on it, a couple of clicks will set up your computer as a DHCP server and share its internet connection with the pi. To do this you need to plug the pi directly into the network socket on your computer
using a crossover cable It has been pointed out that the raspberry pi’s have auto-sensing ethernet ports and so you do not need a crossover cable.
If you look closely at the two cables above, you can see that in first cable (the blue one) the colours of the wires are the same on both ends, but on the crossover cable (the grey one) some of the wires in are different positions (namely 1,2,3 and 6). If this were for a gigabit connection, all 8 wires would have to be crossed, but as the pi doesn’t support gigabit speeds, this doesn’t matter to us (nb a gigabit crossover cable is backwards compatible with the older, slower connections). The first cable, often called a patch cable, is used when connecting a computer to a hub/switch/router etc., the second, the crossover cable, is used when connecting a computer to a computer, or a hub/switch/router to a hub/switch/router (nb lots of modern devices can auto sense what sort of connection you have so a crossover cable isn’t always needed anymore).
If you need to setup a shared connection to your pi, as just discussed, do it now by clicking on the networking icon in the top right hand of your screen (its exact look depends on what theme you are using and if you currently have a working connection), select “Edit Connections” from the drop down list that appears. In the dialog box that pops up, making sure you are on the “Wired” tab, click “Add”. Give the connection a name, such as Shared, click on the “IPv4 Settings” tab and select “Shared to other computers” (all the other fields can be left as they are, though if you aren’t going to be leaving the pi connected to the computer in this manner permanently, you might want to uncheck “Connect Automatically”). Then click ok.
Now click the networking icon again in the top right hand corner, and this time, select the connection you just created (you might first have to disconnect a wired connection if one is set to connect automatically). This will set up a small network consisting of your computer and the pi that is connected to your main internet connection (if you have one). Of course, none of this will actually happen until you turn the pi on, which we are going to next.
So, now you have the correct sort of network cable plugged into your pi and either your router, network or computer, the fun begins. Plug in the power supply and turn it on. Wohoo! You now have a working Raspberry Pi (for a somewhat limited definition of working.) Let’s configure it.
The first thing we need to do is find out what details the DHCP server gave the pi so we can find it on the network. Specifically, what we need is its IPv4 address. You can think of an IP address of a computer as being somewhat analogous to the postal address of a house. In the same way as a postal address starts by narrowing the location of the house down by road and then the exact house in the that road, so the IP address narrows down a computer’s location by network segment before identifying the exact computer on that segment. It’s probably a good idea to have a quick look at an IPv4 address now so we know what we are talking about.
Here is a typical one: “192.168.0.2”. Each block of numbers goes up to 255. Which bit of the address corresponds to the road name and which bit house number is determined by the network mask. For example a network mask of 255.255.255.0 means that the first three numbers are analogous to the road and the final one to the house number, whilst 255.0.0.0 would mean that only the first number is analogous to the road and the final three to the house number. Thus, given a network mask of 255.255.255.0, 192.168.0.2 is on the same “road” as 192.168.0.3, but on different “road” to 192.168.1.4 and, therefor, cannot talk to 192.168.1.4 without the use of some sort of linking device.
Those of you who are quick at math might have worked out that this means there are around 4 billion possible addresses. These have all been used. As a result, IPv4 is in the process of being replaced by IPv6, which increase the number of address to 340 undecillion (7.9×1028 more than IPv4) or “a lot more” if you prefer. This process is likely to take a good few years. At the moment, it is still much more common for people to use IPv4 so that is what I’ll cover here.
Private networks that are not connected directly to internet (e.g. your home network, which is connected to the internet through a router) have special addresses reserved for them. These are 192.168.0.0–192.168.255.255, 172.16.0.0–172.31.255.255 and 10.0.0.0–10.255.255.255.
So getting back to our pi, we know that the IP address assigned to it must be in one of these three ranges and, if you are on a typical network, both your computer and your pi have to be on the same network “street”. Depending on how you connected your pi to the network, there are three different ways in which you go about finding the pi’s address. I’m going to deal with the three cases separately, you only need to follow the one that applies to you.
1) Computer and pi attached via a crossover cable.
This is the easiest case to deal with. You just need to look in the log on your computer to find the address. In your terminal type
tail /var/log/syslog. The tail command shows you the last few lines of a file (ten by default). Look out for a line with dnsmasq-dhcp. If you plugged and turned on your pi awhile ago, the relevant line might not be in the last ten. In this case you can use grep, a massively handy little program, to print out the lines from the syslog that contain dnsmasq-dhcp,
grep dnsmasq-dhcp /var/log/syslog.
2)Pi attached to (simple) local network and you have access to router.
This is also pretty straight forward. You just need to log in to the router and have a look at its log file.
First we have to find the IP address of the router, the default one is probably written on the side of it. If not, bring up a terminal and type
route this brings up the routing table. The default route will be your router (the default route is the one your computer uses if it is looking for a computer that is not on the same network “street” as you, such as a computer on the internet). Mine is 192.168.1.254, though it is more common to use 192.168.0.1.
To log into the router, type the IP address you found via the route command into your web browser. Enter your user name and password (the defaults, again, are often on the router or in the documentation you received with it, else a quick google will tell you.)
As there are so many different routers on the market, I can’t tell you exactly where to find the information you are looking for, so you might have to poke about it to find it. On mine, it can be found in several places. “Attached Devices” is the simplest.
3) Pi attached to a network (including large corporate one) where you don’t have access to whatever gave the pi its IP address (or if you just want to do it this way for fun).
In this case we can’t just look at a log file to work out the IP address. We are going to have to be a bit sneakier. What we are going to do is scan the network till we find it. Be warned, if the network isn’t yours, doing so might be frowned upon by the IT department and be against company policy, as it is the sort thing someone might do if they were trying to get information about the network in order to hack it.
That being said lets see how we do it. First of all, install zenmap:
sudo apt-get install zenmap (zenmap is a graphical frontend for nmap, those who wish to stay hardcore can just use nmap instead), and then fire it up as root (if you can’t run zenmap as root, it won’t be able to identify the pi using its mac address. In which case, you’ll have to keep an eye out for a computer with just one open ssh port. If it is a big network with lots of computers that match that criteria, you may want to run a more thorough scan to help you identify it).
In the target box you put the address of the network segment we are interested in. The pi is going to be on the same one as your computer, so we can use its address and network mask as the target. To get this information, type ifconfig at the command prompt.
Here you can see I have three network devices: eth0 (my ethernet card), lo (the loopback interface, which the computer uses to talk to itself) and wlan0 (the wireless card). You might have slightly different network interfaces depending on what hardware you have. We are interested in the interface that is connected to the same network as the pi, in my case, wlan0. We can see its IPv4 address is 192.168.1.16 and its netmask is 255.255.255.0. We therefor know that the pi must have an address somewhere between 192.168.1.1 and 192.168.1.255. There are various ways we can enter this into zenmap, but the most intuitive is to use the asterisk as a wild card thus, “192.168.1.*”.
It is up to you what level of scan you run, but a quick scan will get you the information you need. The fuller scans can take a long time if you are on big network, but it is quite interesting to see how easy it is to discover things about the network. Anyway, click the scan button and wait for the pi to turn up.