目錄
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
首頁 系統教程 Linux 在Linux計算機上設置GIT服務器

在Linux計算機上設置GIT服務器

Jul 28, 2025 am 02:47 AM
linux

安裝Git:在服務器上通過包管理器安裝Git並驗證版本;2. 創建專用Git用戶:使用adduser創建git用戶並可選地限制其shell訪問;3. 配置開發者SSH訪問:為git用戶設置.ssh目錄和authorized_keys文件,添加開發者的公鑰;4. 創建裸倉庫:在服務器上初始化bare倉庫並設置正確所有權;5. 客戶端克隆與推送:開發者通過SSH克隆倉庫、提交更改並成功推送代碼,完成私有Git服務器搭建。

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 minimal, 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.

    以上是在Linux計算機上設置GIT服務器的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

pycharm解釋器在哪裡 解釋器位置查找方法 pycharm解釋器在哪裡 解釋器位置查找方法 May 23, 2025 pm 10:09 PM

在PyCharm中設置解釋器的位置可以通過以下步驟實現:1.打開PyCharm,點擊“File”菜單,選擇“Settings”或“Preferences”。 2.找到並點擊“Project:[你的項目名]”,然後選擇“PythonInterpreter”。 3.點擊“AddInterpreter”,選擇“SystemInterpreter”,瀏覽到Python安裝目錄,選中Python可執行文件,點擊“OK”。設置解釋器時需注意路徑正確性、版本兼容性和虛擬環境的使用,以確保項目順利運行。

參加VSCode線下技術交流活動的經驗 參加VSCode線下技術交流活動的經驗 May 29, 2025 pm 10:00 PM

參加VSCode線下技術交流活動的經驗非常豐富,主要收穫包括插件開發的分享、實戰演示和與其他開發者的交流。 1.插件開發的分享:了解瞭如何利用VSCode的插件API提升開發效率,如自動格式化和靜態分析插件。 2.實戰演示:學習瞭如何使用VSCode進行遠程開發,認識到其靈活性和擴展性。 3.與開發者交流:獲取了優化VSCode啟動速度的技巧,如減少啟動時加載的插件數量和管理插件加載順序。總之,這次活動讓我受益匪淺,強烈推薦對VSCode感興趣的人參加。

linux如何限制用戶資源? ulimit怎麼配置? linux如何限制用戶資源? ulimit怎麼配置? May 29, 2025 pm 11:09 PM

Linux系統通過ulimit命令限制用戶資源,防止資源過度佔用。 1.ulimit是shell內置命令,可限製文件描述符數(-n)、內存大小(-v)、線程數(-u)等,分為軟限制(當前生效值)和硬限制(最高上限)。 2.臨時修改直接使用ulimit命令,如ulimit-n2048,但僅對當前會話有效。 3.永久生效需修改/etc/security/limits.conf及PAM配置文件,並添加sessionrequiredpam_limits.so。 4.systemd服務需在unit文件中設置Lim

Informix與MySQL在Linux上的比較 Informix與MySQL在Linux上的比較 May 29, 2025 pm 11:21 PM

Informix和MySQL均為廣受青睞的關係型數據庫管理系統,它們在Linux環境下均表現優異並得到廣泛應用。以下是對兩者在Linux平台上的對比分析:安裝與配置Informix:在Linux上部署Informix需要下載對應的安裝文件,隨後依據官方文檔指引完成安裝及配置流程。 MySQL:MySQL的安裝過程較為簡便,可通過系統的包管理工具(例如apt或yum)輕鬆實現安裝,並且網絡上有大量的教程和社區支持可供參考。性能表現Informix:Informix以卓越的性能和

Debian下Filebeat與Elasticsearch如何集成 Debian下Filebeat與Elasticsearch如何集成 May 28, 2025 pm 05:09 PM

在Debian操作系統中,實現Filebeat與Elasticsearch的集成能夠簡化日誌數據的採集、傳輸和存儲流程。以下是具體的實施步驟:第一步:部署Elasticsearch首要任務是在Debian系統中完成Elasticsearch的安裝工作。你可以從Elastic官網下載對應版本的Elasticsearch軟件包,並依據官方提供的指導完成安裝過程。下載與安裝Elasticsearchwgethttps://artifacts.elastic.co/downloads/elasticse

VSCode 插件更新後導致編輯器崩潰的原因及解決辦法 VSCode 插件更新後導致編輯器崩潰的原因及解決辦法 May 29, 2025 pm 10:03 PM

VSCode插件更新後編輯器崩潰的原因是插件與現有版本的VSCode或其他插件存在兼容性問題。解決方法包括:1.逐個禁用插件排查問題插件;2.降級問題插件到之前版本;3.尋找替代插件;4.保持VSCode和插件更新,並進行充分測試;5.設置自動備份功能以防數據丟失。

如何在Debian上實現Docker的自動化部署 如何在Debian上實現Docker的自動化部署 May 28, 2025 pm 04:33 PM

在Debian系統上實現Docker的自動化部署可以通過多樣的方法來完成,以下是詳細的步驟指南:1.安裝Docker首先,確保你的Debian系統保持最新狀態:sudoaptupdatesudoaptupgrade-y接著,安裝必要的軟件包以支持APT通過HTTPS訪問倉庫:sudoaptinstallapt-transport-httpsca-certificatescurlsoftware-properties-common-y導入Docker的官方GPG密鑰:curl-

mysql怎麼調成中文界面 輕鬆設置mysql中文語言環境 mysql怎麼調成中文界面 輕鬆設置mysql中文語言環境 Jun 04, 2025 pm 06:36 PM

要把MySQL調成中文界面,可以通過MySQLWorkbench或命令行工具實現。 1)在MySQLWorkbench中,打開“Preferences”,選擇“Appearance”選項卡,然後在“Language”下拉菜單中選擇“Chinese(Simplified)”,重啟即可。 2)使用命令行工具時,設置操作系統的語言環境變量,如在Linux或macOS上使用“exportLANG=zh_CN.UTF-8”,然後運行mysql客戶端。

See all articles