Introduction
Want complete control over your code repositories? Setting up a private Git server on Linux gives you flexibility, enhanced security, and avoids third-party limitations. This guide walks you through the process using basic Linux tools – no expensive software required!
Prerequisites
- A Linux machine (Ubuntu/CentOS/Debian) with sudo access
- SSH enabled (standard on most servers)
- Basic terminal familiarity (
cd
,mkdir
,nano
)
Step 1: Create a Dedicated Git User
sudo adduser git # Follow prompts to set password
sudo su - git # Switch to git user
mkdir ~/.ssh && touch ~/.ssh/authorized_keys
chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys
Why? Isolates Git operations for security. Clients will connect via this account.
Step 2: Add Developer SSH Keys
On each developer’s machine:
cat ~/.ssh/id_rsa.pub # Copy public key
On your Git server (as git
user):
nano ~/.ssh/authorized_keys # Paste each developer's key here
Pro Tip: Use ssh-copy-id git@your-server-ip
from client machines for easier setup.
Step 3: Create Your First Repository
# On server as git user:
mkdir /home/git/my-project.git
cd /home/git/my-project.git
git init --bare # Initialize bare repo (no working directory)
Bare repositories store version history without active files – ideal for central servers.
Step 4: Configure Repository Permissions
sudo chown -R git:git /home/git/my-project.git
sudo chmod -R 770 /home/git/my-project.git # Adjust for your user group
Security Note: Restrict access using Linux group permissions if needed.
Step 5: Clone & Test from Client Machine
git clone git@your-server-ip:/home/git/my-project.git
cd my-project
touch README.md
git add . && git commit -m "Initial commit"
git push origin main
If connection fails:
- Verify
authorized_keys
formatting - Check firewall (
sudo ufw allow 22
for SSH)
Managing Multiple Repositories
Repeat Step 3 for new projects:
git init --bare /home/git/new-project.git
Developers clone via:
git clone git@server:/home/git/new-project.git
Advanced Options
- Git Shell Restrictions:
sudo usermod -s /usr/bin/git-shell git # Restricts git user to Git commands only
- Web Interface: Install Gitea or GitLab for GUI management
- HTTPS Access: Configure Nginx/Apache as reverse proxy with SSL
Conclusion
You now have a fully functional private Git server! Benefits include:
🔒 Complete data ownership
⚡ Faster local network transfers
💸 Zero subscription costs
🔧 Customization freedom
Next Steps:
- Set up automated backups (
rsync
orcron
) - Explore Git hooks for CI/CD automation
- Add repository access controls via
gitolite
> Troubleshooting? Check SSH logs: tail -f /var/log/auth.log
Empower your team with a secure, self-hosted Git ecosystem! 🐧💻