How to Set Up a Git Server on Ubuntu
Version control is essential for managing code efficiently, and hosting your own Git server can provide greater control over your repositories. In this guide, we’ll walk you through setting up a Git server on Ubuntu.
Prerequisites
- A server running Ubuntu (20.04 or later recommended)
- SSH access to the server
- Basic knowledge of Linux commands
Step 1: Install Git
First, update your package list and install Git:
sudo apt update
sudo apt install git -y
Verify the installation:
git --version
Step 2: Create a Git User
For security purposes, create a dedicated Git user:
sudo adduser git
Follow the prompts to set a password and user details.
Step 3: Set Up SSH Access
To allow remote access, add your public SSH key to the Git user’s authorized keys file.
Copy your local machine’s public key:
cat ~/.ssh/id_rsa.pub
Then, on the server:
sudo mkdir -p /home/git/.ssh
sudo nano /home/git/.ssh/authorized_keys
Paste the copied key and save the file. Set proper permissions:
sudo chown -R git:git /home/git/.ssh
sudo chmod 700 /home/git/.ssh
sudo chmod 600 /home/git/.ssh/authorized_keys
Step 4: Create a Bare Repository
A bare repository stores version history but does not have a working directory.
sudo mkdir -p /home/git/repos
sudo chown -R git:git /home/git/repos
cd /home/git/repos
git init --bare myrepo.git
Step 5: Configure Git Server Permissions
Ensure the Git user has the correct permissions:
sudo chown -R git:git /home/git/repos/myrepo.git
To prevent unwanted modifications, enforce proper permissions:
cd /home/git/repos/myrepo.git
chmod -R 755 .
Step 6: Clone the Repository
From your local machine, run:
git clone git@git-server:/home/git/repos/myrepo.git
Replace git-server
with your server’s IP or hostname.
Step 7: Push Changes
Navigate to your cloned repository, create a file, and push it to the remote server:
cd myrepo
touch README.md
git add README.md
git commit -m "Initial commit"
git push origin master
Conclusion
Setting up a Git server on Ubuntu provides a secure and flexible way to manage code repositories. By following these steps, you can host your own Git server and collaborate with your team efficiently.
How to Monitor System Performance Using htop and atop (F.A.Q)
Can I use Git without SSH?
Yes, you can configure Git to use HTTP/HTTPS with a web server like Apache or Nginx, but SSH is the most secure and commonly used method.
How do I add more users to the Git server?
Create additional users and add their SSH keys to /home/git/.ssh/authorized_keys
.
How do I back up my Git repositories?
You can back up by copying the entire /home/git/repos
directory to another location using rsync
or scp
.
0 Comments