Setup Git Server on Ubuntu



A Git server on Ubuntu can help you host your repositories on a centralized server. This is particularly important for anyone working in a development team. In this tutorial, we will explain the details of setting up a Git server on Ubuntu including the installation of Git, Users and other Security considerations.

What You'll Need

  • A machine with Ubuntu 20.04 or higher and access to a terminal and a basic knowledge of Linux as an operating system
  • Root or sudo access to install software
  • Basic knowledge of the command line
It is now important to describe each step in more detail.

Step 1: Update Your System

Users need to ensure that the Ubuntu system is updated before getting Git installed into the system.

sudo apt update && sudo apt upgrade -y

Step 2: Install Git

Ubuntu uses aptitude for package management which makes it possible to install git. Use the following command:

sudo apt install git -y

Once installed, verify the Git installation:

git --version

If you get the version number then it means Git is installed in your system successfully.

Step 3: Create a New Git User

Having a specific user for your Git repositories brings about security in as much as it makes everyone use their own.
sudo adduser git

This will make you to key in a password and other details of the new user which you want to add. After that switch to the git user.

sudo su - git

Step 4: Set Up SSH for Secure Access

SSH is a secure protocol that controls the connection to your Git server so only those who are allowed can connect.

1. Create an .ssh directory for the Git user:

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

2. Add SSH keys of users who need access to this Git server. Open the authorized_keys file:

nano ~/.ssh/authorized_keys

Each user should copy his/her public key (normally, their file is id_rsa.pub) and append it to the authorized_keys file.
3. Set permissions for the authorized_keys file:

chmod 600 ~/.ssh/authorized_keys

Step 5: Create a bare Minimums Git Repository

The concept of a ?bare' git repository is in fact perfectly suit for a server, primarily because the ?bare' repository does not contain working files not files which are ?checked out' but is designed for sharing code only.

1. Create a directory to store repositories:

mkdir -p ~/repositories/my_project.git

2. Initialize the repository as a bare Git repository:

cd ~/repositories/my_project.git
git init --bare

3. Set permissions to ensure the Git user owns this directory:

sudo chown -R git:git ~/repositories/my_project.git

Step 6: Set Up Access Permissions As Per Your Requirement (It Is Not Compulsory)

You also can manage the access to the repository through SSH settings or, perhaps, through some extra tools. Well, let us keep using the SSH based access to avoid complicating the explanation of the steps.

Step 7: Clone the Repository in to Another in a local Computer

As to use the new Git server repository, the next step must be cloning it in the local machine.

git clone git@your_server_ip:~/repositories/my_project.git

Replace your_server_ip with the server's IP address.

Step 8: Permissions for the collaborative Work Organisation

To allow multiple users to push changes, set up permissions within the repository:

chmod -R g+ws ~/repositories/my_project.git

Add users to the Git group:

sudo usermod -aG git

Step 9: Test the Git Server

Push changes to the server to confirm everything is configured correctly:

1. Navigate to the local repository:

cd my_project

2. Make a change, commit, and push it to the server:

git commit -m "Initial commit"
git push origin master

If there is no error, congratulations, you have your own Git server on Ubuntu up and running!

Securing Your Git Server

In addition, firewall rules should allow only the connection via SSH to the Git server, and the fail2ban tool can be configured to prevent repeated failed login attempts.

Common Troubleshooting Tips:

  • SSH Authentication Errors: It should also ensure that correct SSH keys are inserted into the authorized_keys and that permissions of the files are okay.
  • Permissions Issues: Ensure the Git user owns the repository directories and should have 755 group permissions

Conclusion

Implementing Git server on Ubuntu is extremely useful for storing codes. In this tutorial, we explained how you can set up an active and efficient Git server on Ubuntu. Occasionally, you need to make copies of your repositories to prevent data loss, and it may be a good idea to have other methods of tracking for better server control.

Updated on: 2024-11-01T11:37:00+05:30

120 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements