Git server on Linux — bare repo, SSH keys, and post-receive deploy

Took a while to get this working the first time; writing it down so I do not have to rediscover it.

Outline

Server

  • Git layout
    • Dedicated git user and group for repos
    • SSH keys so pushes do not need a password
    • post-receive hook deploys into the site directory
  • Web directory
    • Empty target tree ready to receive checkouts

Client

  • Local dev directory
  • Generate SSH keys
  • Normal git workflow

Hands-on

Build Git from source

https://www.kernel.org/pub/software/scm/git/

Download the latest .tar.gz to /usr/local.

Install

tar -zxf git-2.2.1.tar.gz 
cd git.2.2.1
make prefix=/usr/local/git all
make prefix=/usr/local/git install

yum install git-all is easier, but the version lags. On Alibaba Cloud CentOS 6.5 I was stuck on Git 1.7.2 from packages.

A custom prefix is not on PATH by default; extend /etc/profile:

vim /etc/profile
# Find PATH=/usr/local/php/bin:$PATH and extend it:
PATH=/usr/local/php/bin:/usr/local/git/bin:$PATH
# Save, open a new shell session

Bare repository

groupadd git
useradd git -g git
cd /home/git
mkdir repo.git   # name as you like
cd repo.git
git init --bare    # bare repo — objects and refs only
chown -R git:git /home/git/repo.git

For hardened SSH, some guides restrict the git user to git-shell only. After a source install:

# /etc/passwd
vim /etc/passwd
# e.g. change the login shell from:
git:x:502:503::/home/newbmiao:/bin/bash
# to:
git:x:502:503::/home/git:/usr/local/git/bin/git-shell
# Optional: copy allowed commands
cp /usr/local/git-2.2.1/contrib/git-shell-commands /home/git/
# SSH sessions as git then only run Git-over-SSH commands

Passwordless SSH

su git
cd /home/git/
ssh-keygen -C 'your@email.com' -t rsa   # defaults are fine

Do the same on each client. You get:

.ssh
├── id_rsa
└── id_rsa.pub    # server needs this in authorized_keys

On the server:

vim /home/git/.ssh/authorized_keys
# paste the client's id_rsa.pub
# restart sshd (and git-daemon if you use it)
/etc/init.d/git-daemon restart 
# git-daemon lives under /usr/local/git/libexec/git-core/git-daemon — copy to /etc/init.d/ if needed
/etc/init.d/sshd start

Client workflow

From a client (I used Git Bash on Windows):

# empty project dir
git init
vim test
# edit, save
git add test
git commit -m 'init test'
git remote add origin git@your_host_name:/home/git/repo.git
git push origin master

Others:

git clone git@your_host_name:/home/git/repo.git
cd repo
vim README
git commit -am 'fix for the README file'
git push origin master

Deploy hook

Central Git is done; I also wanted each push to refresh a web tree for testing.

# assume the site lives in /www/web
cd /home/git/repo.git/hooks
vim post-receive
# contents:
GIT_WORK_TREE=/www/web  git checkout -f
chown git:git post-receive
chmod +x post-receive

The next push checks out into /www/web.

Early on, even with the public key installed and both git-daemon and sshd running, git clone still asked for a password and then failed with Permission denied, please try again. Later it worked—probably file permissions on ~git or .ssh, but I never pinned down the exact SSH step. If you know why, I’d like to hear it.

Questions? Leave a comment below or email: newbvirgil@gmail.com