Home » Linux

Set up git server through ssh connection

By: Zhiqiang Ma On: Dec 21, 2009 Views: 736 Comments: 3 Print Email
Tags: , , , ,

If you need to set up a git server for multiple users which may contain contributors and administrator, I recommend using gitosis:

Setting Up Git Server Using Gitosis: http://pkill.info/b/1432/setting-up-git-server-using-gitosis/

Managing Repositories on Git Server Using Gitosis: http://pkill.info/b/1434/managing-repositories-on-git-server-using-gitosis/

Using the method in this post, every user will have ssh access to the git server and every user is administrator. This is okay for a small group of members who trust each other. But for a better solution, you should try gitosis.


In this post how to set up a basic git server and a more complex one (the git server is a server inside of a local area network) will be introduced.

A basic git server set up tutorial.

In this part we will build up a git server through ssh connection. We use ssh to pull or push data from or to git server. The git server can be directly connected. Suppose that we set up git server on machines example.org.

Server side git user and home

logon to the git server by ssh username@example.org. username is the account name that can sudo on the git server.

sudo yum install git
sudo useradd -m -d /lhome/git -u 1005 git
sudo vim /etc/passwd

Here we assume git’s home directory is /lhome/git. Then we can change git’s shell from /bin/bash to /usr/bin/git-shell to forbid logging in for a shell for the git account.

Add isa_key.pub to git’s .ssh/authorized_keys

ssh example.org  # log on to git server, using the account that can sudo
cp .ssh/id_rsa.pub /dev/shm/    # copy pub key to a temp directory
sudo su                         # operate in git's home as root
cd /lhome/git/.ssh
cp authorized_keys authorized_keys.bak   # backup before changing is a good habit
cat /dev/shm/id_rsa.pub >> authorized_keys # append pub key to git's autorized keys list

Create repository

log on example.org  # using the account that can sudo

sudo su git
cd /lhome/git
mkdir example.git    # the repository directory
cd example.git
git --bare init      # initial the repository, --bare means only objects is stored on server

First commit:

on local laptop:

mkdir example
cd example
git init
touch README
git add README
git commit -m 'first commit'
git remote add origin ssh://git@example.org/~/example.git
git push origin master

When programming:

# we need to clone the repository for one time:
git clone ssh://git@example.org/~/example.git 

# ====
# then every time we want to edit some files:

cd example
git pull  # pull the newest version from the repository

# after changing some files:

git commit -a -m 'msg'  # commit the changes with a message msg
git push # push the changes to the repository

*********************************************************

A more complex git server set up tutorial.

In this part we will build up a git server through ssh connection. We use ssh to pull or push data from or to git server. The git server is inside of a local area network. We use port forwarding to connect to it. Suppose that we set up git server on virtual machines vm111, and the gateway server of the net work which vm111 is inside of is gate.example.org. And port 22111 on gate.example.org is forwarded to vm111:22.

Server side git user and home

logon to the git server by ssh username@gate.example.org -p 22111. username is the account name that can sudo on the git server.

sudo yum install git
sudo useradd -m -d /lhome/git -u 1005 git
sudo vim /etc/passwd

Then change git’s shell from /bin/bash to /usr/bin/git-shell to forbid logging on for a shell for the git account.

Add isa_key.pub to git’s .ssh/authorized_keys

ssh gate.example.org -p 22111  # log on to vm111, using the account that can sudo
cp .ssh/id_rsa.pub /dev/shm/    # copy pub key to a temp directory
sudo su                         # operate in git's home as root
cd /lhome/git/.ssh
cp authorized_keys authorized_keys.bak   # backup before changing is a good habit
cat /dev/shm/id_rsa.pub >> authorized_keys # append pub key to git's autorized keys list

Create repository

log on gate.example.org -p 22111 # using the account that can sudo

sudo su git
cd /lhome/git
mkdir example.git    # the repository directory
cd example.git
git --bare init      # initial the repository, --bare means only objects is stored on server

First commit:

on local laptop:

mkdir example
cd example
git init
touch README
git add README
git commit -m 'first commit'
git remote add origin ssh://git@gate.example.org:22111/~/example.git
git push origin master

When programming:

# we need to clone the repository for one time:
git clone ssh://git@gate.example.org:22111/~/example.git 

# ====
# then every time we want to edit some files:

cd example
git pull  # pull the newest version from the repository

# after changing some files:

git commit -a -m 'msg'  # commit the changes with a message msg
git push # push the changes to the repository

Updated history
26 Feb. 2010. Small changes.
25 Feb. 2010. A simple tutorial is added.
Jul. 16, 2001. Add gitosis’s suggestions and format the titles.

Read more:

Digg del.icio.us Stumble Techorati Facebook Newsvine Reddit Twitter
Mixx LinkedIn Google Bookmark Yahoo Bookmark MySpace LiveJournal Blogger RSS feed
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...

3 Comments »

  • Anonymous said:

    hello,
    thank you for your useful article.
    can i use this in linux cent os 5.4?

  • Eric said:

    Hello. I haven’t try it on cent os 5.4. I make it run on Fedora 11. But it should work. You can have a try ;)
    As far as I know, you should install git by yourself since git is not in it’s repository.

  • Eric said:

    git isn’t in RHEL or CentOS’s repository. But the community supported EPEL has git. Users of CentOS or RHEL can use it to install git:

    https://fedoraproject.org/wiki/EPEL

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.