Installing Git server on Ubuntu 12.04

Posted on Wednesday, August 15, 2012



Installing Git server on Ubuntu 12.04


I am familiar with using subversion, but my new job calls for using Git so I this is my attempt to set us a Git server on Ubuntu and use it.  First the main site for Git is http://git-scm.com/ [1] 

If you are new to Git, like I am, the Git folks have a wonderful free online book at http://git-scm.com/book [2].   And if you want to get right to it the section on setting up a server is at http://git-scm.com/book/en/Git-on-the-Server-Setting-Up-the-Server
(I took a of information from this guide)

Starting from a fresh install of Ubuntu 10.04 server with openssh
installed.

First create a git user




  >  sudo useradd -d /home/git -s /bin/bash -m git
  >  sudo passwd git
  >  su git







Setup SSH for git user



·         Create SSH Key
o   Open up terminal and execute           



  >  ssh-keygen –t rsa –b 2048


·         Update Authorized keys
o   From every machine you want to have access to this box run (current IP is 192.168.0.150)


Add ssh keys for users



  >  su git
  >  cd
  >  mkdir .ssh



Currently in my system the authorized_keys file in /home/patman/.ssh/ is exactly what I want for git so I will just copy it.



  >  su patman
  >  sudo cp ~/.ssh/authorized_keys /home/git/.ssh/



However if you should have keys you want to add just place them in /tmp folder and append them to this file like so.




  >  su git
  >  cat /tmp/id_rsa.pub >> ~/.ssh/authorized_keys







install git




  >  sudo apt-get install git-core




Now if you run this command



  >  which git



You should get






create a bare bones git repository




  >  sudo mkdir -p /opt/git
  >  sudo chown git:git /opt/git
  >  su git
  >  cd /opt/git
  >  mkdir project.git
  >  cd project.git
  >  git --bare init
















Here you can see the extra directories made by git for the repository





Create a repository and add this as a remote server


On another machine run the following




  >  git init
  >  git add .
  >  git commit –m ‘initial commit’
  >  git remote add origin git@192.168.0.60:/opt/git/project.git
  >  git push origin master




After all that I got this error

error: src refspec master does not match any.
error: failed to push some refs to 'git@192.168.0.60:/opt/git/project.git'



Looks like it did not like the fact that there was nothing in the directory?   I am using Cygwin on a Windows machine to push this up, it has version 1.7.9 of git..



At any rate here is what I did as a temp fix.




  >  touch bob.txt
  >  git add bob.txt
  >  git commit –m “added bob”
  >  git push origin master










That seemed to have fixed it.



Now to test it out clone the git respository



  >  cd
  >  mkdir git_test
  >  cd git_test
  >  git clone git@192.168.0.60:/opt/git/project.git
  >  ls project


And you should see the bob.txt


Now from here edit the file and check it into the local repository then sing that with the master



  >  cd project
  >  vi bob.txt
  >  git add bob.txt
  >  git commit –m “Edited the bob.txt file”
  >  git status


Now you will see that it has been added and commited to the local repository.

Now to push it up to the master git server.



  >  git push origin master



Now head back to the first directory where bob.txt was originally created and pushed up.   Now we want to pull down any changes


  >  git pull git@192.168.0.60:/opt/git/project.git










It pulls down the updates and updates bob.txt



Set up git-shell


With the current setup anyone who has access to this git server via the git user can just ssh into the box as the git user.   It would be a good idea to prevent this.

To prevent this you need to give the git user the shell git-shell to do this edit /etc/passwd



  >  sudo vi /etc/passwd


Change shell gor git user to /usr/bin/git-shell







Now when I ssh as git
I get the following error

fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.
Connection to 192.168.0.60 closed.



Shelling in as git has been disabled but git tools still work to push commits up to the master server.

I think this is not set up exactly right, the git-shell, but it works for my purposes.


References
[1]  Git main site
       http://git-scm.com/
       Visited 2/2012
[2]  Git book
       Visited 2/2012
[3]  4.4 Git on the Server - Setting Up the Server
       Visited 2/2012

No comments:

Post a Comment