Alpha Hydrae

Coding on the edge of the web...

Set Up Your Own Git Server

In addition to GitHub, I like having a private copy of my repositories on one of my own servers. It serves both as a secondary backup, and also to play with pushed commits as I’ll explain in a future post. The idea is to set up a server that can be accessed through SSH in the same way as GitHub, with a remote that looks like git@myserver.com:myrepo.git, and with public key authentication.

Setup

Install Git
1
2
3
4
5
# Fedora 17
yum install git-all

# Ubuntu 11
apt-get install git

Next, we need a Git user. We want this user to only have access to Git, so we’ll restrict his shell. On Fedora 17, the shell we want is /bin/git-shell. Run which git-shell if you’re not sure where it is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# create the user (the -d option specifies the home directory)
useradd -m -d /home/git git

# you must allow the git shell to be used on your system
vim /etc/shells
    # add this line
    /bin/git-shell

# set the git user's shell
usermod -s /bin/git-shell git

# set up public key authentication (as the git user)
su -s /bin/bash git
    cd
    mkdir .ssh && chmod 700 .ssh && cd .ssh
    touch authorized_keys && chmod 600 authorized_keys

And your Git user is ready. Everyone who needs access must have their public key in /home/git/.ssh/authorized_keys.

Usage

Add a repository on your server
1
2
3
4
5
# create bare repo (as the git user)
su -s /bin/bash git
    cd
    mkdir myrepo.git && cd myrepo.git
    git init --bare
Configure the remote on a repo
1
2
3
# add the remote with the correct domain and repo
git remote add myremote git@myserver.com:myrepo.git
git push myremote master

You can now push your secret commits in the privacy of your own server.

Meta

  • OS: Fedora 17
  • Git: 1.7.11.2

Comments