How to Setup a Git Server

By 

Updated on

6 min read

Git Private Server

When it comes to Git hosting, you have a number of options available. GitHub, Gitlab and Bitbucket are popular solutions, but running your own Git server is an alternative worth considering.

Setting up a Git Server allows you to create private repositories without the restrictions of the providers’ free plans.

In this guide, we will explain how to set up a bare Git server on Linux. This setup is good if you have few repositories and the collaborators are tech-savvy. Otherwise, you should consider installing a self-hosted git application such as Gitea , Gogs , or Gitlab .

The Git server can be set up on any remote Linux machine or even on your local system.

Setting Up the Git Server

The first step is to install Git on your server.

If you are using Debian or Ubuntu, refresh the local package index and install git by running the following commands as sudo user:

Terminal
sudo apt update && sudo apt install git

On Fedora, RHEL, and derivatives, install the git package with:

Terminal
sudo dnf install git

Next, create a new user that will manage the Git repositories:

Terminal
sudo useradd -r -m -U -d /home/git -s /bin/bash git

Because useradd creates the account with a locked password, replace the password field with a value that cannot match a valid password:

Terminal
sudo usermod --password 'NP' git

The user home directory is set to /home/git, and all repositories will be stored under this directory. The git user cannot authenticate with a password, but SSH key authentication remains available.

Switch to user “git” using the su command:

Terminal
sudo su - git

Run the following commands to create the SSH directory and set the correct permissions :

Terminal
mkdir -p ~/.ssh && chmod 0700 ~/.ssh

Create a file named ~/.ssh/authorized_keys which will hold the authorized users’ SSH keys:

Terminal
touch ~/.ssh/authorized_keys && chmod 0600 ~/.ssh/authorized_keys

The server setup is complete, and you are now ready to create your first Git repository.

Run the following command to initiate a new empty repository:

Terminal
git init --bare --initial-branch=main ~/projectname.git

You can name the directory as you want. The important thing is to create the repository under the “git” user home directory.

output
Initialized empty Git repository in /home/git/projectname.git/

Return to your administrative user:

Terminal
exit

Configuring Local Git Repository

To be able to push the local git changes to the Git server, you will need to add your local user SSH public key to the remote “git” user’s authorized_keys file.

If you already have an Ed25519 SSH key pair on your local system, display the public key by typing:

Terminal
cat ~/.ssh/id_ed25519.pub

The output should look something like the following:

output
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGG7Q3K8xW1v2J5h9qR6uY4nP0mL8cF2sA7dE3bN9kT your_email@domain.com

If you get an error message saying No such file or directory, it means that you do not have an SSH key pair generated on your local machine.

To generate a new SSH key pair , use the following command. The ed25519 type is the recommended modern default, and it produces a shorter, faster key:

Terminal
ssh-keygen -t ed25519 -C "your_email@domain.com"

After the key is created, display its public part:

Terminal
cat ~/.ssh/id_ed25519.pub

If you need to interoperate with an older system that does not support ed25519, generate an RSA key instead:

Terminal
ssh-keygen -t rsa -b 4096 -C "your_email@domain.com"

Display the RSA public key with:

Terminal
cat ~/.ssh/id_rsa.pub

Copy your public key from the appropriate cat command above and return to an administrator shell on the Git server.

On the server, open your text editor and paste the public key that you copied from your local machine into the ~/.ssh/authorized_keys file:

Terminal
sudo nano /home/git/.ssh/authorized_keys

The entire public key text should be on a single line.

We are assuming that the Git package is already installed on your local machine. If not, install it in the same way as explained in the previous sections.

If you have an existing unversioned project, navigate to the project directory. If you are starting from scratch, create the project directory, and navigate to it:

Terminal
cd /path/to/local/project

Initialize a Git repository with main as the initial branch:

Terminal
git init -b main

The last step is to add the git remote to your local repository:

Terminal
git remote add origin git@git_server_ip:projectname.git

Do not forget to replace git_server_ip with your Git server hostname or IP address.

To verify that everything is set up correctly, create a test file :

Terminal
touch test_file

Add the changes to the staging area:

Terminal
git add .

Commit the changes:

Terminal
git commit -m "descriptive message"

Push the local repository changes to a remote repository:

Terminal
git push -u origin main

If everything is set up correctly, the output should look something like this:

output
Counting objects: 3, done.
Writing objects: 100% (3/3), 218 bytes | 218.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git_server_ip:projectname.git
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.

To add a new collaborator, copy their public SSH key to the “git” user’s ~/.ssh/authorized_keys file, one key per line.

You can use the same steps to create new repositories. Remember that the bare repository must exist on the server before you add the git remote to your local repository.

Troubleshooting

If a push or clone fails, the problem is almost always with SSH access or file permissions. Here are the most common errors and how to fix them.

Permission denied (publickey)
The server rejected your key. Confirm that your local public key is present in /home/git/.ssh/authorized_keys on the server and that the entire key sits on a single line. Test the connection directly with ssh git@git_server_ip to see whether the key is accepted.

Authentication fails even though the key is correct
SSH refuses keys when the permissions are too open. The .ssh directory must be 0700 and authorized_keys must be 0600, both owned by the “git” user. Reset them with sudo chown -R git:git /home/git/.ssh, then sudo chmod 700 /home/git/.ssh and sudo chmod 600 /home/git/.ssh/authorized_keys.

Could not resolve hostname or Connection refused
The remote address is wrong or SSH is not reachable. Verify the value you used in git remote add, make sure the sshd service is running on the server, and confirm that port 22 is open in the firewall.

Conclusion

You now have your own private Git server running over SSH, with full control over who can push and pull. As your needs grow, the same machine can host a self-hosted application such as Gitea for a web interface and pull requests.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page