Private Git Server Setup on Ubuntu

In this tutorial, you will learn an Ultimate guide for Private Git Server Setup on Ubuntu. Git is a free and open-source version control system that is used for software development and other projects. But it is recommended to run Git on your own server. By running a private Git server, you can have more control over your server and collaborate with your team.

Ultimate Guide For Private Git Server Setup on Ubuntu

To complete this guide, you must have access to your Ubuntu server as a root or non-root user with sudo privileges. In this guide, we use Ubuntu 22.04 LTS to show you the guide steps to set up a private Git server.

Step 1 – Install Git on the Ubuntu Server

The first step is to install Git on Ubuntu. First, run the system update with the command below:

sudo apt update

Then, use the following command to install Git:

sudo apt install git -y

Verify your Git installation by checking its version:

git --version
Output
git version 2.34.1

Next, locate the git-shell path by using the command below:

which git-shell
Output
/usr/bin/git-shell

If this path doesn’t exist in the /etc/shells, you must add it to the file:

sudo vi /etc/shells

Add the git-shell path at the end of the file:

/usr/bin/git-shell

When you are done, save and close the file.

Step 2 – Create a New Git User To Manage Git Repositories

In this step, you must create a dedicated user so that you can manage your private Git repositories on Ubuntu. To do this, you can use the following command:

sudo adduser git

You will be asked to set a password for your Git user and enter the detailed information.

Output
Adding user `git' ...
Adding new group `git' (1000) ...
Adding new user `git' (1000) with group `git' ...
Creating home directory `/home/git' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for git
Enter the new value, or press ENTER for the default
        Full Name []:

        Room Number []:         Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [Y/n] y

Then, switch to your Git user with the following command:

sudo su git

Step 3 – Create an SSH Directory and Authorized Keys File on Ubuntu

At this point, you must create an SSH directory where you can store the SSH keys and set the correct permissions for it. To do this, run the commands below:

# cd
# mkdir ~/.ssh && chmod 700 ~/.ssh

Next, you must create the authorized-keys file inside the SSH directory where the Public keys are there and set the correct permissions for it. To do this, run the following commands:

# touch ~/.ssh/authorized_keys
# chmod 600 ~/.ssh/authorized_keys

At this point, you can open the authorized-keys file and add the public keys of users who you want to access your private Git server on Ubuntu. You can use your desired text editor like vi editor or nano editor:

nano ~/.ssh/authorized_keys

Add the public keys of the users you want and save and close the file.

Step 4 – Create Individual Repository Directory For Git Server

At this point, you must create a new individual repo directory for Git server and set the correct ownership for it. To do this, run the follwoing commands:

# cd /home/git
# sudo mkdir test-server.git

Next, you must switch to the directory and initialize the git repo with the follwoing commands:

# cd test-server.git
# git init --bare
Output
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint:   git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint:   git branch -m <name>
Initialized empty Git repository in /home/git/test-server.git/

Back to the home directory and set the correct ownership with the follwoing command:

# cd ..
# chown -R git.git test-server.git

At this point, you have succesfully set up the private git server on Ubuntu. Now you can start to use it.

Step 5 – Use Private Git Repository on Ubuntu

At this point, you have created the private git repo under the git user on Ubuntu. Now you can use this repo in two different ways:

  1. Access the repository from your local machine with a different user
  2. Access the repository from a different computer

Let’s see how you can access the private git repo.

Access Private Git Repo From Local Machine with a Different User

If you want to access the Git repo from the same computer, you must log in to the server with a normal user not the Git user. Then, you must use the command below to generate the SSH key pairs if you dont have it:

ssh-keygen -t rsa

When it is completed, you will get the following output:

Output
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:ve5iyJdgbbvhg2UEEfBg21LdF5jAeaO3NJaw1VA6p2I root@ubun.jammy
The key's randomart image is:
+---[RSA 3072]----+
|    +.+=.+.*o.   |
|   . *. = B.o    |
|    o o. *o+.    |
|     .  +.*+     |
|       oE+oo     |
|      o.=...     |
|     o B.o.      |
|      +.Bo       |
|       oo=o      |
+----[SHA256]-----+

Then, use the following command to copy the public key to the private git user SSH directory on Ubuntu:

ssh-copy-id -i ~/.ssh/[your-public-key] git@[your-pc-IP-Address]

In our case, it is like:

ssh-copy-id -i /root/.ssh/id_rsa.pub git@ip-address
Output
Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'git@ip-address'"
and check to make sure that only the key(s) you wanted were added.

If you dont get any error, you have successfully copy your public keys.

Now to show you it is working correctly, we create a project test directory with the comamnd below:

mkdir testproject

Then, switch to it and initilize an empty Github repo:

# cd testproject
# git init
Output
Initialized empty Git repository in /root/testproject/.git/

Next, add the remote origin on your local machine:

# git remote add origin git@[your-IP-address]:[path-to-your-provate-repo]
In my case:
# git remote add origin git@server-ip:/home/git/test-server.git

Here you can craete a sample file inside the project directory and push it to your private git repo. To do this, you can use the follwoing git comamnds:

# touch index.html
# git add *
# git config user.email "orca@example.com"
# git config user.name "orca"

Then, make your first commit:

git commit -m "Initial Commit"
Output
[master (root-commit) 17e0e74] Initial Commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 index.html

At this point, you can push the changes to the remote repository:

git push --set-upstream origin master

Enter the SSH passphrase, and you will see:

Output
Enter passphrase for key '/root/.ssh/id_rsa':
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 209 bytes | 209.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To server-ip:/home/git/test-server.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

Access Private Git Repo from a different computer

The steps in this way are the same as the above steps that you access from the local machine. You just remember that your computer should be on the same network as the computer running the private Github repository.

Also, remember to use the public keys of the coputer you want to access to the private git repo. And all the steps are the same.

That’s it, you are now successfuly finsih Private Git Server Setup on Ubuntu. Hope you enjoy it.

Should I host my own Git server?

It can be say that the best way is to run Git on your own server. It help you save costs, also you have more control over your server.

Can I create private Git repository free?

Yes, you can easily install Git on your server and create a dedicated user for your Git and follow the guide steps in this article and create your free private Git server.

What is the difference between Git and GitHub private server?

Git is a version control system that lets you manage and keep track of your source code history. GitHub is a cloud-based hosting service that lets you manage Git repositories. 

You may be intrested in these article too:

Using SCP to Transfer Files with SSH keys Ubuntu

Install Latest MS SQL Server with Docker on Ubuntu 22.04

Memcached Installation Steps on Debian 12

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!