search
HomeSystem TutorialLINUXHow to set up a private Git server on Linux

How to set up a private Git server on Linux

Feb 11, 2024 pm 02:00 PM
linuxlinux tutoriallinux systemlinux commandshell scriptit serviceembeddedlinuxGetting started with linuxlinux learning

Git is an open source version control system used by developers around the world. Next, we'll cover how to create your own Git Linux server to host your projects.

A Git server is used to host a project's repository, which contains source code and other core files. While in most cases you can rely on a globally renowned Git hosting service like GitHub, in some cases it is better to host your own personal Git server for added privacy, customizability, and security.

Now, let’s learn how to set up a private Git server on Linux.

Prerequisites for setting up Git server

Before you start setting up your private Git server, you will need to have access to a backup computer or subscribe to a cloud provider. This is important because you will be setting up the backup machine as a Git server and connecting to it from your local machine and performing Git operations.

Although there are no explicit system requirements, 1GB of RAM should be enough to get the Git server running properly. Also, make sure you have a Linux distribution installed and running on your computer.

Step 1: Download and install Git on the Linux server

如何在 Linux 上设置私有 Git 服务器

It goes without saying that as a preliminary step, you need to install Git on your Linux server. Launch a terminal and use your Linux distribution’s package manager to install Git:

On Debian/Ubuntu derivatives:

sudo apt install git

Arch-based distributions:

sudo pacman -S git

On CentOS/RHEL/Fedora:

sudo dnf install git

After installing Git on your system, continue with the following steps to configure your Linux system to host your Git repository as a Git server.

Step 2: Set up a Git user account

如何在 Linux 上设置私有 Git 服务器

Connect to your Linux server via SSH, RDP or any other remote access protocol. Alternatively, if you are using an alternate machine as a server, turn it on and create a new user account to work with your repository.

ssh username@address
sudo useradd git

如何在 Linux 上设置私有 Git 服务器

After adding a new user, use the su command to switch to the user:

su git

Create a dedicated git User account is a security protocol that ensures clients connecting to your Git server have limited visibility and access to the resources on the machine. This allows you to collaborate securely on group projects where multiple team members will have access to your server.

Step 3: Create .ssh directory and add authorization key

Creating a **.ssh** directory is necessary to store the public key and other basic data that will determine who can access this Git server. First, log in to the git user account you created earlier, create a .ssh directory, and restrict access to only the git user:

ssh git@address
mkdir .ssh
chmod 700 .ssh/
touch .ssh/authorized_keys

如何在 Linux 上设置私有 Git 服务器

Use the chmod command to protect directory access to ensure that no one but you can change it. Go into the **.ssh directory and use the ****touch** command to create a new file "authorized_keys".

cd .ssh
ssh-keygen -t rsa #如果您没有 id_rsa.pub 文件,请使用此命令
cat id_rsa.pub

如何在 Linux 上设置私有 Git 服务器如何在 Linux 上设置私有 Git 服务器

您必须使用您希望授予对 Git 服务器访问权限的客户端的 SSH 公钥来更新此文件。暂停 SSH 会话并使用文本编辑器或cat 命令在本地计算机中打开**.ssh/id_rsa.pub**文件。该文件包含您的公共加密密钥,当将其写入authorized_keys 文件时,您将无需密码即可访问Git 服务器。

cd .sshvi authorized_keys

复制公钥并启动到 Git 服务器的新 SSH 连接。进入**.ssh**目录,使用文本编辑器打开 authorized_keys 文件并粘贴公钥。保存更改并退出。

从那时起,您应该能够在没有任何密码的情况下连接到服务器。对将连接到服务器的每台计算机重复此步骤。

第 4 步:创建一个目录来存储您的所有存储库

访问 Linux 服务器并创建一个目录或使用内置目录作为根目录。请记住,这是存储所有存储库的目录。为了更整洁地组织项目,这是一个很好的做法。

mkdir directory_name

创建目录后,继续本指南的最后一步以完成 Git 服务器的设置。

第 5 步:通过添加新项目启动开发

您现在实际上已经完成了 Git 服务器的设置。现在您只需要通过初始化存储库并将远程源添加到本地计算机来开始开发。使用cd命令进入父目录并创建一个**.git**项目目录:

cd parent_director
ymkdir new_project.git

现在,初始化一个裸 git 存储库:

git init --bare

初始化存储库后,是时候在本地计算机上添加远程源了:

git remote add origin name git@address:new_project.git

这就是您在服务器端需要做的所有事情。现在任何经过身份验证的客户端都可以执行常规的 Git 操作,例如推送、拉取、合并、克隆等。要开始新项目,每次创建新项目时都必须重复此步骤。

通过执行 git push 测试其功能:

touch testfile
git add testfile
git commit -m "test file"
git push name master
git clone git@address:new_project.git

