Setting Up Git Server through SSH Connection
Git server throught SSH is easy and fast to set up, although 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 gitolite or gitosis.
If you need to set up a git server for multiple users which may contain contributors and administrator, you may try gitolite as recommended by the gitolite author. A good tutorial for gitolite is here: gitolite tutorial.
If you prefer gitosis, please refer to: Setting Up Git Server Using Gitosis and Managing Repositories on Git Server Using Gitosis. The gitosis proves quit stable from my experience, although it does not have as many features as gitolite.
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 have administrator privilege (or can sudo) on the git server.
Install git package
# yum install git
Add the user for git
# useradd -m -d /lhome/git -u 1005 git
Configure the git user’s shell
# 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. To make this work, the /usr/bin/git-shell should be put into /etc/shells to avoid “user ‘git’ has invalid shell, rejected” error. (Thanks to Tiago for this)
Add isa_key.pub to git’s .ssh/authorized_keys
log on to git server, using the account that have root or sudo privilege
ssh username@example.org
Copy pub key to a temp directory
# cp .ssh/id_rsa.pub /dev/shm/
operate in git’s home as root
# cd /lhome/git/.ssh
backup before changing is a good habit
# cp authorized_keys authorized_keys.bak
append pub key to git’s autorized keys list
# cat /dev/shm/id_rsa.pub >> authorized_keys
Create repository
log on example.org using the account that can edit git’s files.
Create the repository directory (as the git user on server)
# su - git $ cd ~ $ mkdir example.git
Initial the repository, –bare means only objects is stored on server (as the git user on server)
# su - git $ cd ~/example.git $ git --bare init
First commit:
The first commit and push on local machine will create the initial repository.
Initialize the local repository
$ mkdir example $ cd example $ git init
Add a initial empty README file
$ touch README
Add README to the repository
$ git add README
Commit the changes (adding a file)
$ git commit -m 'first commit'
Add the remote git repository address
$ git remote add origin ssh://git@example.org/~/example.git
Push the commit to remote repository
$ 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 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, the gateway server of the net work which vm111 is inside of is gate.example.org, and port 22111 on gate.example.org is port 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.
# yum install git # useradd -m -d /lhome/git -u 1005 git # 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. And remember to set the /etc/shells file (refer to the “basic git” section above).
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 su - git # operate in git's hom $ 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
# 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
Tags: Client config, Git, SSH, Tutorial
Update history: Jan. 18, 2010. Format and fix small mistakes. 26 Feb. 2010. Small changes. 25 Feb. 2010. A simple tutorial is added. Jul. 16, 2010. Add gitosis's suggestions and format the titles. Aug. 25, 2011. Fix the problem with git-shell. Nov. 22. Revise for better writing.
guide fails for me
“fatal: couldn’t not switch to..”