Table of Contents
2. Create a Dedicated Git User
3. Set Up SSH Access for Developers
4. Create a Bare Repository
5. Clone and Use the Repository from Client
Optional: Improve Security and Usability
Home System Tutorial LINUX Setting up a Git Server on a Linux Machine

Setting up a Git Server on a Linux Machine

Jul 28, 2025 am 02:47 AM
linux

Install Git: Install Git on the server through the package manager and verify the version; 2. Create a dedicated Git user: Use adduser to create a git user and optionally restrict its shell access; 3. Configure developer SSH access: Set the .ssh directory and authorized_keys file for git users, and add the developer's public key; 4. Create a bare repository: Initialize the bare repository on the server and set correct ownership; 5. Client cloning and push: Developer cloning the repository through SSH, submit changes and successfully push code to complete the construction of a private Git server.

Setting up a Git Server on a Linux Machine

Setting up a Git server on a Linux machine is a straightforward process that gives you full control over your repositories. While services like GitHub or GitLab are popular, a self-hosted Git server is ideal for private teams, internal projects, or learning purposes. Here's how to set it up using SSH and Git, the simplest and most common method.

Setting up a Git Server on a Linux Machine

1. Install Git on the Server

First, ensure Git is installed on your Linux machine.

On Debian/Ubuntu:

Setting up a Git Server on a Linux Machine
 sudo apt update
sudo apt install git

On CentOS/RHEL/Rocky Linux:

 sudo yum install git
# Or on newer versions:
sudo dnf install git

Verify the installation:

Setting up a Git Server on a Linux Machine
 git --version

2. Create a Dedicated Git User

It's best practice to create a dedicated user for Git to isolate access and improve security.

 sudo adduser git

Set a password (or disable shell login later for enhanced security).

You can optionally disable shell access by setting the git user's login shell to git-shell :

 sudo usermod -s /usr/bin/git-shell git

This restricts the git user to only Git-related operations over SSH.


3. Set Up SSH Access for Developers

Developers will push and pull code using SSH keys. You need to set up authorized keys for each user.

As the git user, create the .ssh directory:

 sudo su - git
mkdir .ssh && chmod 700 .ssh
touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys

Now, collect each developer's public SSH key (usually id_rsa.pub or id_ed25519.pub ) and append them to authorized_keys .

Example:

 echo "ssh-rsa AAAAB3NzaC1yc2E... user@machine" >> ~/.ssh/authorized_keys

Each key should be on a single line.

? Tip: You can automate this with scripts or tools like ssh-copy-id (used from the client side).


4. Create a Bare Repository

A Git server uses bare repositories — repositories without a working directory.

Create one in /home/git (or any preferred location):

 cd /home/git
git init --bare myproject.git

You can name it anything, but .git extension is conventional.

Set proper ownership:

 sudo chown -R git:git myproject.git

5. Clone and Use the Repository from Client

On a developer's machine, clone the repo:

 git clone git@your-server-ip:/home/git/myproject.git

Replace your-server-ip with the actual server IP or domain.

Make a change and push:

 cd myproject
echo "Hello" > README.md
git add .
git commit -m "Initial commit"
git push origin master

That's it — your Git server is working.


Optional: Improve Security and Usability

  • Use SSH key authentication only : Disable password login for SSH in /etc/ssh/sshd_config :

     PasswordAuthentication no

    Then restart SSH: sudo systemctl restart sshd

  • Organize repositories : Place all repos in /home/git/repositories/ for consistency.

  • Back up regularly : Since it's self-hosted, ensure you have backups of /home/git .

  • Use Git hooks : Automate tasks (eg, deploy on push) using hooks in the hooks/ directory of the bare repo.


  • Alternatives to Consider

    For more features (like web UI, user management, access control), consider:

    • GitLab Self-Managed : Full-featured, open-source platform.
    • Gitea or Gitiles : Lightweight, easy to set up.
    • cgit or GitWeb : Simple web interfaces for read-only access.

    But for a minimum, secure, and fast setup, plain Git over SSH is hard to beat.


    Setting up a basic Git server doesn't require complex tools. With just SSH and Git installed, you can have a private, functional server up in minutes. It's not flashy, but it's reliable and gives you full control.

    The above is the detailed content of Setting up a Git Server on a Linux Machine. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1582
276
What is the code number of Bitcoin? What style of code is Bitcoin? What is the code number of Bitcoin? What style of code is Bitcoin? Jul 22, 2025 pm 09:51 PM

