36

I have found myself repeatedly setting up all my pi's with raspbian in pretty much the same manner using raspi-config.

Is there a way to extract all the settings that I've made using raspi-config into an executable script that I could download with wget to each new pi I set up?

A similar solution with the same effect would also be appreciated. I guess I could try one of the rpi docker implementations but I'm not that eager to find out how bleeding edge that stuff is.

7
  • 1
    it depends on what configuration you would like to do ? raspi-config modify configurations files that you can modify using a other way (/boot/config.txt,/etc/module.conf...)
    – mpromonet
    Mar 22, 2015 at 13:02
  • 2
    It's a pretty simple 1000 line shell script in /usr/bin, if that helps. If you want to disable it starting up with a fresh images, delete /etc/init.d/apply_noobs_os_config and the link in /etc/rc2.d.
    – goldilocks
    Mar 22, 2015 at 13:34
  • Just as raspi-config concentrates all the basic setup tasks to one place without requiring me to know how to do each task I'd like a simple DSL that let's me specify all settings in one file. Kind of like docker.
    – 8DH
    Mar 22, 2015 at 17:04
  • Why not just use docker?
    – Tim Holt
    Mar 22, 2015 at 18:27
  • Yeah, why not? What is the preferred docker solution for rpi?
    – 8DH
    Mar 22, 2015 at 20:11

5 Answers 5

36

At least nowadays, raspi-config seems to support noninteractive mode:

cat /boot/cmdline.txt            # show original cmdline.txt
raspi-config nonint do_serial 1  # disable serial console
cat /boot/cmdline.txt            # confirm changes
raspi-config nonint do_serial 0  # enable serial console
raspi-config nonint do_serial 2  # disable serial console, but enable serial hardware (/dev/serial0)
cat /boot/cmdline.txt            # confirm changes

with this, you can write setup scripts that can do anything you could do via the interactive command, and it will do exactly the same things. You will need to find the relevant commands and their usage from the script's source, though.