您的文件将成功推送到远程源。要交叉检查推送操作是否有效,您可以克隆存储库,您应该在存储库中找到测试文件。

Git 服务器的安全提示

随着 Git 服务器的启动和运行,您必须密切注意它的安全状态,因为它是您的个人服务器,维护和保护它免受外部威胁是您的唯一责任。要采用的一些最佳安全实践是:

  • 禁用密码登录
  • 将默认 shell 更改为 git-shell。这限制了登录用户发出任何非 git 命令
  • 为 SSH 使用自定义端口
  • 禁用root用户登录
  • 定期备份数据

您可以在 Linux 服务器上实施许多此类安全配置和安全措施,以保护其免受攻击者的侵害并防止未经授权的访问。

The above is the detailed content of How to set up a private Git server on Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:良许Linux教程网. If there is any infringement, please contact admin@php.cn delete
How to Automatically Restart a Failed Service in LinuxHow to Automatically Restart a Failed Service in LinuxApr 28, 2025 am 09:39 AM

This guide details how to configure automatic service restarts in Linux using systemd, enhancing system reliability and minimizing downtime. System administrators often rely on this functionality to ensure critical services, such as web servers (Apa

10 Hidden Linux Commands Every Sysadmin Should Know10 Hidden Linux Commands Every Sysadmin Should KnowApr 28, 2025 am 09:35 AM

As Linux users, we often rely on commonly used commands ls, grep, awk, sed and find to complete the work. But Linux has a large number of lesser-known commands that can save time, automate tasks and simplify workflows. This article will explore some underrated but powerful Linux commands that deserve more attention. rename – efficient batch rename files The rename command is the savior when you need to rename multiple files at once. Without using loops containing mv, rename allows you to easily apply complex renaming patterns. Change all .txt files to .log. rename 's/\.txt$/\.log/' *

How to List All Running Services Under Systemd in LinuxHow to List All Running Services Under Systemd in LinuxApr 28, 2025 am 09:29 AM

Linux systems provide various system services (such as process management, login, syslog, cron, etc.) and network services (such as remote login, email, printer, web hosting, data storage, file transfer, domain name resolution (using DNS), dynamic IP address allocation (using DHCP), and so on). Technically, a service is a process or group of process (usually known as a daemon) that runs continuously in the background, waiting for incoming requests (especially from the client). Linux supports different ways to manage services (start, stop, restart, enable automatic startup at system startup, etc.), usually through a process or service manager. Almost all modern Linux distributions now use the same

CrossOver 25: Run Windows Software and Games on LinuxCrossOver 25: Run Windows Software and Games on LinuxApr 28, 2025 am 09:27 AM

Run Windows Software and Games on Linux with CrossOver 25 Running Windows applications and games on Linux is now easier than ever, thanks to CrossOver 25 from CodeWeavers. This commercial software solution lets Linux users run a wide variety of Wind

pCloud - The Most Secure Cloud Storage [50% Off Offer]pCloud - The Most Secure Cloud Storage [50% Off Offer]Apr 28, 2025 am 09:26 AM

Secure Your Data with pCloud: A Comprehensive Guide to Linux Installation pCloud, a leading secure cloud storage service, provides a robust platform for managing your files and data. This guide details the installation process on Linux systems. About

MangoHud - Monitor FPS, CPU & GPU Usage in Linux GamesMangoHud - Monitor FPS, CPU & GPU Usage in Linux GamesApr 28, 2025 am 09:25 AM

MangoHud: A powerful tool for real-time monitoring of Linux gaming performance MangoHud is a powerful and lightweight tool designed for gamers, developers, and anyone who wants to monitor system performance in real time. It acts as an overlay for Vulkan and OpenGL applications, displaying important information such as FPS, CPU and GPU usage, temperature, etc. This article will explore the functions, working principles and usage of MangoHud, and provide step-by-step instructions for installing and configuring MangoHud on Linux systems. What is MangoHud? MangoHud is an open source project available on GitHub and aims to provide a simple and customizable way to monitor

5 Must-Know Linux Command Line Archive Tools – Part 15 Must-Know Linux Command Line Archive Tools – Part 1Apr 28, 2025 am 09:23 AM

Managing archived files is a common task in Linux. This article, the first of a two-part series, explores five powerful command-line archive tools, detailing their features and usage with examples. 1. The tar Command: A Versatile Archiving Utility t

Top 7 Tools to Compare Files in Linux (with Examples)Top 7 Tools to Compare Files in Linux (with Examples)Apr 28, 2025 am 09:21 AM

This guide explores various methods for comparing text files in Linux, a crucial task for system administrators and developers. We'll cover command-line tools and visual diff tools, highlighting their strengths and appropriate use cases. Let's assum

See all articles

Hot AI Tools

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor