Friday, January 4, 2013

How to setup a git server on a Synology NAS

Context

  • Software development (Linux, MacOS, Java / Eclipse)
  • Home network (security is not a concern in this context)
  • Synology NAS (DS 112j, DSM 4.1) 
Important update (august 31th 2013):  since I wrote this post, Synology has incorporated a Git server with the DSM 4.3. So what follows is deprecated...

Nevertheless, you may find that it is far more simpler and powerful to setup a git server on a Raspberry PI using Gitolite.

This memo describes how to setup a Git over SSH server on a Synology NAS. Use it at your own risk ! Your context certainly differs from mine ...



Enable SSH on the Synology
First of all, SSH must be activated on the NAS. This can be done using the  Control Panel of the DSM and the Terminal app. SSH will be used to connect to the Synology, and as the communication protocol for Git.

Bootstrap ipkg

As git is not installed by default on Synology devices (like many useful Unix commands), one has to extend the DSM using ipkg, the Itsy package manager, dedicated to Debian based embedded devices.

More information (and disclaimers) about modifying Synology devices can be found here.

Identify the bootstrap file

The first thing to do is to identify what is the corresponding bootstrap file for your Synology device (popular bootstrap URLs are here).

Log into the NAS

Then  log into the Synology device (diskstation2 in this example) using ssh:

% ssh root@diskstation2
root@diskstation2's password: 

BusyBox v1.16.1 (2012-09-26 03:28:29 CST) built-in shell (ash)
Enter 'help' for a list of built-in commands.

DiskStation2#

(I changed the original prompt which ends with a redirection sign ">". I hate redirection signs in prompts...)

Get the bootstrap file

Get the bootstrap file (for a DS112j target in this example) in a temporary directory and install it:

DiskStation2# cd /volume1/@tmp
DiskStation2# wget http://ipkg.nslu2-linux.org/feeds/optware/cs08q1armel/cross/unstable/syno-mvkw-bootstrap_1.2-7_arm.xsh



Run the bootstrap

DiskStation2# sh syno-mvkw-bootstrap_1.2-7_arm.xsh

then do some cleaning:

DiskStation2# rm syno-mvkw-bootstrap_1.2-7_arm.xsh


Modify root's user PATH

Modify root path in .profile: include /opt/bin and /opt/sbin at the beginning of the PATH variable (/opt is where ipkg installs things, /opt/sbin being useful if you want to install commands like lsof).


Reboot the NAS 

