keepcalculator

06 Numbers and Powers

Set Up a Private Git Server on a VPS

Card No. 06·73581 · Updated · Standard formula, shown below

This guide walks you through setting up your own private Git server on a VPS (Virtual Private Server). You’ll get a central place to store your repositories with SSH-based access, giving you full control over your code and no dependency on third-party hosting. By the end, you’ll be able to clone, push, and pull from your own server using standard Git commands.

What You’ll Need

  • A VPS running a modern Linux distribution (Ubuntu 22.04 or Debian 12 recommended). You can use any provider that gives you root SSH access.
  • SSH client installed on your local machine (usually preinstalled on macOS and Linux; on Windows, use PowerShell or install OpenSSH).
  • Basic familiarity with the command line and Git.
  • Git installed on your local machine (if not, install it from git-scm.com or your package manager).
  • A domain name (optional, but makes SSH config easier). If you skip it, you’ll use the server’s IP address.

Step-by-Step

  1. Connect to your VPS. Open a terminal and SSH into your server as a user with sudo privileges. For example: ssh user@your-server-ip. If you’re using a key pair, ensure the key is added to your SSH agent.
  2. Update the system. Run sudo apt update && sudo apt upgrade -y (for Debian/Ubuntu) to ensure all packages are current.
  3. Install Git. Run sudo apt install git -y. Verify with git --version.
  4. Create a dedicated Git user. This user will own all repositories and will be the account you SSH into for Git operations. Run: sudo adduser git. Follow the prompts to set a password (or skip if you prefer key-only access). This user should not have sudo privileges.
  5. Set up SSH keys for the git user. On your local machine, if you don’t have an SSH key pair, generate one with ssh-keygen -t ed25519 -C "your_email@example.com". Then copy your public key to the server: ssh-copy-id git@your-server-ip. This adds your public key to /home/git/.ssh/authorized_keys. Test with ssh git@your-server-ip – you should get a shell (we’ll restrict it later).
  6. Create a repositories directory. As the git user (or via sudo), create a directory to hold all repos: sudo mkdir /home/git/repos and set ownership: sudo chown git:git /home/git/repos.
  7. Restrict the git user to Git commands only. Edit the SSH configuration for the git user by adding a forced command. Create a file /home/git/.ssh/authorized_keys if it doesn’t exist. For each public key line, prefix it with: command="/usr/bin/git-shell -c \"$SSH_ORIGINAL_COMMAND\"",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty followed by the key. This ensures the user can only run Git commands, not a shell.
  8. Create a bare repository. On the server, as the git user, navigate to the repos directory and create a bare repo: cd /home/git/repos && git init --bare myproject.git. The .git suffix is a convention for bare repositories.
  9. Clone from your local machine. On your local machine, clone the empty repo: git clone git@your-server-ip:/home/git/repos/myproject.git. If you set up a domain, replace the IP with the domain (e.g., git@git.example.com:/home/git/repos/myproject.git). You’ll be prompted to confirm the host key on first connection.
  10. Add files and push. Add some files to the cloned repo, commit, and push: cd myproject, create a file, git add ., git commit -m "initial commit", then git push origin master (or main if you set that as default).
  11. Configure Git to use a shorter SSH alias (optional). On your local machine, edit ~/.ssh/config to add a Host entry: Host gitserver HostName your-server-ip User git. Then you can clone using git clone gitserver:repos/myproject.git.

Common Mistakes

  • Forgetting to make the repo bare. If you use git init instead of git init --bare, you’ll have a working directory that can cause conflicts. Always use bare for central servers.
  • Not setting permissions correctly. If the git user doesn’t own the repos directory, you’ll get permission errors when pushing. Ensure chown -R git:git /home/git/repos.
  • Using the wrong SSH key. Make sure the public key you added to the server matches the private key on your local machine. Test with ssh -T git@your-server-ip – you should see a Git shell message, not a shell prompt.
  • Ignoring the forced command syntax. If you misplace quotes or backslashes in the authorized_keys file, SSH may fail entirely. Double-check the syntax.

Tips

  • Use SSH keys instead of passwords. It’s more secure and convenient. Disable password authentication for the git user if you want extra security.
  • Set up a domain name. It makes SSH config cleaner and avoids remembering IP addresses. Add an A record pointing to your VPS.
  • Enable Git’s built-in hooks. You can add post-receive hooks to trigger deployments or notifications.
  • Back up your repos regularly. Use git clone --mirror or simply tar the repos directory to a safe location.
  • Use a firewall. Only allow SSH (port 22) and maybe HTTP/HTTPS if you plan to serve Git over those protocols later.

Troubleshooting

  • Permission denied (publickey). This means your SSH key isn’t being accepted. Check that the public key is in /home/git/.ssh/authorized_keys, that the file permissions are correct (chmod 700 ~/.ssh and chmod 600 ~/.ssh/authorized_keys), and that you’re connecting as the git user.
  • Connection refused. SSH might not be running on the server, or the firewall is blocking port 22. Check with sudo systemctl status ssh and sudo ufw status.
  • Fatal: ‘/home/git/repos/myproject.git’ does not appear to be a git repository. This often means the path is wrong or the repo doesn’t exist. Verify the repo exists on the server and that you’re using the correct path.
  • Push fails with ‘cannot lock ref’ or ‘unable to create …’ Check file permissions on the repo directory. The git user must have write access.
  • Git shell appears but commands don’t work. If you see a message like “fatal: unrecognized command ‘ls'”, it means the forced command is working correctly – you can only run Git commands.

Frequently Asked Questions

Can I use this setup for multiple users?

Yes. Add each user’s public key to the authorized_keys file. Each user can then access the repositories. For finer control, you can create separate users or use a tool like Gitolite.

How do I create a new repository on the server?

SSH into the server as the git user (or via sudo), navigate to /home/git/repos, and run git init --bare newrepo.git. Then clone it from your local machine.

Can I use HTTP/HTTPS instead of SSH?

Yes, but it requires additional setup (like a web server and authentication). SSH is simpler and more secure for a small team.

What is the difference between a bare and non-bare repository?

A bare repository has no working directory – it only contains the Git data. It’s meant to be a central hub for pushing and pulling. Non-bare repos are for development with a working copy.

How do I back up my Git server?

You can simply copy the entire /home/git/repos directory to another location. Alternatively, use git clone --mirror for each repo to create a full backup.

Is it safe to use the git user for other tasks?

No. The git user should only be used for Git operations. The forced command restricts it to Git commands, which is a security best practice.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →