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.
Git Server Installation and Configuration
1. Switch to root user
su - root2. Create git and user groups
groupadd git
groupadd user3. 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 git4. 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 user5. Install required packages
sudo apt-get install git git-core ssh6. Create an empty bare repository
mkdir project.git
cd project.git
git --bare7. 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 password8. Configure SSH key for stevenrao (key generation covered later)
mkdir /home/git/.ssh
vim /home/git/.ssh/authorized_keys # paste the public key9. Adjust repository configuration
vim /home/git/project.git/config
[core]
repositoryformatversion = 0
filemode = true
bare = true # add these lines
[receive]
denyCurrentBranch = ignore10. Restart SSH service
/etc/init.d/ssh restartGit Client Configuration (on another machine)
1. Install required packages
sudo apt-get install git git-core ssh2. Add a user for the client
useradd stevenrao -g user -m -s /bin/bash
passwd stevenrao # set password
su - stevenrao # switch to the new user3. 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.pub5. 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 master6. 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 pushSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
