Git is an open-source version control system used by developers worldwide. Here’s how you can create your own Git Linux server to host your projects.
A Git server hosts the repository of a project, which contains the source code and other core files. While, for the most part, you can rely on globally known Git hosting services like GitHub, in some cases, it is better to host your personal Git server for added privacy, customizability, and security.
Let's learn how you can set up a private Git server on Linux.
Before you start setting up your private Git server, you must have access to a spare machine or should be subscribed to cloud providers. This is important as you'll set up the spare machine to act as a Git server to which you'll connect from your local machine and perform Git operations.
While there are no well-defined system requirements, one gigabyte of RAM should be enough for the Git server to be functional. Furthermore, make sure you have a Linux distribution up and running on the machine.
Needless to say, you need to have Git installed on your Linux server as a preliminary step. Fire up a terminal and use the package manager of your Linux distribution to install Git:
On Debian/Ubuntu derivatives:
On Arch-based distributions:
On CentOS/RHEL/Fedora:
Once Git is installed on your system, proceed to the next steps to configure your Linux system to host your Git repositories as a Git server.
Connect to your Linux server via SSH, RDP, or any other remote access protocol. Or, if you are using a spare computer as the server, switch it on and create a new user account to handle your repositories.
After the new user is added, switch to it using the su command:
Creating a dedicated git user account is a safety protocol that ensures clients connecting to your Git server will have limited visibility and access to the resources on the machine. This allows you to safely collaborate in group projects where multiple team members will be accessing your server.
Creating a .ssh directory is necessary to store public keys and other essential data that'll dictate who gets access to this Git server. To begin with, log in to the git user account you created previously, create the .ssh directory, and restrict access to only the git user:
Secure the directory access permissions using the chmod command to ensure that no one except you can make changes to it. Move into the .ssh directory and create a new file "authorized_keys" using the touch command.
You will have to update this file with the SSH public keys of clients to whom you want to give access to the Git server. Suspend the SSH session and open the .ssh/id_rsa.pub file in your local machine using a text editor or the cat command. This file contains your public encrypted key, which, when written into the authorized_keys file, will grant you access to the Git server without a password.
Copy the public key and spin up a fresh SSH connection to the Git server. Move into the .ssh directory, open the authorized_keys file with a text editor and paste the public key. Save changes and exit.
From then on, you should be able to connect to the server without any password. Repeat this step for each machine that'll be connecting to the server.
Access the Linux server and create a directory or use an inbuilt one as the root directory. Keep in mind that this is the directory under which all your repositories will be stored. This is a good practice for the sake of neater organization of projects.
After creating the directory, move on to the final step in this guide to finish setting up the Git server.
You're now practically done with setting up the Git server. Now you just need to kick off development by initializing repositories and adding the remote origin to your local machine. Move into the parent directory using the cd command and create a .git project directory:
Now, initialize a bare git repository:
With the repository initialized, it's time to add the remote origin on your local machine:
That's all you needed to do on the server side of things. Now any authenticated client can carry out regular Git operations like push, pull, merge, clone, and more. To start new projects you will have to repeat this step each time you create a new project.
Test out its functionality by performing a git push:
Your file will be successfully pushed to the remote origin. To cross-check whether the push operation worked, you can clone the repository, and you should find the test file in the repository.
With the Git server up and running, you must pay close attention to its security stature as it's your personal server and it is your sole responsibility to maintain and protect it from external threats. Some of the best security practices to adopt are:
There are many such security configurations and safety measures that you can implement on your Linux server to protect it from attackers and prevent unauthorized access.
Debarshi Das is an independent security researcher with a passion for writing about cybersecurity and Linux. With over half a decade of experience as an online tech and security journalist, he enjoys covering news and crafting simplified, highly accessible explainers and how-to guides that make tech easier for everyone. While he’s programming and publishing by day, you’ll find Debarshi hacking and researching at night.
Join our newsletter for tech tips, reviews, free ebooks, and exclusive deals!