How to Setup a Git 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:
sudo apt update && sudo apt install gitOn Fedora, RHEL, and derivatives, install the git package with:
sudo dnf install gitNext, create a new user that will manage the Git repositories:
sudo useradd -r -m -U -d /home/git -s /bin/bash gitBecause useradd creates the account with a locked password, replace the password field with a value that cannot match a valid password:
sudo usermod --password 'NP' gitThe 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:
sudo su - gitRun the following commands to create the SSH directory and set the correct permissions :
mkdir -p ~/.ssh && chmod 0700 ~/.sshCreate a file named ~/.ssh/authorized_keys which will hold the authorized users’ SSH keys:
touch ~/.ssh/authorized_keys && chmod 0600 ~/.ssh/authorized_keysThe 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:
git init --bare --initial-branch=main ~/projectname.gitYou can name the directory as you want. The important thing is to create the repository under the “git” user home directory.
Initialized empty Git repository in /home/git/projectname.git/Return to your administrative user:
exitConfiguring 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:
cat ~/.ssh/id_ed25519.pubThe output should look something like the following:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGG7Q3K8xW1v2J5h9qR6uY4nP0mL8cF2sA7dE3bN9kT your_email@domain.comIf 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:
ssh-keygen -t ed25519 -C "your_email@domain.com"After the key is created, display its public part:
cat ~/.ssh/id_ed25519.pubIf you need to interoperate with an older system that does not support ed25519, generate an RSA key instead:
ssh-keygen -t rsa -b 4096 -C "your_email@domain.com"Display the RSA public key with:
cat ~/.ssh/id_rsa.pubCopy 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:
sudo nano /home/git/.ssh/authorized_keysThe 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:
cd /path/to/local/projectInitialize a Git repository with main as the initial branch:
git init -b mainThe last step is to add the git remote to your local repository:
git remote add origin git@git_server_ip:projectname.gitDo 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 :
touch test_fileAdd the changes to the staging area:
git add .Commit the changes:
git commit -m "descriptive message"Push the local repository changes to a remote repository:
git push -u origin mainIf everything is set up correctly, the output should look something like this:
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 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