To get your Raspberry Pi up and running in all the ways that you need to begin that awesome new project there are certain basics to take care of.
- Prepare SD Card
- Copy Raspbian Operating System to SD Card
- Set Locale
- Set Timezone
- Keyboard Setup
- Secure Shell
Operating System Image
Raspbian is a modified version of Debian, a very popular and powerful GNU/Linux operating system used the world over.
Raspbian comes in two different flavours of note, the desktop version, and the lite version.
For this tutorial I will employ the use of Raspbian Lite. This version is much smaller than the desktop version as it does not contain any graphical desktop related software packages, yet does contain everything essential to getting up and running quickly.
When choosing an SD card for use with your Raspberry Pi there is little need for more than a Class 10 card. The Raspberry Pi I/O tops out at about 10MB/s (roughly). For more information on SD Card classifications, visit the SD Card Associations website.
You’ll notice there are SHA-256 hash values for each file on the Raspberry Pi Foundations download page for Raspbian. Once your copy of Debian has downloaded completely, you can check these values against your own SHA-256 calculation to ensure that the files are identical and that no weirdo has altered them in any way between you and the server hosting the files. This is unlikely to happen with torrents, but not impossible if the
.torrent file you use has been hijacked in some way.
To verify the SHA-256 hash of any file, you’ll need
openssl, a cryptography toolkit.
~$ openssl sha256 2018-06-27-raspbian-stretch-lite.zip SHA256(2018-06-27-raspbian-stretch-lite.zip)= 3271b244734286d99aeba8fa043b6634cad488d211583814a2018fc14fdca313
The SHA-256 hash calculated by
openssl sha256 filename and the SHA-256 hash provided by the Raspberry Pi Foundation should match exactly. If not, discard the file and try the download again.
There are a couple methods that I know for copying the OS over to the SD Card. I will demonstrate both, as one tends to be much faster than the other, but I recommend that before you begin flashing your OS image you “zero out” the SD card completely.
Note: For Windows users, A) just… what are you thinking? And B) I highly recommend the linux subsystem for Windows 10, instructions here. This may not help you flash the SD card tho, so there are also these instructions. For GNU/Linux users, carry on, noble fellows.
Find The Device Path
First you’ll need to know where your SD Card is on the file system. Typically, this will be something like
/dev/mmcblk0, but depending on what kind of system you’re using this may vary.
At the command prompt, we can check to see if any volume on our SD card is mounted with the df command:
~$ df # This command shows all currently mounted file systems Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mmcblk0 15542944 8 15542936 1% /media/user/16GB
On my GNU/Linux laptop I have inserted my SD Card into the SD Card reader and the volume is automatically mounted for me to use. Most SD Cards will come with a default filesystem formatting such as
For our Rasbperry Pi we’ll need to condition this card before we begin copying the Raspbian disk image to it.
Root & Disk Access
To condition the card we want to ensure that every block of memory on the card contains only zeros. To do this we will need to use the root account on our system to access disk functions. This can be done one of two ways:
~$ sudo su
[sudo] password for user:
Note the command prompt will change from a dollar sign
$ to a hashmark
# to indicate that you are now using the root account. This method works if you have the
sudo package installed and your user account is a part of the
sudo group. This way you can use your own password to gain access to root functions.
Another method is to login as the root user, or to switch to the root user using the root users password. On most systems there is no root password, but for some there may be. If you know what this password is, you can switch to the root account using the
su Switch User command.
~$ su root
All Zeros All The Time
Once the root account is available to you, you can begin to condition your SD card with the
dd command. This command is called Disk Destroyer for a very good reason. It is very simple to destroy data with this command, and it should be used with great care.
To zero out the SD card there is an unlimited supply of zeros available at
/dev/zero. Making sure the card has no mounted volumes, …
~# umount /dev/mmcblk0*
… proceed with the wipe by issuing the
dd command with the following options:
~# dd if=/dev/zero of=/dev/mmcblk0 status=progress
if is the source location for the data you want to write.
of is the destination to which you wish to write your data.
status=progress provides a single line of output that tells you how much data has been written to the destination.
status=progress feature of
dd is relatively new, so don’t be surprised if on older systems this returns an error. It can be safely omitted.
Alternatively, you can use the
pv Pipe Viewer command to track how much data has been written to the destination. This command can track data flow between two pipes
|, such as:
~# dd if=/dev/zero | pv | dd of=/dev/mmcblk0
This process will take a long time. I used the
time command, a GNU program for measuring CPU resource usage, to check just how long it took on my system like so:
~# time dd if=/dev/zero of=/dev/mmcblk0 status=progress
15926592000 bytes (16 GB, 15 GiB) copied, 3410 s, 4.7 MB/s dd: writing to '/dev/mmcblk0': No space left on device 31116289+0 records in 31116288+0 records out 15931539456 bytes (16 GB, 15 GiB) copied, 3414.4 s, 4.7 MB/s
real 56m54.400s user 0m47.840s sys 5m14.872
It’s often handy to use the
time command to give yourself an idea of just how long a process may take to complete its tasks, and in the world of computer science, the less time it takes, usually the better. In my case, at a data rate of about 5MB/s, my 16GB card was completely zero’d out in about 57 minutes.
This is the longest part of the process. What follows is realtively quick, and there are two methods to getting the OS onto the SD card from here.
Decompress and Write to SD Card
The first method is to use
dd again to copy block for block the entire OS image onto the card.
Ensure the image is uncompressed so that you have a file that ends in
.img and not
unzip tool is perfectly straightforward.
~$ unzip 2018-06-27-raspbian-stretch-lite.zip
Archive: 2018-06-27-raspbian-stretch-lite.zip inflating: 2018-06-27-raspbian-stretch-lite.img
Decompressing the file will take a bit of time and the resulting .img file will be much larger than its zipped counterpart. For Raspbian Lite the uncompressed image is roughly 1.8 gigabytes.
Once decompressed, the Disk Destroyer command can copy the image to the SD card in much the same way it copied endless zeros to the card from
~# time dd if=./2018-06-27-raspbian-stretch-lite.img of=/dev/mmcblk0 status=progress 1861247488 bytes (1.9 GB, 1.7 GiB) copied, 388 s, 4.8 MB/s 3637248+0 records in 3637248+0 records out 1862270976 bytes (1.9 GB, 1.7 GiB) copied, 391.515 s, 4.8 MB/s
real 6m31.520s user 0m4.628s sys 0m33.728s
This is the slow method. Since we’ve already zero’d out the sd card, we can turn to a very different tool to write the os image,
bmaptool. This utility, as I understand it, will only copy the bits that need to be changed as opposed to writing every single bit as in the case of
dd. I could be wrong about that tho… it’s a bit mysterious…
bmaptool is slightly more complicated in terms of passing arguments, but you can achieve your goal like this:
~# bmaptool copy --nobmap ./2018-06-27-raspbian-stretch-lite.img /dev/mmcblk0 bmaptool: info: no bmap given, copy entire image to '/dev/mmcblk0' bmaptool: info: 100% copied bmaptool: info: synchronizing '/dev/mmcblk0' bmaptool: info: copying time: 2m 16.0s, copying speed 13.1 MiB/sec
As you can see,
bmaptool accomplishes our goal in less than half the time required by
dd. This may or may not be of value to you, but its good to know.
Handily, I believe in many cases
bmaptool can also decompress the source file as it’s being written to the destination, eliminating the need to do this yourself. For more information about
bmaptool check here.
With the os image loaded onto the SD card, you can now assemble your Raspberry Pis accessories together. You will need to insert the SD card into the SD card slot, and then the keyboard, HDMI cable and finally power supply. In this photo I have a Raspberry Pi Model B enclosed in a Pimoroni Pibow Ninja case.
Its not strictly necessary to attach an ethernet cable to the Pi just yet, but giv’er if that’s what yer int’r.
The default username and password for a fresh Raspbian image are
Once logged in as the
pi user, you can run
raspi-config to get to the heart of the Raspberry Pis settings.
pi@raspberrypi:~ $ sudo raspi-config
Before you go and change your password, there are a few steps to consider first. This is a hard lesson learned if you’re me. 😆 Without getting ahead of ourselves, let’s start with the
Locale is representative of language and geographical region. This will vary depending on where you are in the world, and by default is set to Great Britain where the Raspberry Pi was born 👶🏻.
In my case, I would change this to
en_CA.UTF-8 UTF-8 for English, Canada, and UTF-8 standard character encoding. More about character encoding here. You can use the arrow keys to highlight selections and the spacebar to make a selection ⌨️.
Once you’ve highlighted and selected your preferred locale, the next page will have you select it again from a much shorter list. The TAB key will switch between the <Ok>, <Cancel> options and the list itself.
en_GB.UTF-8 UTF-8 locale is presently in use, it cannot be removed at this time, but you can go through this process again after a reboot to eliminate it if you wish. Simply deselect the
en_GB.UTF-8 UTF-8 locale from the first, very long list using the spacebar. A deselected option shall have no asterisk between the braces, ie.
tzdata, the timezone setting. In my case this would be America/Toronto.
Next is Keyboard Layout, currently configured for a keyboard designed for the good folks of Great Britain. There are key differences between keyboards all around the world, no pun intended (or was it?)
It is best to change this setting for your specific hardware before you change your password. The same keys would not necessarily map to the same characters and your password wouldn’t exactly be correct if you didn’t.
Again, hard lesson learned. Locking yourself out of the OS usually means having to re-flash the sd card, thereby losing everything you’ve accomplished so far, or in a very complex way, changing the password by altering the filesystem on some other machine. Yikes.
Just below Keyboard Layout is the WiFi Country setting. If your Pi has onboard WiFi or a WiFi adapter you’ll need to set this before you can start sending and recieving WiFi signals.
This is important because in Canada, for example, channel 14 of the 2.4Ghz wireless band is reserved, whereas in other countries it may not be. More about ISM Radio Bands here.
What remains are settings subject to your personal preferences. At this point it is a good idea to reboot the system, login as the
pi user and immediately change your password.
If you want to access your Raspberry Pi from another computer using
ssh Secure Shell, you can start by making a hidden
.ssh directory in your home directory. The leading period denotes a “hidden” file, which is a bit silly because they’re not really hidden, but they will not appear unless you know how to look for them.
pi@raspberrypi:~ $ mkdir .ssh # create the .ssh directory
pi@raspberrypi:~ $ chmod 700 .ssh # give only yourself access
pi@raspberrypi:~ $ umask 077 .ssh # default permissions for new files
ls List command without any options will not show the hidden files and folders in your current directory, but if you tack on the
-a option you will see them.
pi@raspberrypi:~ $ ls -a
.ssh directory you’ll need an
authorized_keys file. You can create this file with the
pi@raspberrypi:~ $ cd .ssh
pi@raspberrypi:~/.ssh $ touch authorized_keys
touch command is used to create file entries on the file system that reference files with no data, or “empty” files.
On the computer you wish to have access from, the same applies. Create a
.ssh directory in your home folder if one doesn’t already exist. Change your working directory to the
.ssh directory, then run the
ssh-keygen command to create a new key pair.
pi@raspberrypi:~/.ssh $ ssh-keygen
Generating public/private rsa key pair. Enter file in which to save the key (/home/kyro/.ssh/id_rsa):
Name the key something relevant, like
mypi or use the default value
id_rsa by hitting enter.
Enter passphrase (empty for no passphrase): Enter same passphrase again:
Choosing a lengthly passphrase will protect your private key from being used by someone with access to your home folder, but if you know your home folder is safe from intrusion, or if you don’t want to bother typing out your passphrase every time you use your private key, you can hit enter to skip this step.
Your identification has been saved in /home/user/.ssh/id_rsa. Your public key has been saved in /home/user/.ssh/id_rsa.pub. The key fingerprint is: SHA256:kbsTyFKLtQUg25rvkeZn0vtHH0igeV/NT3fKAbizN4E user@host
The key's randomart image is: +---[RSA 2048]----+ | . ... . | | + .... . | | . . oo+. o + | | o =o=.oE o + +| | o o =.So = o =o| | . o o= + o .| | =. o. o o | | +..+ .. . | | o+.o.. | +----[SHA256]-----+
You now have a key pair to authenticate and enable encryption between yourself and your Raspberry Pi. To complete the setup you’ll have to copy the contents of the
id_rsa.pub file into the
authorized_keys file on your Raspberry Pi inside your
On your Raspberry Pi you will need to enable the ssh server through the
raspi-config utility. Under the
Interfacing Options menu you will find the ssh enable option. This is necessary before you connect to the Pi remotely.
And since I’m dog tired right now and it’s time for beddy byes, you can finished up using the instructions here.
Ta-da! You’re ready to roll. I may come back and add some fine tuning tips here later… maybe… 😄… 😴…