7
  • 1
    That is wonderful!
    – 8DH
    May 31, 2016 at 13:16
  • So when I prepare a SD card I could put the image there, add my script to the folder and then run the script when the pi has booted.
    – 8DH
    May 31, 2016 at 13:18
  • 1
    That is everything I'm aware of - if there is more, it's not very visibly documented. I stumbled upon this while looking into the script, and didn't look for further information after I found this feature. May 31, 2016 at 14:43
  • 2
    @8DH Is there something you can't handle with this method? (if it's still relevant to you personally) May 31, 2016 at 14:45
  • 2
    @run_the_race When I was writing this answer, as I said, nowhere. I found it in the sources - e.g. here (I have no clue if that's the right repo, but you can of course check the script on your RPi directly too). The other answer has some concrete commands as well. Oct 28, 2021 at 9:07
37

This is still not documented anywhere, but the rc_gui tool (I'm assuming it would be a graphical user interface to the raspi-config if I were to ever install the full desktop) uses command line options to the raspi-config bash script. You can see the full list of options in the C source code. UPDATE: The official repo has disappeared for some reason. This is the most active fork I could find.

/* Command strings */
#define GET_CAN_EXPAND  "sudo raspi-config nonint get_can_expand"
#define EXPAND_FS       "sudo raspi-config nonint do_expand_rootfs"
#define GET_HOSTNAME    "sudo raspi-config nonint get_hostname"
#define SET_HOSTNAME    "sudo raspi-config nonint do_hostname %s"
#define GET_BOOT_CLI    "sudo raspi-config nonint get_boot_cli"
#define GET_AUTOLOGIN   "sudo raspi-config nonint get_autologin"
#define SET_BOOT_CLI    "sudo raspi-config nonint do_boot_behaviour B1"
#define SET_BOOT_CLIA   "sudo raspi-config nonint do_boot_behaviour B2"
#define SET_BOOT_GUI    "sudo raspi-config nonint do_boot_behaviour B3"
#define SET_BOOT_GUIA   "sudo raspi-config nonint do_boot_behaviour B4"
#define GET_BOOT_WAIT   "sudo raspi-config nonint get_boot_wait"
#define SET_BOOT_WAIT   "sudo raspi-config nonint do_boot_wait %d"
#define GET_SPLASH      "sudo raspi-config nonint get_boot_splash"
#define SET_SPLASH      "sudo raspi-config nonint do_boot_splash %d"
#define GET_OVERSCAN    "sudo raspi-config nonint get_overscan"
#define SET_OVERSCAN    "sudo raspi-config nonint do_overscan %d"
#define GET_CAMERA      "sudo raspi-config nonint get_camera"
#define SET_CAMERA      "sudo raspi-config nonint do_camera %d"
#define GET_SSH         "sudo raspi-config nonint get_ssh"
#define SET_SSH         "sudo raspi-config nonint do_ssh %d"
#define GET_VNC         "sudo raspi-config nonint get_vnc"
#define SET_VNC         "sudo raspi-config nonint do_vnc %d"
#define GET_SPI         "sudo raspi-config nonint get_spi"
#define SET_SPI         "sudo raspi-config nonint do_spi %d"
#define GET_I2C         "sudo raspi-config nonint get_i2c"
#define SET_I2C         "sudo raspi-config nonint do_i2c %d"
#define GET_SERIAL      "sudo raspi-config nonint get_serial"
#define GET_SERIALHW    "sudo raspi-config nonint get_serial_hw"
#define SET_SERIAL      "sudo raspi-config nonint do_serial %d"
#define GET_1WIRE       "sudo raspi-config nonint get_onewire"
#define SET_1WIRE       "sudo raspi-config nonint do_onewire %d"
#define GET_RGPIO       "sudo raspi-config nonint get_rgpio"
#define SET_RGPIO       "sudo raspi-config nonint do_rgpio %d"
#define GET_PI_TYPE     "sudo raspi-config nonint get_pi_type"
#define GET_OVERCLOCK   "sudo raspi-config nonint get_config_var arm_freq /boot/config.txt"
#define SET_OVERCLOCK   "sudo raspi-config nonint do_overclock %s"
#define GET_GPU_MEM     "sudo raspi-config nonint get_config_var gpu_mem /boot/config.txt"
#define GET_GPU_MEM_256 "sudo raspi-config nonint get_config_var gpu_mem_256 /boot/config.txt"
#define GET_GPU_MEM_512 "sudo raspi-config nonint get_config_var gpu_mem_512 /boot/config.txt"
#define GET_GPU_MEM_1K  "sudo raspi-config nonint get_config_var gpu_mem_1024 /boot/config.txt"
#define SET_GPU_MEM     "sudo raspi-config nonint do_memory_split %d"
#define GET_HDMI_GROUP  "sudo raspi-config nonint get_config_var hdmi_group /boot/config.txt"
#define GET_HDMI_MODE   "sudo raspi-config nonint get_config_var hdmi_mode /boot/config.txt"
#define SET_HDMI_GP_MOD "sudo raspi-config nonint do_resolution %d %d"
#define GET_WIFI_CTRY   "sudo raspi-config nonint get_wifi_country"
#define SET_WIFI_CTRY   "sudo raspi-config nonint do_wifi_country %s"
#define CHANGE_PASSWD   "(echo \"%s\" ; echo \"%s\" ; echo \"%s\") | passwd"

UPDATE: I notice that there is nothing in there about the locale or keyboard settings. So, that suggests to me that we must examine the source of raspi-config. Luckily we have plenty examples above to get an idea of how to use that script noninteractively. So, what I need to do to solve this problem headlessly is:

locale=en_US.UTF-8
layout=us
sudo raspi-config nonint do_change_locale $locale
sudo raspi-config nonint do_configure_keyboard $layout

The variables are the values I would have selected from the curses UI. Locale options are the first field in the file /usr/share/i18n/SUPPORTED. Keyboard layout options are the directory names in /usr/share/X11/xkb/symbols/. Being in the US, I use en_US.UTF-8 and us. YMMV

4
  • 2
    If you are interested in automating the setup of Raspberry Pis, you should check out my pi-init2 project on github. It provides the long-requested feature of "run a script on boot (headlessly)". Apr 11, 2018 at 16:18
  • There is also sudo raspi-config nonint do_audio %d for choosing audio output (0=auto, 1=jack, 2=HDMI)
    – golimar
    Jul 11, 2019 at 20:06
  • @golimar where did you get that? The original rc_gui project has disappeared and the best fork I could find (answer updated) doesn't include do_audio. But that is indeed a function found in curl -sL https://github.com/RPi-Distro/raspi-config/raw/master/raspi-config | grep -E '(do|get)_[a-zA-Z0-9_ ]+\(' | sort | uniq Jul 12, 2019 at 5:16
  • Here: gist.github.com/damoclark/ab3d700aafa140efb97e510650d9b1be , but your command is great to find all of them, thanks
    – golimar
    Jul 15, 2019 at 17:56
4

All raspi-config does is editing a few configuration files, which you can also edit with a script. Check the sed manual to see how such edits can be efficiently done.

If you have a lot of changes to apply in this way, I would consider setting up a clean RPi image the way you want, diff its complete filesystem* against a vanilla image, and then apply the changes with patch when you need to set up a new installation.

Finally, take a look at tools such as etckeeper - those are specifically designed to manipulate configuration changes and are able to track subtle changes a simple diff would miss, such as permissions. Note that contrary to its name, etckeeper is not restricted to /etc, e.g. running etckeeper -d /boot init; etckeeper -d /boot commit will make a backup of the files in /boot.

(*) Apparently it isn't obvious, so to clear all misunderstandings, I mean you should run something like diff -r /etc /mnt/vanilla_root/etc > etc.diff and diff -r /boot /mnt/vanilla_boot > boot.diff, not diff my.img vanilla.img.

1
  • I'm using Ansible to keep my RPi configs in sync. It's idempotent, which is excellent, and hopefully with this nonint interface to raspi-config I can fill in some of the bits that have been stumping me.
    – RoUS
    Aug 12 at 0:38
1

Use dd (or similar) to copy the working Pi image to your PC.

Use dd (or similar) to copy the copied image from your PC to your Pi.

man dd

3
  • 1
    Yeah, there is of course the image way but then I would need to maintain the image with updates and stuff. (unless there is an elegant solution for that as well...)
    – 8DH
    Mar 22, 2015 at 16:59
  • 3
    @8DH, your suggestion of downloading settings then uploading also means you have to maintain an "image" with updates as well.
    – Tim Holt
    Mar 22, 2015 at 18:26
  • @TimHolt he would need to maintain a bash setup script only. From a size perspective I'd understand why that's more enticing than a whole rpi image
    – Lorenzo
    Apr 24, 2022 at 20:48
0

I have 1 SD-card 100% set up to my desires that I keep as a base for all new installations.

When setting up a new Pi I just clone that SD-card instead of starting from scratch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.