How to host your own Git server
If you are thinking that hosting your own Git server is very complex, then you're in for a ride...
I recently discovered Hackattic, a platform that presents captivating real-world engineering challenges for problem-solving. I found this concept to be a more engaging alternative to conventional coding practice, such as leetcode. It offers practical scenarios that contribute to a deeper understanding of engineering problem-solving.
Hosting your own git server was one of the problem which you needed to solve. So, this post is about how I went about solving that problem.
Setting up your EC2
For this step, you can use any cloud provider according to your preference. I have used AWS. The first thing is to set up your EC2 instance. There is nothing special that needs to be done. Just spin up the instance and then SSH into it. Follow the steps from this link if you are new.
Add new Git user
Let’s walk through setting up SSH access on the server side. In this example, you’ll use the authorized_keys method for authenticating your users.
First, you create a git user account and a .ssh directory for that user.
$ sudo adduser git
$ su git
$ cd
$ mkdir .ssh && chmod 700 .ssh
$ touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keysGenerating keys
Now we will have to generate a public-private key pair. Note: this step needs to be done on your local machine and not the server.
Open a terminal window. At the shell prompt, type the following command:
ssh-keygen -t rsaThe ssh-keygen program will prompt you for the location of the key file. Press Return to accept the defaults. You can optionally specify a passphrase to protect your key material. Press Return to omit the passphrase. The output of the program will look similar to this:
TextEnter file in which to save the key (/Users/rishabh/.ssh/id_rsa): Created directory '/Users/rishabh/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /Users/rishabh/.ssh/id_rsa. Your public key has been saved in /Users/rishabh/.ssh/id_rsa.pub.Get the public key
Text$ cat /Users/rishabh/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCB007n/ww+ouN4gSLKssMxXnBOvf9LGt4L ojG6rs6hPB09j9R/T17/x4lhJA0F3FR1rP6kYBRsWj2aThGw6HXLm9/5zytK6Ztg3RPKK+4k Yjh6541NYsnEAZuXz0jTTyAUfrtU3Z5E003C4oxOj6H0rfIF1kKI9MAQLMdpGW1GYEIgS9Ez Sdfd8AcCIicTDWbqLAcU4UpkaX8KyGlLwsNuuGztobF8m72ALC/nLF6JLtPofwFBlgc+myiv O7TCUSBdLQlgMVOFq1I2uPWQOkOWQAHukEOmfjy2jctxSDBQ220ymjaNsHT4kgtZg2AYYgPq dAv8JggJICUvax2T9va5 gsg-keypair
Configuring server
Make sure that you are the git user in server and not as the root. I was confused in this step and lost a lot of time, and later realised that the user should not be root here but the one who wants to access it.
The public key which was generated in your local machine needs to be pasted inside the
.ssh/authorized_keysfile.Now, you can set up an empty repository for them by running
git initwith the--bareoption, which initializes the repository without a working directory:Text$ cd /srv/git $ mkdir project.git $ cd project.git $ git init --bare Initialized empty Git repository in /srv/git/project.git/Let's try pushing the first version of my project into that repository by adding it as a remote and pushing up a branch.
Text# on Rishabh's computer $ cd myproject $ git init $ git add . $ git commit -m 'Initial commit' $ git remote add origin git@gitserver:/srv/git/project.git $ git push origin master
That's it and now you have your own Git server, where you can pull, push or even others can pull and push stuff to.
Of course, the steps mentioned here are done manually each time, an interesting exercise would be to write a Bash script which will automate this process and it will be a seamless addition of new user to your own git server 😎.
I would be really interested in seeing it, please do share with me here: mail me