(don't forget to warn your family users ...)

DiskStation2# reboot now

(or use the web interface, in any case, you must hear the born again beep ...)

Install packages

Log again and install required packages:

DiskStation2# ipkg install coreutils


DiskStation2# ipkg install git





Make symbolics links of git commands into /usr/bin 



This is a simple way to make these commands available via sshd.

DiskStation2# for f in `ls /opt/bin/git*`
> do
> ln -s $f /usr/bin     
> done

Alternative: use ~git/.ssh/environment and modify /etc/ssh/sshd_config to set PermitUserEnvironment to yes as explained here.

Create a git user on the NAS

Use the web interface to create a git user (belonging to the group users) with no privileges.

Several extra steps are required :

As root user
  • Change some fields of the git user in /etc/passwd (using vi)
    • Replace the home directory
      • /var/services/homes/git to /volume1/git
    • Replace the login shell
      • /sbin/nologin to /bin/ash
As git user
  • Become the git user : DiskStation2# su - git
  • Just to be sure, check that the git user homedir belongs to the git user (this should be the case)
  • Create a .profile file 
    • Copy and adapt the root's .profile file
    • Don't forget to change the HOME variable accordingly (I did not understood why the HOME var is not setup automatically ...)
  • chmod to 700 the git user homedir
  • create a  .ssh folder within the git user homedir, again with 700 perms
Note: I realized sometime later that relocating the homedir of the git user on /volume1 had the side effect of creating automatically (at the next reboot) a git share. Using the web interface, I then disabled this share for the members of the users group, and hide it from the Network places.

Authorize git users

As the git user just created on the Synology will be accessed for git purposes using the SSH protocol by your development account, you must add its public key in the ~git/.ssh/authorized_keys file (whose permissions must be 600).

Doing this will also let you access to the git account of the Synology from your development account securely and without supplying a password.

I used vi and copy / paste between xterms  to copy my public key in the authorized_keys file.

If your are not familiar with SSH and public/private keys, you should read this article (how to create a private/public key pair, how to include the public key in an remote authorized_keys file).

If the following command fails (replace diskstation2 by the IP name of your NAS) or prompts you for a password, it means that your SSH setup is not correct:

myself@myhost:~/% ssh git@diskstation2 ls /etc/shells
/etc/shells

Test the git server

On the Synology

  • Create a repositories folder in the git homedir
  • cd to it, then create a git repo:
git@DiskStation2% echo $PWD
/volume1/git/repositories

git@DiskStation2% mkdir aiuto.git
git@DiskStation2% cd aiuto.git/
git@DiskStation2% git init --bare
Initialized empty Git repository in /volume1/git/repositories/aiuto.git/
git@DiskStation2% ls
HEAD  branches  config  description  hooks  info  objects  refs

On a client workstation

myself@myhost:~/dev/projects% git clone ssh://git@diskstation2/volume1/git/repositories/aiuto.git
Cloning into 'aiuto'...
warning: You appear to have cloned an empty repository.
myself@myhost:~/dev/projects% ls -a aiuto/
.  ..  .git
myself@myhost:~/dev/projects% cd aiuto/
myself@myhost:~/dev/projects/aiuto% echo "V0.1 - 2 janvier 2013" > Changes.txt
myself@myhost:~/dev/projects/aiuto% git add Changes.txt
myself@myhost:~/dev/projects/aiuto% git commit -m "Changes.txt file created - release notes"
[master (root-commit) 82fcd36] Changes.txt file created - release notes
 1 file changed, 1 insertion(+)
 create mode 100644 Changes.txt
myself@myhost:~/dev/projects/aiuto% git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 267 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://git@diskstation2/volume1/git/repositories/aiuto.git
 * [new branch]      master -> master


References

Links bellow helped me to work on this topic.

http://www.wonko.de/2010/04/set-up-git-on-synology-nas.html
http://www.bluevariant.com/2012/05/comprehensive-guide-git-gitolite-synology-diskstation/
http://stackoverflow.com/questions/10888300/gitosis-vs-gitolite (reading one of the answers, I decided to not use gitolite to begin with git; being the only software developer in casa ...).


Note: An alternative to install git on a Synology device
A Raspberry PI 512MB in a box.
Git friendly !
 is to install it on a Raspberry PI. For those of you who don't know, a Raspberry is an affordable credit card sized computer, running under Linux (particularly Raspbian which is a Debian derivative), equipped with 512MB of memory, and which uses using a SD Card as a storage device. 




4 comments:

  1. Thanks, I just spent my morning following your steps to install Git on my Synology NAS. It all seems to work.

    Suggestions:

    - Under "Modify root's user PATH", the /opt/bin and /opt/sbin should be at the start of the PATH var, otherwise it seems to find an incompatible version of something in bin, possibly 'wget'. Things won't work.

    - Under "Authorize git users", be more explicit about what you are doing. It took me a while to figure out I needed to create the keys in the default location, then add them over to ~git/.ssh/authorized_keys.

    - Be sure to note that the 'git' user needs to be the owner of it's home directory. I had 'root' as owner, my mistake, and it caused permission denied errors.

    ReplyDelete
    Replies
    1. Thank you for your feedback !

      I have modified some parts of the post in order to take your suggestions into account.

      Delete
  2. GIT is now supported from the DSM 4.3 as a standard package.

    ReplyDelete
    Replies
    1. Thanks Thibault, this is exactly what I say at the very beginning of this post with a verrrrrry big font and stabylo boss (c) background :-)

      Delete

Please, enter your comments bellow