Tweak your config
Helping you to perfect your Pi project, Christian Cawley explains the best way to customise your Raspbian config file.
Helping you perfect your Pi project, Christian Cawley explains ways to customise your Raspbian config file.
The chances are that when you flash a new image to your Raspberry Pi’s microsd card, you’re doing it wrong. While the Pi’s default desktop experience in Raspbian has improved, the configuration side of things hasn’t really matured as much as it could have. The result is you booting up with a keyboard, mouse and monitor attached, and setting up wireless networking, SSH and various other options.
But while the pre-boot configuration could be better, it isn’t completely absent. A Raspberry Pi running Raspbian Stretch – and some other operating systems, but we’re concentrating on Raspbian – can be configured while the microsd card is still inserted in your PC. Wireless networking and SSH can be preconfigured, as can settings for the display, memory and even overclocking.
This is thanks to the Pi’s /boot directory, where files for the wireless configuration and SSH can be created. It’s also where you’ll find config.txt, a text file that stores configuration information for the Raspberry Pi as it boots the operating system.
Ready to pre-configure your Raspbian installation? After writing the operating system disk image file to microsd, browse to the /boot folder in your file manager or Terminal. Here, create a new file in nano:
sudo nano wpa_supplicant.conf
Copy the following into the file:
ctrl_interface=dir=/var/run/wpa_supplicant Group=netdev network={ SSID=”YOUR_NETWORK_NAME” PSK=”YOUR_PASSKEY” key_mgmt=wpa-psk
}
This basic script is straightforward. Edit the SSID so that it matches the name of your wireless network. Similarly, set PSK to your network’s password, then press Ctrl+x to save and exit the file. When the Raspberry Pi boots up, these details will be detected and automatically loaded into Raspbian’s wireless settings. Your Raspberry Pi will be online almost as soon as it boots.
Another easy configuration option for Raspbian is to set up SSH access before you’ve booted your Pi. The benefits of this are considerable. Being able to boot a Raspberry Pi, have it connected to your network, and be able to remotely access the computer saves a lot of messing about. There’s no need to hook up a keyboard, mouse, and display – the Pi will be ready for action.
To enable SSH, once again open your file manager or terminal emulator in the /boot directory. Here, create a new file, simply labelled ssh: sudo touch ssh
That’s all you need. When the Raspberry Pi boots, Raspbian will detect the ssh file and take this as an instruction to enable SSH access. Any SSH app, whether it’s an FTP app with SSH support or a command line tool, can then securely connect to your Raspberry Pi over Wi-fi.
Config your config.txt
If you’re looking for a greater selection of pre-boot tweaks, you’ll need to look at the config.txt file, also in boot. This is a list of configurations organised using the basic format of a property=value statement, all on one line. For example, to set the HDMI cable to a display as the default output for audio from the Pi, you would use hdmi_drive=2
You have three main options for editing config.txt.
The first is from within Raspbian while your Raspberry Pi is running. However, any changes made to the file will not be applied until you reboot. You may also remotely access config.txt via SSH, editing it in nano or whatever text editor you prefer. Finally, of course, you can edit it on your desktop computer.
This can be done before you boot Raspbian for the first time, or at any point in future. All manner of settings can be adjusted, from audio and video options
to the camera, GPIO, and beyond. Before proceeding, however, it’s a good idea to make a copy of the config. txt file. This will ensure that you can overcome any issues that may transpire. For example, specifying the wrong setting when attempting to overclock your Pi will result in the computer not booting. By restoring the original file, you can easily resolve this.
So prior to editing config.txt, make a copy with mv: mv config.txt backupconfig.txt
To restore the backup use rm to remove the new config.txt, then reverse the previous command: rm config.txt mv backupconfig.txt config.txt
With the config.txt file open, you’ll see that many lines of text start with a # comment symbol. This prevents the lines being read by the operating system; removing the hash ‘activates’ the command. It’s a useful method of leaving notes in the file, too, to explain what each group of commands do.
You now have a host of settings that can be adjusted. Most of these will not need changing, however; the Raspbian developers have configured the operating system to be as useful as possible for the widest set of parameters. However, some systems need a bit of individuality. For example, you might be building a project that requires the GPIO pins to be reconfigured. Fortunately, this is possible under the GPIO section of config.txt.
For example, to configure pins 12 to 16 as inputs, use the gpio directive, along with the ip command (standing for ‘input’): gpio=12-16=ip
Bear in mind that changes here can be overridden when Raspbian is running using raspi-gpio.
Usually the Pi’s memory split is managed in raspiconfig. However, this can be managed using the gpu_ mem command in config.txt. You need only specify the amount of memory for the GPU: the remainder will be allocated to the system RAM. For example, using gpu_mem_256 sets the GPU memory to 256MB. You can go as high as gpu_mem_1024 , but note that if the Pi’s total RAM is lower than the figure you’re specifying, the command will not work. This does mean, however, that you can specify multiple gpu_mem values on an SD card that you plan to swap between multiple models with differing amounts of RAM.
With options for overclocking, kernel instructions, audio visual settings, and even including additional configuration files (useful if you have multiple changes you regularly make to the default config.txt), it’s worth spending some time browsing the file to get a grip of just what pre-boot tweaks are possible in Raspbian
Baking your config
As straightforward as config.txt is, it isn’t the most forgiving environment, and changes can have disastrous results. Happily, a tool called Pibakery is a user-friendly alternative. Available from www.pibakery.org, you can set this up on Linux from source, or install it on macos or Windows. Pibakery comes with Raspbian (and Raspbian Lite) built in, and offers a sensible, Scratchstyle block-based UI. Blocks are provided for different types of configuration; for example, Startup actions, Network settings, and more.
These can be dragged into place, individual parameters set, and then the whole thing is baked into Raspbian as the OS is written to your SD card. For example, using the On First Boot block enables easy initialising of your Wi-fi connection, SSH configuration and even VNC server. These blocks can be dragged, dropped, configured and saved. Configurations can also be imported and exported thanks to XML support, making it the ultimate pre-configuration tool.