Setting up your own git server on Ubuntu

This will create a new user ‘gitosis’ and prepare a structure for repositories in /srv/gitosis. Now let’s initialize a gitosis-admin repo – it is used for managing repositories and access

Of course there’s always an option to use github. And if you’re working on an open source project, or want to concentrate on coding and not system administration, github is a lot better option than setting up and managing your own git server (I’ve been so impressed by @defunkt‘s presentation on #frozenrails, that started recommending github to everyone 🙂 But if you already pay for a virtual machine somewhere (like Linode), then setting up your own git server might be a viable option, especially that it is sooo easy.

The following instructions have been verified on Ubuntu 10.04 Lucid Lynx, but should work at least on Ubuntu 9.04 and 9.10 just as well.

Let’s start with installing gitosis itself. Issue the following command on the server:

sudo apt-get install gitosis

This will create a new user ‘gitosis’ and prepare a structure for repositories in /srv/gitosis. Now let’s initialize a gitosis-admin repo – it is used for managing repositories and access rights.

sudo -H -u gitosis gitosis-init < ~/tmp/my_public.key

You need to have a public key for accessing it. If you don’t have one yet, you can use

ssh-keygen -t rsa

command on your local machine to generate public/private key pair. Copy public key to the server before initializing gitosis-admin repository.

Now with gitosis-admin repo initialized on the server – let’s clone it to the local computer.

git clone gitosis@myserver.com:gitosis-admin.git

If you see an error like:

Permission denied (publickey).
fatal: The remote end hung up unexpectedly

That is most likely because you restricted access to you server via ssh to only certain users, and gitosis is not one of them. Edit /etc/ssh/sshd_config, find AllowUsers line and add gitosis to the list.

Now that you have successfully cloned gitosis-admin repo to your local computer, you can add  new users and new repositories.

To add new repository, edit gitosis.conf and add lines like:

[group myrepo]
writable = myrepo
members = user@computer

After that commit and push the changes to gitosis-admin project:

git commit -a -m "Added myrepo repository"

Now you can clone this new repository to your local machine (note .git added to the name of the repository):

git clone gitosis@mygitserver.com:myrepo.git

To allow new user to access your repository, get this user’s public key, copy it to gitosis-admin/keydir as newuser@computer.pub, then edit gitosis.conf and add newuser@computer (without .pub) to the list of members:

[group myrepo]
writable = myrepo
members = user@computer newuser@computer

Now add new files and commit and push changes to git server (make sure you really add all files and don’t forget to push – these are quite common mistakes when adding new users).

git add .
git commit -m "Added user newuser"
git push

With a freshly cloned empty repository you’ll need to add you first files, do a commit and push origin master:

vim README.txt
git commit -a -m "Added readme"
git push origin master

Now you can git pull and git push as much as you like.


Comments

45 responses to “Setting up your own git server on Ubuntu”

  1. sudo -H -u git gitosis-init < ~/tmp/my_public.key

    should read…

    sudo -H -u gitosis gitosis-init < ~/tmp/my_public.key

    No git user was found using the first example. After installing gitosis, the user created is "gitosis", not "git".

    Good tutorial by the way. Yours is the first I have come across which made it work.

    Many thanks.

  2. Thank you, Ethan. I corrected the guide.

  3. diamond diana

    i cannot get this to work.
    i am basically stuck at the first step. when i am trying to clone the git repo i get the following error

    fatal: ‘gitosis-admin.git’ does not appear to be a git repository
    fatal: The remote end hung up unexpectedly

    i am using ubuntu 10.04. any ideas?

  4. What is the exact command you use to do the clone? Do you have anything extra before “gitosis-admin.git”? Do you have correct key to access the remote machine – i.e. does does ssh gitosis@yourserver.com work?

  5. Srihari

    As you said “sudo apt-get install gitosis”
    will create a new user ‘gitosis’ and prepare a structure for repositories in /srv/gitosis.
    Is there a way to change the location from /srv/gitosis to say for example /home/mygit or something ?

  6. @Diana instead of using the above scp like syntax for addressing the repo you can use git clone ssh://gitosis@myserver.com/gitosis-admin.git

  7. TheWakeUpCall

    Hi, I have tried to follow your guide but I can’t seem to get it to work. I get to the stage where I put ‘git clone gitosis@myserver.com:gitosis-admin.git’ but I just get asked for a password for the gitosis account. I have searched the internet far and wide and cannot find a solution. My key is definitely right, as I checked the settings in gitosis and it matches that of my machine.

    Any clues?

  8. Thanks for the instructions. I was able to get it set up. I am doing a connection from an svn repository, so I created the MyRepo.git with “git svn clone” and then copied the results into /srv/gitosis/repositories”.

    Since I created these from svn repositories, do you have methods to continue to update the gitosis from

  9. Bjørn T.

    You probably want a git push after the git commit -a -m “Added myrepo repository” as well…

  10. A Group section defines which groups there are and what users are in them and in what repos they have write access.
    Thank you!

  11. after adding README.txt you first have to “git add .”
    Great simple walkthrough though! thanks!

  12. […] I didnt confuse you too much on the way. This guide uses parts from Ivan Kuznetsov’s guide, the gelp with the ssh came from the ssh manpage. This entry was posted in Guide and tagged […]

  13. […] here’s some I followed, but they all have slight variations in the approach: here, here, and here. It seems somewhat hit and miss whether it works or not (as you’ll find if you google for […]

  14. […] followed this guide http://www.ivankuznetsov.com/2010/05…on-ubuntu.html but when I doing the clone the password isn’t […]

  15. DavidZoope

    kamagra pas cher: kamagra pas cher – kamagra en ligne

  16. RobertFloob
  17. BernardBroam

    cialis sans ordonnance: Cialis en ligne – Cialis sans ordonnance pas cher tadalmed.shop

  18. Bradleynag

    pharmacie en ligne: pharmacie en ligne – pharmacie en ligne france fiable pharmafst.com

  19. PeterVof

    п»їpharmacie en ligne france pharmacie en ligne pharmacie en ligne france fiable pharmafst.shop

  20. BernardBroam

    kamagra en ligne: kamagra livraison 24h – kamagra 100mg prix

  21. BillieOxymn

    Tadalafil 20 mg prix sans ordonnance: Tadalafil 20 mg prix en pharmacie – Acheter Cialis 20 mg pas cher tadalmed.shop

  22. RobertFloob

    http://tadalmed.com/# Cialis en ligne

  23. Bradleynag

    achat kamagra: kamagra livraison 24h – Kamagra pharmacie en ligne

  24. BernardBroam

    Achat Cialis en ligne fiable: cialis sans ordonnance – Tadalafil sans ordonnance en ligne tadalmed.shop

  25. BillieOxymn

    Pharmacie Internationale en ligne: Meilleure pharmacie en ligne – п»їpharmacie en ligne france pharmafst.com

  26. Bradleynag

    Tadalafil 20 mg prix en pharmacie: Cialis sans ordonnance pas cher – Acheter Viagra Cialis sans ordonnance tadalmed.shop

  27. BernardBroam

    Tadalafil achat en ligne: Tadalafil achat en ligne – Cialis sans ordonnance pas cher tadalmed.shop

  28. PeterVof

    Acheter Cialis 20 mg pas cher Cialis sans ordonnance 24h Pharmacie en ligne Cialis sans ordonnance tadalmed.com

  29. RobertFloob

    http://pharmafst.com/# pharmacie en ligne

  30. BernardBroam

    pharmacie en ligne france pas cher: pharmacie en ligne france fiable – pharmacie en ligne avec ordonnance pharmafst.com

  31. BillieOxymn

    kamagra 100mg prix: kamagra pas cher – Achetez vos kamagra medicaments

  32. Bradleynag

    achat kamagra: kamagra gel – achat kamagra

  33. BernardBroam

    Acheter Cialis 20 mg pas cher: Tadalafil sans ordonnance en ligne – Acheter Cialis 20 mg pas cher tadalmed.shop

  34. RobertFloob

    https://tadalmed.shop/# Acheter Cialis 20 mg pas cher

  35. PeterVof

    kamagra gel Kamagra Oral Jelly pas cher achat kamagra

  36. BillieOxymn

    Pharmacie en ligne Cialis sans ordonnance: cialis generique – Cialis sans ordonnance pas cher tadalmed.shop

  37. Bradleynag

    Achetez vos kamagra medicaments: Kamagra Oral Jelly pas cher – kamagra livraison 24h

  38. BernardBroam

    acheter mГ©dicament en ligne sans ordonnance: pharmacie en ligne sans ordonnance – pharmacie en ligne pas cher pharmafst.com

  39. RobertFloob

    http://pharmafst.com/# pharmacie en ligne france pas cher

  40. PeterVof

    Tadalafil achat en ligne Cialis sans ordonnance pas cher Acheter Cialis 20 mg pas cher tadalmed.com

  41. Bradleynag

    kamagra 100mg prix: achat kamagra – kamagra livraison 24h

  42. BernardBroam

    kamagra 100mg prix: Acheter Kamagra site fiable – kamagra gel

  43. RobertFloob

    http://pharmafst.com/# acheter mГ©dicament en ligne sans ordonnance

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.