Skip to content

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.

Share

11 Comments

  1. Ethan Cane wrote:

    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.

    Tuesday, May 18, 2010 at 19:46 | Permalink
  2. Thank you, Ethan. I corrected the guide.

    Tuesday, May 18, 2010 at 21:40 | Permalink
  3. diamond diana wrote:

    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?

    Sunday, May 30, 2010 at 21:57 | Permalink
  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?

    Monday, May 31, 2010 at 19:02 | Permalink
  5. Srihari wrote:

    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 ?

    Saturday, June 19, 2010 at 12:59 | Permalink
  6. Gunjan wrote:

    @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

    Monday, August 23, 2010 at 9:14 | Permalink
  7. TheWakeUpCall wrote:

    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?

    Monday, October 18, 2010 at 23:37 | Permalink
  8. Sabba Hillel wrote:

    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

    Tuesday, March 8, 2011 at 20:28 | Permalink
  9. Bjørn T. wrote:

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

    Tuesday, June 21, 2011 at 10:29 | Permalink
  10. joey wrote:

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

    Wednesday, October 12, 2011 at 13:28 | Permalink
  11. Joey wrote:

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

    Saturday, October 22, 2011 at 14:01 | Permalink

5 Trackbacks/Pingbacks

  1. […] http://www.ivankuznetsov.com/2010/05/setting-up-your-own-git-server-on-ubuntu.html […]

  2. […] Great primer – Link […]

  3. […] 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 […]

  4. […] 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 […]

  5. Gitosis as git server | Informacyjny Bloguś on Tuesday, May 15, 2012 at 7:10

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

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*