To set up Git on a Linux server, install Git via your distro’s package manager, configure your user identity, secure SSH access, and create a bare repository for centralized collaboration. Then add your remote, clone from clients, and push changes. This guide walks you through installation, configuration, security, and best practices end-to-end.
If you’re wondering how to set up Git on a Linux server safely and correctly, you’re in the right place. In this step-by-step guide, we’ll install Git, configure it for the first time, enable SSH authentication, create a proper bare repository, and apply security best practices so your team can push and pull confidently.
Prerequisites and What You’ll Build
This tutorial is optimized for the primary keyword: how to set up Git on a Linux server. We’ll cover Debian/Ubuntu, RHEL/CentOS/AlmaLinux/Rocky, Fedora, openSUSE, and Arch. You’ll set up a Git-only user, a secure SSH workflow, and a production-ready bare repository that multiple developers can use as a central remote.
Step 1: Install Git on Your Linux Server
Update your repositories and install Git using your distribution’s package manager. This ensures you get verified binaries and resolves dependencies.
Debian/Ubuntu
sudo apt update
sudo apt install -y git
git --version
RHEL/CentOS/AlmaLinux/Rocky
# On older RHEL/CentOS:
sudo yum install -y git
# On RHEL 8+/Alma/Rocky:
sudo dnf install -y git
git --version
Fedora
sudo dnf install -y git
git --version
openSUSE
sudo zypper refresh
sudo zypper install -y git
git --version
Arch Linux
sudo pacman -Syu --noconfirm git
git --version
Installing the Latest Git from Source (Optional)
Distros sometimes lag behind the latest Git. Compile from source if you need the newest features or performance fixes.
# Build requirements (examples)
# Debian/Ubuntu:
sudo apt update && sudo apt install -y make gcc libssl-dev libcurl4-gnutls-dev libexpat1-dev gettext unzip
# RHEL/Alma/Rocky/Fedora:
sudo dnf groupinstall -y "Development Tools"
sudo dnf install -y curl-devel expat-devel openssl-devel perl-CPAN gettext-devel
# Download and build
cd /usr/src
sudo curl -LO https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.46.0.tar.xz
sudo tar -xf git-2.46.0.tar.xz
cd git-2.46.0
sudo make prefix=/usr/local all
sudo make prefix=/usr/local install
# Verify
git --version
Step 2: Configure Git (Global Settings)
Set your identity and sensible defaults. These values apply system-wide for your user and are required for commits.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# Recommended quality-of-life settings
git config --global init.defaultBranch main
git config --global color.ui auto
git config --global core.editor "nano" # or vim
# If working cross-platform with Windows users:
git config --global core.autocrlf input # Linux/macOS safe default
Step 3: Set Up SSH Keys for Secure Access
SSH keys offer secure, passwordless authentication. Generate a key pair on your client machine (developer workstation) and place the public key on the server.
# On your client machine:
ssh-keygen -t ed25519 -C "you@example.com"
# Press Enter to accept defaults and set a passphrase
# Copy the public key to the server:
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@your-server-ip
# Test:
ssh -T user@your-server-ip
If ssh-copy-id isn’t available, paste the output of cat ~/.ssh/id_ed25519.pub into ~/.ssh/authorized_keys on the server, then set secure permissions:
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "ssh-ed25519 AAAA... you@example.com" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
Step 4: Create a Git-Only User and a Bare Repository
A bare repository stores Git data without a working tree and is ideal for a central server. We’ll create a restricted “git” user and lock it to git-shell so it can only run Git commands.
# Create a system user for Git hosting
sudo adduser \
--system \
--shell /usr/bin/git-shell \
--group \
--home /srv/git \
git
# Create a bare repository
sudo -u git mkdir -p /srv/git
sudo -u git git init --bare /srv/git/project.git
# Optionally set a description
echo "Project repo" | sudo tee /srv/git/project.git/description
# Ensure proper permissions
sudo chown -R git:git /srv/git/project.git
Add developers’ public keys to the git user’s authorized_keys file so they can push/pull:
sudo -u git mkdir -p /srv/git/.ssh
sudo -u git touch /srv/git/.ssh/authorized_keys
sudo chmod 700 /srv/git/.ssh
sudo chmod 600 /srv/git/.ssh/authorized_keys
# Append each developer's public key
# e.g., echo "ssh-ed25519 AAAA... dev@laptop" | sudo tee -a /srv/git/.ssh/authorized_keys
Step 5: Clone and Push from a Client
On a developer machine, clone the repository via SSH, create work, and push to the central server. Replace the IP or hostname accordingly.
# From developer workstation:
git clone git@your-server-ip:/srv/git/project.git
cd project
# Add code and push
echo "# Demo" > README.md
git add README.md
git commit -m "Initial commit"
git push -u origin main
If the default branch is not main and you want it to be, run git symbolic-ref HEAD refs/heads/main inside the bare repo before the first push, or set init.defaultBranch globally as shown earlier.
Optional: Smart HTTP with Apache or NGINX
SSH is the simplest approach. If you must serve Git over HTTP(S), enable git-http-backend. Below is a minimal Apache example. Use HTTPS in production and restrict access with HTTP auth or OAuth.
# Apache example (Debian/Ubuntu):
sudo a2enmod cgi alias env
sudo tee /etc/apache2/conf-available/git.conf > /dev/null <<'EOF'
SetEnv GIT_PROJECT_ROOT /srv/git
SetEnv GIT_HTTP_EXPORT_ALL
ScriptAlias /git/ /usr/lib/git-core/git-http-backend/
<Directory "/srv/git">
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
EOF
sudo a2enconf git
sudo systemctl reload apache2
With this, repositories appear at https://your-domain/git/project.git. Ensure your bare repo has a git-daemon-export-ok or use authentication to control access.
Security Best Practices for a Git Server
- Use a dedicated
gituser withgit-shellto restrict commands. - Disable SSH password authentication; allow key-based login only.
- Limit SSH to your team’s IPs via firewall (UFW, firewalld) and consider Fail2ban.
- Set correct permissions on
/srv/gitand~/.sshfiles (no world-writable bits). - Keep Git and OpenSSH updated to the latest stable version.
- Back up
/srv/gitregularly and test restores.
Maintenance: Updates, Backups, and Hooks
- Update Git regularly:
sudo apt upgrade gitorsudo dnf upgrade git. - Back up the bare repos with rsync or snapshots; store offsite.
- Use server-side hooks (e.g.,
pre-receive,update,post-receive) for CI triggers, branch policies, or notifications. - Monitor disk space and inode usage; Git repositories can grow fast.
Troubleshooting Common Errors
Permission denied (publickey)
Ensure your public key is in /srv/git/.ssh/authorized_keys, file permissions are strict (600), and the directory is 700. Test with ssh -v git@server to see which keys are offered.
fatal: Could not read from remote repository
Verify the repository path (/srv/git/project.git), ownership (git:git), and that the repo is bare. Confirm SSH connectivity and correct remote URL.
non-fast-forward updates rejected
Pull or fetch and rebase/merge locally before pushing. Enforce protected branches with server-side hooks or in your Git platform.
git: command not found
Git may not be installed for non-interactive shells. Ensure /usr/bin or /usr/local/bin is in PATH for the git user and that installation paths match your build.
Real-World Tips from Hosting Experience
- Use separate repositories per project; avoid monolithic repos for unrelated code.
- For teams, consider Forgejo/Gitea/GitLab for web UI, permissions, and CI—host them on your Linux server.
- Snapshot your server before major upgrades; roll back quickly if needed.
- Measure clone/push speed; enable compression or upgrade to NVMe storage for large monorepos.
When to Use Managed Infrastructure
If you want a hassle-free setup with predictable performance, a managed Linux VPS or Dedicated Server is ideal. At YouStable, our SSD/NVMe VPS plans, root SSH access, and snapshot backups make hosting Git servers simple, with 24×7 support on hand if you need help securing or scaling your repositories.
FAQs: How to Set Up Git on Linux Server
How do I install Git on Ubuntu quickly?
Run sudo apt update && sudo apt install -y git and verify with git --version. For the latest Git, use the official PPA or build from source, but the repository version is stable and sufficient for most teams.
What is a bare repository and why use it on a server?
A bare repo contains only Git data without a working directory. It’s designed for central remotes that multiple developers push to. Create it with git init --bare /srv/git/project.git and restrict access via SSH keys.
How do I secure my Git server?
Use a dedicated git user with git-shell, disable password SSH logins, enforce key-based auth, keep packages updated, set strict file permissions, and back up repos. Consider a firewall and Fail2ban to reduce brute-force attempts.
Can I serve Git over HTTPS instead of SSH?
Yes. Enable git-http-backend with Apache or NGINX and use TLS certificates. HTTPS is useful behind corporate proxies. However, SSH is simpler to set up and typically faster for developer workflows.
What’s the best way to back up Git repositories?
Regularly snapshot or rsync the entire /srv/git directory to offsite storage. For consistency, pause writes during backup (maintenance window) or use filesystem snapshots (LVM/ZFS) to capture atomic states of active repositories.
Conclusion
Setting up Git on a Linux server is straightforward: install Git, configure global settings, secure SSH, and host a bare repository. With the security and maintenance practices outlined here, you’ll have a reliable remote for your team. Need a fast, secure home for your repos? A YouStable Linux VPS is a battle-tested foundation.