Raspberry Pi - Part 04 - Base Configuration
Raspberry Pi - Part 04 - Base Configuration
-
Base Configuration
Of Your Raspberry Pi
Version: 2020-12-23
The default user name for Raspbian is pi with its password raspberry
The root password is disabled.
When using SSH (port 22), select an Xterm terminal in color, set white on black as screen
colors, use UTF-8 as character set.
With the launch of Raspbian Jessie there is one significate difference users are going to notice,
and that’s the fact that your Pi will boot straight up into a GUI (instead of the CLI, like
Raspbian Jessie).
That’s great, but what if you just simply prefer the Command Line Interface, or don’t want the
added overhead of the Graphical User Interface? Well you can quite easily tell your Pi to boot
to CLI. Here’s how:
This will load up the raspi-config menu. From the “System” tab, you can simply click the radio
button next to “To CLI” to change the boot preference. Hit “OK” and then “YES” to reboot.
The Raspberry Pi will reboot, but this time you’ll be presented with the all too familiar CLI
login!
If you want to change back to booting to the GUI, you can still change it using raspi-config
Please note that Raspbian start with the 102key US QWERTY keyboard layout loaded !!! See
last page for the layout of such a keyboard.
sudo raspi-config
and change, as needed, the basic configuration for your region & language
1. Expand Filesystem
2. Internationalisation Options
a. change locale
b. timezone
c. keyboard layout
Now reboot
sudo reboot
sudo raspi-config
and change the other settings as required. Consider to change the user password right now
If you are going to add a camera, enable camera here
Check out also the advanced options. See suggestions a bit further
Consider to re-organize its memory. The Raspberry Pi has a “memory_split” option that divides
the memory between the CPU and GPU.
Inside of the file, there are a few lines commented out with hashes. If you want to change this,
uncomment and change as required
#console-width=1024
#console-height=700
When you have finished press [Ctrl] + X. This will ask if you want to save the modified files.
Press 'Y' and then hit [Return] to save the file with the same name.
If you have already connected your Raspberry Pi to your network, find your IP address
hostname -I
or use
ifconfig
where you can see also its MAC address. Eg. Eth0 = wired network interface
sudo reboot
Note: If you are using an wired ethernet connection (with a router) there should be no
configuration required and your PI should connect to your network out of the box.
Suggestions
passwd
You’ll be asked to enter your current password, which by default is set to raspberry. Hit
Return again and you’ll be asked for your new password.
You’ll need to enter it twice, hitting Return both times, to set it correctly.
By default all Raspberry Pis are called raspberrypi. In Linux-speak this is its hostname,
not to be confused with your user name, which defaults to pi.
Either use the ‘Advanced Options’ -> ‘Hostname’ option in raspi-config or at the command
prompt, you can view your hostname by typing
simply use the arrow keys on your keyboard to jump to the end of the word ‘raspberrypi‘
so you can delete and replace it with whatever hostname you’d like.
When you have finished press [Ctrl] + X. This will ask if you want to save the modified
files. Press 'Y' and then hit [Return] to save the file with the same name.
Now change also /etc/hosts, so local address(es) resolves with the new system name.
127.0.1.1 <hostname>
When you have finished press [Ctrl] + X. This will ask if you want to save the modified
files. Press 'Y' and then hit [Return] to save the file with the same name.
SSH stands for Secure Shell. It’s a secure means of accessing the command line interface
of your Raspberry Pi from a separate computer anywhere in the world but usually from
within your own home network.
Using SSH you can update your Pi, tweak settings or run command line tools and
applications in a ‘headless’ way = without the need for a monitor, keyboard or mouse to be
attached.
Since it’s already configured by default, we’ll demonstrate how to do it, but also how to
make finding your Pi on any network infinitely easier than it is by default…
Let’s say you have another Linux or Mac computer and you want to access your Raspberry
Pi from. Assuming your Pi is powered up and connected to the same network as your
computer, all you’d need to do is type a simple command into the terminal window of your
other computer to access it:
Typing SSH tells your computer you want to create a Secure Shell connection, ‘pi‘ is the
user name of the system you wish to access, and the numbers that follow is the IP address
of the system you want to access.
It’s quite straightforward, but it does assume you already know the IP address. In this
world of dynamic IP assignment, it’s not always easy to know without defeating the object
of running ‘headlessly’, but setting up your Raspberry Pi and typing
ifconfig
into a terminal window to find out what the IP address is in the first place.
Recent versions of Raspbian, which use dhcpcd, allow ssh to work over a link-local address
and avahi (which is a zeroconf implementation) enables programs to discover hosts
running on a local network. This means you can plug the Pi into a computer with an
Ethernet cable or a local network router and connect without knowing the IP address.
While it’s relatively easy to set a static IP address for your Pi, or easier still to reserve the
IP address for the Pi on your router’s settings page, why bother?! You can actually connect
to your Pi by name, not number, circumventing all the looking up, writing down and
generally mucking around you otherwise need to do.
To achieve this all you need is a little tool installed via the command line like so:
Since you already know your Raspberry Pi’s hostname, assuming both the computer and
the Raspberry Pi are on the same network you now just need to type the following to SSH
into your Pi:
Again, we type SSH to tell your computer you wish to make a Secure Shell connection and
‘pi’ to denote the username you’d like to connect as, but instead of having to worry about
IP addresses, we simply replace ‘hostname’ in the above command with the actual
hostname of your Pi and append the .local.
Once you’ve hit Return, you’ll be asked to type ‘yes‘ if you want to continue. Then you’ll be
asked to input your Raspberry Pi’s password. Once complete you’ll be met by the same
command prompt you’d see if you were sat in front of your Raspberry Pi.
Once you’ve finished doing whatever you want with your Raspberry Pi, you can end the
SSH session by typing Control + D.
Accessing your Raspberry Pi via SSH on a Windows computer is a little different since you’ll
need to use a dedicated piece of software like Putty
http://www.chiark.greenend.org.uk/~sgtatham/putty/. That said, it’s very easy to set-up
and use once installed.
With the Avahi daemon set-up as we’ve demonstrated you can also enjoy IP address-less
access to your Raspberry Pi in just about any network-aware application you like, not just
SSH!
If you want to login as root using SSH or WinSCP you need to edit the config of SSHD, do this:
PermitRootLogin without-password
and change it to
PermitRootLogin yes
/etc/init.d/ssh restart
or
Now you can login as root, but I recommend you using a strong password or ssh-keys
There have now been a number of revisions to the Raspberry Pi PCB so the device you have in
front of you could be one of a number of variants. The changes include mounting holes,
modifications to the power supply circuitry, different GPIO headers and varying numbers of
USB ports. The Pi 2 introduced a new CPU and additional memory.
The variants currently available are:
In order to find out what hardware revision you have you can run the following command at
the command prompt or via a terminal window :
cat /proc/cpuinfo
In this example I’ve got a PCB with a Revision code of 0002. That is a plain old “Model B
Revision 1.0”.
Note : The Revision number given in cpuinfo file is the hardware revision number. This is not
the same as the Raspberry Pi Revision. In this example I have a Revision 1.0 with a hardware
revision code of 0002. This is not a Revision 2 board!
The differences between the board revisions are minor but it now makes sensible to identify
which board revision you are using when creating hardware or software.
Important Note: As of the 4.9 kernel, all Pi’s report BCM2835 as Hardware, even those with
BCM2836 and BCM2837 processors. You should not use this string to detect the processor.
uuuuuuuuFMMMCCCCPPPPTTTTTTTTRRRR
You can determine the revision of your current firmware with the following command:
$ uname –a
Linux kermit 3.12.26+ #707 PREEMPT Sat Aug 30 17:39:19 BST 2014 armv6l GNU/Linux
/
/
firmware revision --+
On Raspbian, the standard upgrade procedure should keep your firmware up to date:
To update and upgrade to the (optional) change(s) from the older Raspbian desktop/user-
interface to the current layout/behaviour etc.:
Note: Previously, documents have suggested using the rpi-update utility to update the Pi’s
firmware; this is now discouraged. If you have previously used the rpi-update utility to
update your firmware, you can switch back to using apt to manage it with the following
commands:
sudo reboot
or as of Raspbian Buster
Notes:
upgrade
upgrade is used to install the newest versions of all packages currently installed on the
system from the sources enumerated in /etc/apt/sources.list. Packages currently
installed with new versions available are retrieved and upgraded; under no
circumstances are currently installed packages removed, or packages not already
installed retrieved and installed. New versions of currently installed packages that
cannot be upgraded without changing the install status of another package will be left
at their current version. An update must be performed first so that apt knows that new
versions of packages are available.
Dist-upgrade / full-upgrade
dist-upgrade in addition to performing the function of upgrade, also intelligently
handles changing dependencies with new versions of packages; apt has a "smart"
conflict resolution system, and it will attempt to upgrade the most important packages
at the expense of less important ones if necessary. So, dist-upgrade command may
remove some packages. The /etc/apt/sources.list file contains a list of locations from
which to retrieve desired package files. See also apt_preferences(5) for a mechanism
for overriding the general settings for individual packages.
To prevent your Raspberry Pi's from writing a lot of data, and thus, wearing the SD card, you
can do a couple of things.
The first one is to mount a few folders in RAM as tmpfs. The folders are the folders where
temp files and logging is written to. This means that you won't have syslog available, but most
of the time that is not a problem.
Edit /etc/fstab and add the following:
This will mount the above folders in RAM, with a max size of 1 megabyte. The noatime option
means that the access time of a file is not updated, saving a lot of writes as well. You should
also add the noatime option to your other partitions, for example on a standard Raspbian:
Here the /boot partition is also mounted read only (ro). The noatime option is added.
Issue a mount -a command or reboot the machine to make this active.
Linux divides its physical RAM (random access memory) into chucks of memory called pages.
Swapping is the process whereby a page of memory is copied to the preconfigured space on
the hard disk, called swap space, to free up that page of memory. The combined sizes of the
physical memory and the swap space is the amount of virtual memory available.
Swappig causes a lot of writes to the SD card. You would want to turn it off to save writes. The
downside of this is that when there is not enough RAM available the linux OOM killer will
randomly kill processes to save RAM.
Raspbian by default has a swap file, dynamically managed by the dphsys-swapfile utility. You
can turn off this utility by issueing the following commands:
dphys-swapfile swapoff
dphys-swapfile uninstall
update-rc.d dphys-swapfile remove
After a reboot the swap will be gone, which you can check with the free -m command:
My Raspberry Pi's have a cronjob which reboots them once every seven days. This to apply
kernel updates and just a general good procedure to see if all still works after a reboot. By
default, fsck checks a filesystem every 30 boots (counted individually for each partition). I
decided to change this to every boot, so problems will be found and possibly fixed earlier.
To set up an fsck at every boot, execute the following command:
tune2fs -c 1 /dev/mmcblk0p2
sudo reboot
sudo reboot
...
/usr/lib/python2.7/dist-packages/RPi.GPIO-0.6.2.egg-info
...
/usr/lib/python3/dist-packages/RPi.GPIO-0.6.2.egg-info
...
You can see all those lines with 0.6.2.egg-info telling me I have version 0.6.2.
You can also check the RPi.GPIO version in Python (works from 0.4.1a onwards)
It’s quite useful to be able to find out what version of RPi.GPIO you have using Python. Some
features require a recent version, whereas others work with older versions. For example,
interrupts were added at 0.5.1, PWM at 0.5.2 and with 0.5.3 some bugs have been squashed.
You don’t have to write or run any programs to find out which version you have. You can do it
very quickly in a live Python session. In the command line, type…
sudo python
which loads the RPi.GPIO library. Watch out for RPi.GPIO. The i in RPi, is lowercase. All the
rest is uppercase. Get it wrong and it won’t work. So we can use it
>>> GPIO.VERSION
'0.6.2'
>>>
If you buy Raspberry Pi and download Raspbian image then put it on your SD card, you will be
allowed to login using username pi and password raspberry.
Using sudo will enable you to do all you want. If you are annoyed to use sudo every time, and
would like to have root access then, do below
sudo vipw
change line
root:x:0:0:root:/root:/bin/bash
to
root::0:0:root:/root:/bin/bash
When you have finished press [Ctrl] + X. This will ask if you want to save the modified files.
Press 'Y' and then hit [Return] to save the file with the same name.
After this change reboot and after boot, enter root as username, press enter and you will have
root access.
After this, change root password and you can now login directly as root.
passwd root
su root
su -
su pi
There is a very usefull tool inxi available to list all sorts of information about your PI. To
install this tool, type the following in the command line:
sudo inxi –F
or
sudo inxi –h
Note however that this tool is written for Linux running on standard computers. So it might
well not recognise all on your Raspberry Pi.
When you install a package, apt retrieves the needed files from the hosts listed in
/etc/apt/sources.list, stores them in a local repository (/var/cache/apt/archives/), and then
proceeds with installation.
In time the local repository can grow and occupy a lot of disk space. Fortunately, apt provides
tools for managing its local repository: apt's clean and autoclean methods.
apt clean removes everything except lock files from /var/cache/apt/archives/ and
/var/cache/apt/archives/partial/. Thus, if you need to reinstall a package apt should retrieve it
again.
apt autoclean removes only package files that can no longer be downloaded.
We could write some code to check the cpuinfo and extract the bit we want, compare it
with known revision codes etc. But we don’t need any of that because, from RPi.GPIO
0.4.0a onwards (September 2012) we can use a built-in RPi.GPIO variable which does it all
for us.
It’s called GPIO.RPI_REVISION. We can try this out in a live python session…
sudo python
...
>>> import RPi.GPIO as GPIO
>>> GPIO.RPI_REVISION
2
>>>
So then, when you’re making program with some GPIO work that uses any of the ports 0,
1, 2, 3, 21, 27, 28, 29, 30, 31, you can be sure to control the correct ports.
if GPIO.RPI_REVISION == 1:
ports = [0, 1, 21]
else:
ports = [2, 3, 27]
As long as no further changes to GPIO pin allocations are made, the above should work
with all future Pi revisions. If further changes are made, they will need to be incorporated
like this
if GPIO.RPI_REVISION == 1:
ports = [0, 1, 21]
elif GPIO.RPI_REVISION == 2:
ports = [2, 3, 27]
else:
ports = ["whatever the new changes will be"]
import re
def getPiInfo():
try:
with open('/proc/cpuinfo', 'r') as infile:
for line in infile:
# Match a line of the form "Revision : 0002"
# while ignoring extra info in front of the revsion
# (like 1000 when the Pi was over-volted).