插件窝 干货文章 Ubuntu install and configuration Git server

Ubuntu install and configuration Git server

Git sudo bash Install 232    来源:    2025-05-10

Ubuntu Git Server Installation and Configuration Guide

1. Install Git

First, update your package list and install Git:

sudo apt update
sudo apt install git -y

2. Create a Git User (Recommended)

For security purposes, it's best to create a dedicated git user:

sudo adduser git

3. Set Up SSH Access

On the server:

# Switch to git user
su - git

# Create .ssh directory and set permissions
mkdir ~/.ssh
chmod 700 ~/.ssh

# Create authorized_keys file
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

On client machines:

Have users send you their public SSH keys (usually id_rsa.pub) and add them to the authorized_keys file on the server.

4. Initialize a Bare Repository

# As git user
cd ~
mkdir project.git
cd project.git
git init --bare

5. Client-Side Configuration

Users can now clone the repository:

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

6. (Optional) Disable Shell Access for Git User

For additional security, restrict the git user to only Git operations:

# Edit /etc/passwd
sudo nano /etc/passwd

Find the line for the git user and change the shell from /bin/bash to /usr/bin/git-shell:

git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell

7. (Optional) Install GitWeb for Web Interface

sudo apt install gitweb -y

Configure GitWeb by editing /etc/gitweb.conf:

$projectroot = "/home/git";
$git_temp = "/tmp";

Restart Apache to apply changes:

sudo systemctl restart apache2

8. (Optional) Set Up Git Daemon for Anonymous Read Access

sudo apt install git-daemon-run -y

Edit /etc/sv/git-daemon/run:

#!/bin/sh
exec 2>&1
echo 'git-daemon starting.'
exec chpst -ugitdaemon \
  "$(git --exec-path)"/git-daemon --verbose --reuseaddr \
    --base-path=/home/git /home/git

Enable and start the service:

sudo ln -s /etc/sv/git-daemon /etc/service/git-daemon

9. (Optional) Install Gitolite for Advanced Access Control

sudo apt install gitolite3 -y

Set up Gitolite:

sudo su - git
gl-setup /tmp/admin.pub  # Use admin's public key

10. Firewall Configuration

Allow SSH traffic (port 22) and optionally GitWeb (port 80):

sudo ufw allow 22
sudo ufw allow 80
sudo ufw enable

Maintenance Tips

  • To add new repositories: git init --bare in the git user's home directory
  • To add new users: append their public keys to /home/git/.ssh/authorized_keys
  • Regularly update Git: sudo apt update && sudo apt upgrade git

This setup provides a secure, functional Git server on Ubuntu that can be accessed via SSH. For larger teams or more complex requirements, consider GitLab or Gitea for a more feature-rich solution.