Setting up a git server accessible via ssh

For small personal projects I often use git to track my work. Sometimes, I’ll work from a different computer and wish I could clone the repository and continue where I left off. I recently set up a git server that allowed me to do this and all I needed was ssh access.

Provided you have a remote server located at gitserver with user admin, you can set one up by doing the following.

Log in to your server and create the git user.
user@local $ ssh admin@gitserver
admin@gitserver $ sudo useradd -m git

Locally, create the ssh-key pair that you’ll using to log in to your server and copy it over.
user@local $ ssh-keygen -t rsa -f ~/.ssh/id_rsa_gitserver
user@local $ ssh-copy-id -f ~/.ssh/ git@gitserver

Create a placeholder for the repository that you want to track. --bare is used here because you’ll be pushing your current repo to the server.
user@local $ ssh -i ~/.ssh/id_rsa_gitserver git@gitserver
git@gitserver $ mkdir ~/repo.git
git@gitserver $ cd !$ && git init --bare

Push your local copy over to the server. Here, I start a new shell with ssh-agent so key management is handled transparently.
user@local $ ssh-agent bash
user@local $ ssh-add ~/.ssh/id_rsa_gitserver
user@local $ cd ~/repo
user@local ~/repo $ git remote add origin ssh://git@gitserver[:port]/home/git/repo.git
user@local ~/repo $ git push origin master

Finally, lock down the git account
user@local $ ssh admin@gitserver
admin@gitserver $ sudo chsh -s /usr/bin/git-shell git

As a general ssh security tip: make sure that password-based login is disabled and public-keys are required when logging in to the server.

Now you’re all set! You can push changes as usual by using git push.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: