Set Up Git Server through SSH Connection

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

Managing Repositories on Git Server Using Gitosis

Using the method in this post, every user will have accesses to all repositories in 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 through SSH 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 in /etc/passwd to forbid logging in for a shell for the git account (this may not work on Fedora 14 according to Slavi’s comment ).

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:

Wwe 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 through SSH 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 ongate.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 authorized 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
Jan. 18, 2010. Format and fix small mistakes.
26 Feb. 2010. Small changes.
25 Feb. 2010. A simple tutorial is added.
Jul. 16, 2001. Add gitosis’s suggestions and format the titles.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值