paint-brush
A Guide on How to Host Your Own Git-Server With Raspberry Piby@m108falcon
15,128 reads
15,128 reads

A Guide on How to Host Your Own Git-Server With Raspberry Pi

by Mridul WadhwaJanuary 19th, 2022
Read on Terminal Reader
Read this story w/o Javascript

Too Long; Didn't Read

Hosting your own git server can be a fun learning experience used to understand the ins and outs of what goes into maintaining a codebase in private environments. You can set up a working private git-server in no time following the guide if you have a Raspberry-Pi handy. You can also set up on VM or AWS machines or any hosting service with Linux. NOTE: This guide just shows the basic setup, security mechanisms regarding user privileges, and committing restrictions that still need to be set up but are beyond the scope of this article.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - A Guide on How to Host Your Own Git-Server With Raspberry Pi
Mridul Wadhwa HackerNoon profile picture



Hosting your own git server can be a fun learning experience used to understand the ins and outs of what goes into maintaining a codebase in private environments. You can set up a working private git-server in no time following the guide if you have a Raspberry-Pi handy.


You can also set up on VM or AWS machines or any hosting service with Linux.


NOTE: This guide just shows the basic setup, security mechanisms regarding user privileges, and committing restrictions that still need to be set up but are beyond the scope of this article.




Let’s get Going! Shall We….

STEP-1: Setting up a New User to Handle Git Repositories

For security purposes and ease of use, I’ll be setting up SSH keys for authentication b/w server and clients over the network.


a) Log in to your existing pi user in raspberry-pi and set up a new user for your git-server.


pi@hostname:~ $ sudo useradd -s /bin/bash -m [username]


I’ll name my user git and will be referring to it for the rest of the article.


The above command adds a new user, -s denotes shell to be used and -m denotes to create the home directory.


b) Give the password to your new user:

pi@hostname:~ $ sudo passwd git


c) Fill in the password of your choice and change user to git and cd into git’s home directory.

pi@hostname:~ $ su git


d) Fill in the password.

git@hostname:home/pi $


e) And then type cd to go to git’s home directory.


STEP-2: Setting up SSH-key Authentication for Git


a) From your client machine, ssh into git to verify that you can remotely login into the user.


m108falcon@quadre:~ $ ssh git@[ipaddr]


b) If you have default port 22 set and user is set to active, you’ll be prompted for the password and log in. Enter the creds and you’ll be logged into git user in your raspberry-pi.


password-authenticated login

c) Open new terminal tab, and do the following on the client machine.

m108falcon@quadre:~ $ ssh-keygen


d) Follow the steps, give a unique name to key and add a password if you wish.


e) After the keys have been set up, send your identification pub key to the server

m108falcon@quadre:~ $ ssh-copy-id -i [location]/unique_key.pub git@[ipaddr]


f) Try logging into git user, if you’re still prompted to log in with a password then:


m108falcon@quadre:~ $ ssh-agent [your shell] && ssh-add [location]/unique_key

if you were logged in passwordless, then you can ignore the above command.

login with ssh-key based authentication


STEP-3: Setting up Repositories

Raspberry-Pi


a) Within your raspberry-pi logged in as git, make a directory to store all git repositories:

git@kleintragbar:~ $ mkdir [directory]


b) I Named mine srv. Within srv, create a directory for the project you want to manage ending with .git. This is because .git helps Linux differentiate b/w standard directories and git-managed repositories.

git@kleintragbar:~ $ cd srv
git@kleintragbar:~/srv $ mkdir [nameofyourrepo].git


c) I Named mine traceviz.git and will be referring to it from now on.

Initialize bare repository. Bare is needed because remote repositories need to keep track of changes and not have working trees.


git@kleintragbar:~/srv/traceviz.git $ git init --bare


Local Machine

a) Go to your project directory and initialize git repository. Add a file and do the initial commit.

m108falcon@quadre:~ $ cd Projects/traceroute_viz
m108falcon@quadre:~/Projects/traceroute_viz $ git init
m108falcon@quadre:~/Projects/traceroute_viz $ git add README.md
m108falcon@quadre:~/Projects/traceroute_viz $ git commit -m 'README'


b) Add a remote address to your git repository.

m108falcon@quadre:~/Projects/traceroute_viz $ git remote add origin ssh://[email protected]/home/git/srv/traceviz.git


c) Add the address of your remote repository after ssh://

d) Push to the remote server.

m108falcon@quadre:~/Projects/traceroute_viz $ git push -u ssh://[email protected]/home/git/srv/traceviz.git


That concludes setting up repositories for pushing code. :)


Testing the Pushed Code by Cloning

a) On the client machine, go to a different directory than your project and clone the remote repository.


m108falcon@quadre:~/Downloads $ git clone [email protected]:/home/git/srv/traceroute_viz.git


And you’ll encounter this error:

warning

b) To fix it, go to your remote machine and modify HEAD file in the git server.

contents of the remote repo

c) Original contents of HEAD say:

original content

d) And change master to main. You can do so by simply editing files using your favorite editor (nano, vim, emacs, or any other of your choice).

modified file

e) Delete your earlier cloned repo, and clone a new one and you’ll get all the files you need.

Now, you’re truly set up with a local remote repository of your own. Have fun managing code with your buddies/roommates.

All the best! 👍