Skip to content

Instantly share code, notes, and snippets.

@Olliebrown
Last active April 7, 2024 08:15
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Olliebrown/bb1afa4b0bf2ee125628d3e0df5a966e to your computer and use it in GitHub Desktop.
Save Olliebrown/bb1afa4b0bf2ee125628d3e0df5a966e to your computer and use it in GitHub Desktop.
Setting up a Portable Git Server on an RPI 3B+

A Portable git Server on an RPI 3B+

For the past three years I participated in the GDC TrainJam, an event where a bunch of crazy game developers get on a train in Chicago and make games in teams with folks they've never met before! There are no winners and losers, only friends and good memories of exotic places during the 52 hour train ride.

Oh, and we also make a few games with very little in the way of outside access. The train has no usable internet and for a large portion of the ride there is also no cellular service. When there is, it is awful and not at all usable for serious things like syncing frequently to an external repo service like GitHub. The first year I played it by ear and got a live repo running on a thumb drive that we handed around between teammates. There were only two programmers on the team and even then we found this to be a real slog! Very tedious and some times merging was a nightmare.

The next year, I endeavored to do better, and have been pretty successful. I decided to setup a raspberry pi as a git server and to configure it as a wireless access point so that anyone within about a 50 foot radius could just connect to it and push and pull as they need. Below is a quick guide on how I made all this work. Basic linux understanding is a must and the majority of things described will be familiar to anyone that has braved the command line in a debian or ubuntu flavor of Linux. Experience with git will be quite helpful too.

The majority can and SHOULD be done before hand. The last two steps (creating a repo and adding teammates) should be done at the start of the jam once you have code to share (but test them both beforehand with a dummy user and repo just to be sure all is well).

Raspberry Pi Initial Setup

Setup a fresh install of raspbian on your pi:

After it boots up, we want to set it up to connect to wifi and be headless so the rest can be done via ssh:

  • Log in using the default settings: username=pi, password=raspberry
  • run sudo raspi-config and use the first option to change your password immediately
    • The user will still be 'pi' after this but it will use the new password
  • select 'localization settings' and configure the keyboard and timezone
    • The Raspberry PI Foundation is a British company so it defaults to UK options and you may need something else
  • Select 'network settings' and configure wifi to connect to your local wifi network (the same you will connect to from your laptop/desktop)
  • Select 'interface options' and enable the SSH server
  • While in raspi-config, you may also want to go into advanced settings and expand the primary partition to fill the SD card
    • I have a large SD card in my pi (32GB) and that's more than enough for the little repos we make on the train.
    • You might want to plug in a USB drive to use for repos though, it's up to you!

At this point, restart your pi (it should offer to do so when you exit raspi-config) and when it boots, it should automatically connect to wifi and start an ssh server. You can confirm this by logging in again (with the new password) and typing ifconfig. If you see a 'inet addr' entry for 'wlan0' you are good. If not, visit the wifi page above and debug. You can also do a quick test by trying ping www.google.com and if it fails for any reason it probably means you've failed to connect to your wifi.

Setup Part 2: Headless Pi

Your pi should now be configured as a headless ssh server. You can do the rest using ssh from a laptop or desktop (which will be a bit easier) but you could keep going with the keyboard and monitor if you like!

If you have a Mac or unix system you already have what you need. If you have windows, install git for windows which includes the MSYS bash terminal and unix utilities including ssh. After installing, right-click anywhere on the desktop or in the file explorer and select 'git bash here' to bring up a bash terminal. In Unix or Mac, just bring up a terminal.

  • Connect to your pi via ssh with the following command: ssh pi@raspberrypi
    • Both the pi and your computer must be connected to the same network
  • Log in with the new password you set earlier
  • Start now, by updating all the installed packages with the following commands:
    • sudo apt-get update
    • sudo apt-get upgrade
      • Be patient and read and respond to the prompts that may come up
  • Restart the pi with the command: sudo reboot
    • This will disconnect you from the pi, wait for it to reboot and re-connect via the ssh command
  • Install git with the following command
    • sudo apt-get install git
    • As before, pay attention to prompts and let it proceed
    • run git --version to confirm it worked

At this point, you have the basics working. To finish up, we need to convert the pi into its own wireless access point that will broadcast it's own WIFI network for all your teammates to connect to.

Access Point PI

I use the following guide to complete this part: https://www.raspberrypi.org/documentation/configuration/wireless/access-point.md

The idea is roughly as follows:

  • Install and configure dnsmasq and hostapd and then adjust iptables and dhcpcd
  • There's a good chance you might get booted from your SSH connection before you finish everything so be prepared to log back in using the keyboard and monitor.
    • To avoid this, save any system start/restart commands until the end of EVERYTHING
    • The big one that will boot you is hostapd which takes over the wlan interface and disconnects it from the wifi
  • I found that hostapd would not start in a vanilla install of raspbian (The service was reported as 'masked')
    • fix this by running the command sudo systemctl unmask hostapd and then try to start it again
  • After you start up hostapd, do a hard reboot with the power switch and look for the name of your wifi access point
  • Use your other computer to connect to the new rpi wifi network, put in your password, and cross your fingers!
    • If it doesn't work, hook up your keyboard and monitor and try starting/restarting the various services
    • If you see the wifi network and it will take your password but never fully connect, double check the changes to /etc/dhcpcd.conf and restart dhcpcd again

If you kept the exact settings in the guide, the pi will serve ip addresses in the 192.168.4.xxx subnet. You can connect to the pi via ssh using: ssh pi@192.168.4.1 (the hostname of @raspberrypi will probably no longer work)

git Account and Key

From here, I setup a simple git server following a very standard approach like this one: https://git-scm.com/book/en/v2/Git-on-the-Server-Setting-Up-the-Server

We need to configure a default git account and setup a way to log into it via an ssh key (instead of a password)

  • Connect to the WIFI access point and ssh to the server
  • Issue these commands to make a new user, set its password, and make its home directory:
    • sudo useradd git
    • sudo passwd git (then follow the prompts)
    • sudo mkdir /home/git
    • sudo chown git:git /home/git (that's a lotta 'git')
  • On your local machine, generate a new RSA key pair if you don't already have one
    • Just launch the terminal (or git bash on windows)
    • Run the command ssh-keygen -t rsa
    • Accept the default for the prompts (and you probably don't want a passphrase as some git clients don't support it)
    • The new key will be installed under ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub
    • GUARD the id_rsa one WITH YOUR LIFE! This is your private key. Anyone who has it can pretend to be YOU!
  • Install the public key onto the RPI as an authorized key for the 'git' account
    • Here's a crazy command that does it all at once (from the terminal or git bash)
    • cat ~/.ssh/id_rsa.pub | ssh git@192.168.4.1 "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
      • Enter the password for the 'git' account when prompted

Now, you can log in as 'git' without the password. Go ahead, say ssh git@192.168.4.1 and make sure it connects without a password.

Create a Repo

You can create a repo anywhere on the raspberry pi that the 'git' account has access to but I like to make them under /srv/repos to keep it clean. You might also wish to make them on your USB drive if you have one, just make sure to format and mount it first (google for a guide):

  • connect to the pi wifi and SSH to the pi server as the PI account (not the git account)
  • Issue commands to make the repo directories and initialize a bare repo:
    • cd /srv
    • sudo mkdir repos
    • sudo chown git:git repos
    • su git (and enter the git account password, this switches to the git account)
    • cd /srv/repos (<-- start here when you are adding more repos later and just login as git)
    • mkdir myRepo.git (name it whatever you want)
    • cd myRepo.git
    • git init --bare
    • exit (to stop being the git account)
    • logout

Now, get the code initialized on your end, commit something, add the remote and push. On your local machine, in the folder you want to become the repo, do the following commands:

  • git init
  • Create a good .gitignore file NOW (don't wait until later)
  • git add .
  • git commit -m "Initial commit"
  • git remote add origin git@192.168.4.1:/srv/repos/myRepo.git
  • git push -u origin master

Now, you can push, pull and commit to your hearts content from your computer while your connected to the RPI access point. But what about the rest of your team? They just need an authorized ssh key

They could always give you their public key and you could install it for them but I also pre-generated a bunch of keys on my computer and pre-install them to the pi 'git' account with a little script. Here's the bash script to run on your local computer while connected to the RPI wifi ap (this generates 10 new keys in your ~/.ssh folder):

#!/bin/bash
echo "Generating 10 new keys in ~/.ssh ..."
for i in {1..10}; do
  ssh-keygen -t rsa -N "" -C "GitAccessKey${i}" -q -f ~/.ssh/git-${i}
done

echo "Installing keys on the pi server ..."
for i in {1..10}; do
  cat ~/.ssh/git-${i}.pub | ssh git@192.168.4.1 "cat >>  ~/.ssh/authorized_keys"
done

Adding a Teammate

You can get other teammates able to use your repo as follows:

  • If they already have a public key, get it from them and install it like so:
    • cat theirKey.pub | ssh git@192.168.4.1 "cat >> ~/.ssh/authorized_keys"
  • If they don't have one, give them one of the pre-made keys (both the private and public files)
  • Have them install the private key as their identity to be used by git ssh (this varies a bit by OS and git client)
    • Simplest way is to install it for command line ssh by replacing their ~/.ssh/id_rsa with the new key

Then, they just need to connect to the RPI wifi, and clone the repo!

  • git clone git@192.168.4.1:/srv/repos/myRepo.git .

They can now push and pull freely when connected to the RPI wifi.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment