The Wayback Machine - https://web.archive.org/web/20240720035526/https://www.geeksforgeeks.org/how-to-setup-git-server-on-ubuntu/
Open In App

How to Setup Git Server on Ubuntu?

Last Updated : 23 Jan, 2023
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Git is a popular version control system that is widely used for software development and other collaborative projects. Setting up a Git server on Ubuntu allows you to host Git repositories on your own server, which can be useful for collaborating with a team or hosting open-source projects that others can contribute to. Here, we will walk through the steps of setting up a Git server on Ubuntu 20.04 LTS. We will install Git, create a new user to manage the repositories, create a repository directory, initialize a new bare repository, and set the correct permissions on the repository so that other users can access it. 

Steps to Setup Git Server on Ubuntu

Before we begin, make sure we have a clean installation of Ubuntu 20.04 LTS and that you are logged in as a user with Sudo privileges.

Step 1: Install Git.

The first step is to install Git on your Ubuntu server. To do this, open a terminal and enter the following command:

sudo apt-get update

apt-get-update

 

sudo apt-get install git

installing-git

 

This will install the latest version of Git on your server. You can check that Git has been installed correctly by running the following command:

git –version

git-installed

 

This should display the version number of Git that has been installed.

Step 2: Create a Git User.

Next, you need to create a new user account that will be used to manage the Git repositories. This is a best practice as it helps to keep the repositories separate from your main user account. To create a new user, enter the following command:

sudo adduser git

creating-new-user

 

You will be prompted to enter a password and provide some personal information for the user. Once you have completed these steps, a new user will be created.

Step 3: Create a Repository Directory.

Next, you’ll need to create a new directory on your server where you can store your Git repositories. This is typically done in the /usr/local/ directory. You can create a new directory by running the following command:

sudo mkdir /usr/local/git

creating-new-directory

 

Step 4: Change the ownership of the directory to the git user.

Now that you’ve created the directory for the Git repositories, you’ll need to change the ownership of the directory to the git user. This will ensure that the git user has the necessary permissions to read, write, and execute files in the directory. You can change the ownership of the directory by running the following command:

sudo chown git:git /usr/local/git

changing-ownership

 

Step 5: Switch to the git user.

To complete the setup of the Git server, you’ll need to switch to the git user account. This can be done by running the following command:

su -l git

switching-to-git-user-account

 

You should now see the command prompt change to the git user’s account.

Step 6: Initialize a new bare repository.

A bare repository is a type of Git repository that does not contain a working tree (i.e. the files that you’re tracking in your repository). Instead, it only contains the Git metadata and history of the repository. This is the type of repository that you’ll use for your Git server. To initialize a new bare repository, run the following command in the /usr/local/git directory:

git init –bare myproject.git

initializing-new-bare-repository

 

This command will create a new bare repository named “myproject.git” in the /usr/local/git directory.

Step 7: Configure SSH access for the git user.

In order to clone and push to the repository, you need to configure SSH access for the git user. To configure SSH access, you’ll need to add the git user’s public key to the authorized_keys file in the git user’s .ssh directory. You can generate a new ssh key by running ssh-keygen -t rsa -b 4096 on your local machine, type the command:

ssh-keygen -t rsa -b 4096

generating-new-ssh-key

 

In the above image you can see that we have created a ssh_public_key on your local system, this key is saved in /home/git/.ssh/id_rsa.pub. we will use this public key to authorize our server for the login. Now you need to go to the location where this key is present and using the cp command or clipboard simply copy the key and use the key in the next command as follows:

