Operations 7 min read

How to Build Your Own Git Server on Linux: Step‑by‑Step Guide

Learn to set up a private Git server on a Linux machine, create groups and users, configure SSH keys, initialize a bare repository, and connect clients with detailed command‑line instructions for both server and client sides.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Build Your Own Git Server on Linux: Step‑by‑Step Guide

Git Server Installation and Configuration

1. Switch to root user

su - root

2. Create git and user groups

groupadd git
groupadd user

3. Add the git account

useradd git -g git -m -s /bin/bash
usermod -G git,user git   # add git to both git and user groups
passwd git   # set password for git

4. Add git to sudoers (avoid using root for daily work)

vim /etc/sudoers   # add line: git ALL=(ALL:ALL) ALL
su - git   # switch to git user

5. Install required packages

sudo apt-get install git git-core ssh

6. Create an empty bare repository

mkdir project.git
cd project.git
git --bare

7. Add additional user accounts (example: stevenrao)

sudo useradd stevenrao -g user -m -s /bin/bash
sudo usermod -G git,user stevenrao   # add to git and user groups
sudo passwd stevenrao   # set password

8. Configure SSH key for stevenrao (key generation covered later)

mkdir /home/git/.ssh
vim /home/git/.ssh/authorized_keys   # paste the public key

9. Adjust repository configuration

vim /home/git/project.git/config
[core]
    repositoryformatversion = 0
    filemode = true
    bare = true   # add these lines
[receive]
    denyCurrentBranch = ignore

10. Restart SSH service

/etc/init.d/ssh restart

Git Client Configuration (on another machine)

1. Install required packages

sudo apt-get install git git-core ssh

2. Add a user for the client

useradd stevenrao -g user -m -s /bin/bash
passwd stevenrao   # set password
su - stevenrao   # switch to the new user

3. Generate SSH key pair

ssh-keygen -t rsa   # accept defaults, set passphrase if desired
# Example output
Generating public/private rsa key pair.
Created directory '/home/stevenrao/.ssh'.
Your identification has been saved in /home/stevenrao/.ssh/id_rsa.
Your public key has been saved in /home/stevenrao/.ssh/id_rsa.pub.

4. View the public key

cat .ssh/id_rsa.pub

5. Initialize a test project and push to the server

mkdir /home/stevenrao/test_proj
cd /home/stevenrao/test_proj
echo "test git by stevenrao v1.0" > test.txt
git init
git add .
git commit -m 'initial commit'

git remote add origin [email protected]:/home/git/project.git
git push origin master

6. Configure global Git identity

git config --global user.name "stevenrao"
git config --global user.email "[email protected]"

7. Make further changes and push

vim test.txt   # modify content to "test git by stevenrao v1.1"
git commit -am "1.1"
git push
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DevOpsGitServerVersion ControlSSH
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.