As a pioneer in the digital world, Bitcoin’s unique code name and underlying technology have always been the focus of people’s attention. Its standard code is BTC, also known as XBT on certain platforms that meet international standards. From a technical point of view, Bitcoin is not a single code style, but a huge and sophisticated open source software project. Its core code is mainly written in C and incorporates cryptography, distributed systems and economics principles, so that anyone can view, review and contribute its code.

How to make PHP container support automatic construction? Continuously integrated CI configuration method of PHP environment How to make PHP container support automatic construction? Continuously integrated CI configuration method of PHP environment Jul 25, 2025 pm 08:54 PM

To enable PHP containers to support automatic construction, the core lies in configuring the continuous integration (CI) process. 1. Use Dockerfile to define the PHP environment, including basic image, extension installation, dependency management and permission settings; 2. Configure CI/CD tools such as GitLabCI, and define the build, test and deployment stages through the .gitlab-ci.yml file to achieve automatic construction, testing and deployment; 3. Integrate test frameworks such as PHPUnit to ensure that tests are automatically run after code changes; 4. Use automated deployment strategies such as Kubernetes to define deployment configuration through the deployment.yaml file; 5. Optimize Dockerfile and adopt multi-stage construction

How to build an independent PHP task container environment. How to configure the container for running PHP timed scripts How to build an independent PHP task container environment. How to configure the container for running PHP timed scripts Jul 25, 2025 pm 07:27 PM

Building an independent PHP task container environment can be implemented through Docker. The specific steps are as follows: 1. Install Docker and DockerCompose as the basis; 2. Create an independent directory to store Dockerfile and crontab files; 3. Write Dockerfile to define the PHPCLI environment and install cron and necessary extensions; 4. Write a crontab file to define timing tasks; 5. Write a docker-compose.yml mount script directory and configure environment variables; 6. Start the container and verify the log. Compared with performing timing tasks in web containers, independent containers have the advantages of resource isolation, pure environment, strong stability, and easy expansion. To ensure logging and error capture

How to Securely Erase a Hard Drive on Linux How to Securely Erase a Hard Drive on Linux Jul 24, 2025 am 12:08 AM

Confirm the target hard disk device name (such as /dev/sda) to avoid accidentally deleting the system disk; 2. Use sudoddif=/dev/zeroof=/dev/sdXbs=1Mstatus=progress to overwrite the zero value in full disk, which is suitable for most scenarios; 3. Use sudoshred-v-n3/dev/sdX for three random data overwrites to ensure that it cannot be restored; 4. Optionally execute sudobadblocks-wsv/dev/sdX for destructive write tests; finally use sudohexdump-C/dev/sdX|head to verify whether it is all zero and complete safe erasing.

Linux vs Windows: Which Operating System is Better for You? Linux vs Windows: Which Operating System is Better for You? Jul 29, 2025 am 03:40 AM

Windowsisbetterforbeginnersduetoeaseofuse,seamlesshardwarecompatibility,andsupportformainstreamsoftwarelikeMicrosoftOfficeandAdobeapps.2.LinuxoutperformsWindowsonolderorlow-resourcehardwarewithfasterboottimes,lowersystemrequirements,andlessbloat.3.Li

How to Schedule Tasks on Linux with Cron and anacron How to Schedule Tasks on Linux with Cron and anacron Aug 01, 2025 am 06:11 AM

cronisusedforpreciseschedulingonalways-onsystems,whileanacronensuresperiodictasksrunonsystemsthataren'tcontinuouslypowered,suchaslaptops;1.Usecronforexacttiming(e.g.,3AMdaily)viacrontab-ewithsyntaxMINHOURDOMMONDOWCOMMAND;2.Useanacronfordaily,weekly,o

What to do after installing linux What to do after installing linux Jul 23, 2025 am 02:57 AM

AfterinstallingLinux,thefirststepsincludeupdatingyoursystem,installingessentialsoftware,settingupbackupandsecuritymeasures,andcustomizingtheinterfacetosuityourpreferences.1)Updateyoursystemusingtheappropriatecommandforyourdistro(e.g.,sudoaptupdate&am

How to install software on Linux using the terminal? How to install software on Linux using the terminal? Aug 02, 2025 pm 12:58 PM

There are three main ways to install software on Linux: 1. Use a package manager, such as apt, dnf or pacman, and then execute the install command after updating the source, such as sudoaptininstallcurl; 2. For .deb or .rpm files, use dpkg or rpm commands to install, and repair dependencies when needed; 3. Use snap or flatpak to install applications across platforms, such as sudosnapinstall software name, which is suitable for users who are pursuing version updates. It is recommended to use the system's own package manager for better compatibility and performance.

See all articles