Then copy the contents of the public key file `~/.ssh/id_rsa.pub to the authorized keys file on the server by running the following command:

echo “ssh-rsa <your-public-key>” >> ~/.ssh/authorized_keys

copying-contents

 

This will add your public key to the authorized_keys file, allowing you to connect to the server via SSH.

Step 8: Allow the git user to connect to the server via SSH.

Next, you will need to allow the git user to connect to the server via SSH. You can do this by adding the git user to the SSH AllowUsers list in the /etc/ssh/sshd_config file. Open the file by running sudo nano /etc/ssh/sshd_config and add the following line at the end of the file:

AllowUsers git

git-user-connecting-to-server

 

Step 9: Restart the SSH service.

After making the changes to the SSH configuration file, you’ll need to restart the SSH service for the changes to take effect. You can restart the SSH service by running the following command:

sudo service ssh restart

restarting-ssh-service

 

Step 10: Clone the repository from the server.

Finally, you can clone the repository from the server by running the following command on your local machine:

git clone git@server:/usr/local/git/myproject.git

cloning-repository

 

In the above image, I have used my server IP address instead of the server name you can use it as per your choice. This command will clone the “myproject.git” repository from the server to your local machine. You can now make changes to the files in the repository and push those changes back to the server using the standard Git commands (i.e. git add, git commit, git push).

And that’s it! You have now successfully set up a Git server on your Ubuntu machine. You can now use this server to manage your own code repositories or share code with others. Keep in mind that you should secure your git server by configuring a firewall and other security measures, and you should also back up your git repositories regularly.

Conclusion

In this tutorial, we learned how to set up a Git server on Ubuntu 20.04 LTS. We installed Git, created a new user to manage the repositories, created a repository directory, initialized a new bare repository, and set the correct permissions on the repository so that other users can access it. By following these steps, you can host your own Git repositories on your Ubuntu server and collaborate with others on projects. setting up a Git server on Ubuntu is a relatively straightforward process that allows you to host Git repositories on your own server. By following the steps outlined in this tutorial, you can set up a Git server and start hosting your own repositories.

It’s important to note that This guide is meant as a starting point, and there are many other configurations and options that you can explore to customize your Git server to your specific needs. For example, you may want to consider using Git hooks to automate certain tasks or use Git over SSH for secure communication. Additionally, for a big and more secure environment, it would be recommended to use Git server software like Gitlab, Gogs, and Bitbucket which are more feature rich and provide access control and other features out of the box.



Previous Article
Next Article

Similar Reads

How to install and setup the OpenVPN server on Ubuntu/Debian?
A VPN is a tool that acts as a middleman between you and the Internet that you browse. Whatever you do online, the VPN acts as a connecting bridge between your computer and the Internet. To the services that you are using, for example - visiting a website), to them, it appears as if your VPN is the client, but in reality, the VPN relays the informa
6 min read
Difference Between Git remote prune, Git prune and Git fetch --prune
Git is a version control system that helps developers manage and track changes in their codebase. Among its many commands, git remote prune, git prune, and git fetch --prune are essential for maintaining a clean and organized repository. This article will explain the differences between these commands, their syntax, uses, and provide examples to il
4 min read
How to Setup Browsersync for Web Development in Ubuntu?
BrowserSync is an automation tool which is used extensively in web development. This tool makes our testing and tweaking faster by synchronizing file changes and interactions across many devices. Features of BrowserSync. Live reloading Interaction synchronization Simulate slower connections URL history Compatible with task runners like GULP and Gru
2 min read
How to Setup Git on Anypoint Studio?
Git is essential software for developing purposes. Every developer uses Git as a backup of their work. Git is a type of Version Control System. It falls under the Distributed type of the Version Control System. There is also much another software present that serves the same as Git. But Git is famous because it can be accessed by others also. In a
5 min read
Git - Environment Setup
Setting up your environment for Git is an important step for effective version control in your software development projects. This guide will walk you through the process of installing and configuring Git on various operating systems, ensuring you’re ready to manage your code efficiently. What is Git?Git is an open-source, distributed version contr
2 min read
How to install git-crypt on Ubuntu?
git-crypt encrypts and decrypts data in a git repository in a transparent manner. When you commit a file, it is encrypted, and when you check it out, it is decrypted. With git-crypt, you may freely share a repository that contains both public and private information. git-crypt gracefully degrades, allowing developers without the secret key to clone
1 min read
How to Install, Configure and Use GIT on Ubuntu?
Git is a powerful version control system widely used for tracking changes in source code during software development. This article will walk you through the steps to install, configure, and use Git on an Ubuntu system. Table of Content Installing Git with Default PackagesInstalling/Downloading Git from SourceSetting Up GitHow to Use Git Step-by-Ste
5 min read
How to use GIT in Ubuntu ? (Part -2)
Git is a powerful version control system that helps developers track changes in their code, collaborate with others, and manage project versions effectively. Using Git on Ubuntu is simple and efficient. In the previous article, we learnt how to use basic GIT. In this article, we will try to learn some more basic concepts like branching and merging.
5 min read
How to Setup Apache Web Server On Any Android Device?
In this article, we will learn how we can set up Apache Web Server On Any Android Device. For doing this we have to install the Termux app available on the play store. Termux is an Android terminal emulator and Linux environment application that works directly with no rooting or setup required. Termux provides a Linux-like shell to the user where h
2 min read
How to Setup Central Logging Server with Rsyslog in Linux
This article will show us how to use Rsyslog to set up Linux as a centralized logging service. When managing logs from various systems, a centralized Rsyslog setup is advantageous. All log entries from client servers will be sent to the host server, which will allow them to be monitored and preserved in one location. Compared to sshing into each se
4 min read
three90RightbarBannerImg