Self-Hosting Git Repositories Using SSH

Summary: Set up SSH-based Git hosting on your server.


Version control is at the heart of modern software development, and Git reigns as the dominant choice. While platforms like GitHub and GitLab offer powerful hosting solutions, developers and organizations often prefer self-hosting their own Git repositories for greater control, privacy, and customization. One straightforward way to achieve this is by leveraging SSH for secure access. In this guide, we’ll walk through setting up your own SSH-based Git server.


Why Self-Host with SSH?

Self-hosting Git repositories has several advantages:

  • Full control: Manage users, access, and configurations as you see fit.
  • Security: Keep your code private and under your policies.
  • Customization: Integrate with your own infrastructure or CI/CD systems.
  • No Vendor Lock-in: Avoid dependence on third-party platforms.

Setting up Git with SSH ensures that repository access is both secure (with encrypted communication) and convenient (using public-private key authentication).


Prerequisites

Before you begin, ensure you have:

  • A server running Linux (any common distribution).
  • SSH access to the server.
  • git installed on both client and server.
  • At least one user account (other than root) for repository hosting.

Step 1: Install Git on the Server

Most Linux distributions come with Git in their repositories.

For Ubuntu/Debian:

sudo apt update
sudo apt install git

For CentOS/Fedora:

sudo dnf install git

Step 2: Create a Dedicated Git User

For security, it's best to create a separate user (commonly called git) to manage repositories.

sudo adduser git

Set a secure password, or better yet, disable password login in favor of SSH key authentication.


Step 3: Set Up SSH Key Authentication

  1. Generate a key pair on your local machine:

    ssh-keygen -t ed25519 -C "your_email@example.com"
    

    Accept default location (usually ~/.ssh/id_ed25519) and set a passphrase if desired.

  2. Copy your public key to the server:

    ssh-copy-id git@your-server-ip
    

    Or manually:

    • Copy the contents of ~/.ssh/id_ed25519.pub
    • On the server, add it to /home/git/.ssh/authorized_keys
  3. Test the connection:

    ssh git@your-server-ip
    

    You should be logged in without a password prompt.


Step 4: Create a Bare Repository

A bare Git repository is intended for collaboration and does not keep a working code checkout.

  1. Log in as the git user:

    ssh git@your-server-ip
    
  2. Create a directory for repositories (if you haven’t already):

    mkdir -p ~/repositories
    cd ~/repositories
    
  3. Initialize a bare repository:

    git init --bare myproject.git
    

Your repo now lives at /home/git/repositories/myproject.git.


Step 5: Clone the Repository from a Client

Back on your local machine:

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

You now have a local copy. Add, commit, and push changes as usual.


Step 6: Manage Access for Multiple Users

To allow others to contribute:

  1. Collect their SSH public keys.
  2. Add each public key to /home/git/.ssh/authorized_keys.

To distinguish access, you can:

  • Create several user accounts (one per developer), or
  • Use the git-shell for restricted access, or
  • Set up SSH key options to enforce command restrictions.

Step 7: (Optional) Improve Security with Git Shell

Restrict the git user to git-only commands by setting its shell to git-shell:

sudo chsh -s $(which git-shell) git

This prevents regular shell access for the git user.


Maintenance Tips

  • Regular Backups: Use rsync, scp, or automated scripts.
  • Monitor Activity: Check ~/.ssh/authorized_keys entries frequently.
  • Keep Software Updated: Apply security updates to the server and Git.

Conclusion

Self-hosting Git repositories via SSH offers a balance of control, privacy, and simplicity. With just a few steps, you can establish a secure, robust Git server for yourself or your team. This foundational setup can later be expanded with interfaces like Gitea or integrations with CI/CD pipelines for larger operations.

Ready to level up? Explore access control tools (like gitolite) or web interfaces for even more capabilities!


Further Reading


Happy coding and self-hosting!