Thursday, February 16, 2017

Setup a private Git server on Linux (Mint / Ubuntu / Debian)

Git is a distributed version control system (VCS) created by Linus Torvalds for development of the Linux kernel, with other kernel developers contributing to its initial development.  Git development began in April 2005, after many developers of the Linux kernel gave up access to BitKeeper because his creator had withdrawn free use of the product after claiming that BitKeeper protocols had been reverse-engineered.

The Eclipse Foundation reported that as of May 2014, Git was the most widely used source code management tool; with 42.9% of professional software developers reporting that they use Git as their primary source control system.
Companies like GitHub offer code hosting services based on Git, but free plans usually are limited to open source projects and you are not allowed to host private projects unless you pay for it. The alternative you have for a free service is to set up your own personal Git server.

The following guide will explain how to set up a private Git server for hosting your personal projects. Although you don’t need to set up a Git server if you write your programs in a single computer, since you can use Git in local mode, having a server will allow you to keep and manage all your projects in a common place. You can conveniently access to your code from every computer, even though Internet. Additionally you can invite other friends or colleges to pull your code and collaborate simultaneously in different projects.

Proposed scenario 

 

 

 

The steps involved in the setup are:

#1. Install and configure OpenSSH to allow access through SSH protocol (On server machine)
#2. Create a developers group and add each developer to it (On server machine)
#3. Create a directory for the git repository and configure its permissions (On server machine)
#4. Install Git and create a a new project (On server machine)
#5. Install Git and clone the project (On client machine)


 

# 1. Install and configure OpenSSH

On the server machine open a new console and install OpenSSH

admin@server-pc $ sudo apt install openssh-server


After installation we have to make some changes in the configuration file, so it’s recommended to make a backup of the file before introducing any of the changes.

admin@server-pc $ sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.original


Open the configuration file with a text editor like vi, gedit or xed.

admin@server-pc $ sudo vi /etc/ssh/sshd_config

Find and uncomment the following lines removing the character “#” located at the beginning of each line. This will enable SSH server listen on all adapters thus it can be reached from the network.

ListenAddress ::
ListenAddress 0.0.0.0


Finally, reboot the system or start OpenSSH service from the console.

admin@server-pc $ sudo systemctl restart sshd.service


In case you need a more detailed explanation about the installation process you can visit the Ubuntu documentation site.

# 2. Create a developers group and add each developer to it

In a new console or in the previous one, create the “developers” group. This group will be used for managing permissions all members will have over the repository directory created in step 3.

admin@server-pc $ sudo groupadd developers


Then add all Developer users to the group. For instance, for the user “john” the command would be:

admin@server-pc $ sudo usermod -a -G developers john


# 3. Create a git repository and configure its permissions

Create a new directory for the git repository. Possible target paths could be /srv/git-repo, /var/lib/git-repo or any other file system location of your choice. In my case I have a dedicated volume mounted on /mnt/data/git-repo so I can keep my important data away of the operative system volume.

admin@server-pc $ sudo mkdir /srv/git-repo


Set the group “developers” as owner of the repository directory.

admin@server-pc $ sudo chown -R root:developers /srv/git-repo


Set to the group permissions to read, write and execute.

admin@server-pc $ sudo chmod -R g+rwx /srv/git-repo/


# 4. Install Git and create a new project

Install Git on the server machine using the following command.

admin@server-pc $ sudo apt install git

Create an example project named “myproject”. This step must be repeated every time you want to create a new project on the server, as convention remember to end your project name directory in .git.

admin@server-pc $ git init --bare /srv/git-repo/myproject.git


# 5. Install Git and clone the project

Install Git on the client machine.

user@client-pc $ sudo apt install git

Clone the project using “git clone”.

user@client-pc $ git clone ssh://john@192.168.0.1/srv/git-repo/myproject.git myproject

The above command clones the project “myproject” located on the server path /srv/git-repo/myproject.git. We assume the server is listening on the IP address “192.168.0.1” and the user for the SSH connection is “john”, same user that we previously added on the server to the group “developers”.
After executing the command a directory myproject will be created on the client machine. Every time you want to clone a project you have to execute this command, off course changing all variables according your configurations.
The final step to check everything is working as expected is to create an empty file readme.txt and push that file into the server repository. You can achieve this test by running the following commands:

user@client-pc $ touch readme.txt
user@client-pc $ git add .
user@client-pc $ git commit -m 'Initial commit'
user@client-pc $ git push

It’s important to mention that for commands requiring a connection to the server like clone, fetch, pull, push, etc. Git will prompt you to enter a password, as every SSH connection and operation must be authorized by the server. Notice that the entered password should be the one belonging to the user specified in the “git clone” command, and not the one belonging to the user logged in the client machine.
If your password is complex and/or you find annoying entering a password so often and for so many commands, you can rather generate “SSH keys” so the client machine can automatically authenticate to the server.

 

Conclusion

Having your own Git server will expand your options at managing your code in several aspects: You will get better understanding of advanced Git commands and take advantage of their use, you can share projects with your colleagues or friends, projects are centralized and reachable from every computer from the network, backups are more convenient (specially if you have more than one computer), and many other factors o needs you might have.

No comments:

Post a Comment