Skip to content

Instantly share code, notes, and snippets.

@transforminteractive
Created April 9, 2017 08:48
Show Gist options
  • Select an option

  • Save transforminteractive/50dd95a929e1fafe1374f50b52a6c7be to your computer and use it in GitHub Desktop.

Select an option

Save transforminteractive/50dd95a929e1fafe1374f50b52a6c7be to your computer and use it in GitHub Desktop.
git server for WD EX2

Step 1: Create a folder for all your repositories

Ssh to your My Cloud instance (in my case called cloudy). The password is something you need to set in the web console.

ssh sshd@cloudy cd /usr/share mkdir git

Step 2: Create a git user

Note that you will be asked to enter a password. Make sure to hold on to that password. (Although we will replace it with key based authentication later on.)

useradd -m -d /usr/share/git git

Step 3: Allow ssh access for the git user

While still logged in as the admin (sshd) user, find the /etc/ssh/sshd_config file, and add add the git user to the AllowUsers key:

AllowUsers root sshd git

After that, you need to make sure the SSH daemon rereads its configuration data again. This is how I do it:

pkill -HUP sshd

Step 4: Ssh folder permissions

Go in to the /usr/share/git repository and fix the permissions:

cd /usr/share/git chown go-w . mkdir ./.ssh touch ./.ssh/authorized_keys chown -R git . chmod 600 ./.ssh/authorized_keys chmod 700 ./.ssh

Step 5: Add your public ssh key to the authorized_keys file

Just copy your public key, which is probably in ~/.ssh/id_rsa.pub on your local computer, and add it to the authorized_keys file on your My Cloud instance.

With these changes, you should now be able to logout from your current SSH session, and login using your new git user, without having to pass a password:

ssh git@cloudy

Step 6: Create a bare repository

Suppose that I have a local git repository (created with git init) and I want to make sure it can be pushed to a server as well, then you need to make sure that repository exists on the server. Assuming that you have an SSH session using the git user, and that you're in the /usr/share/git directory:

git init --bare test.git

Step 7: Add the repository you just created as a new remote to your local repository

In your local git repository, type the following command:

git remote add origin git@cloudy:/usr/share/git/test.git

If all went well, then you should now be able to to push your local changes to the remote repository:

git push origin master

Troubleshooting SSH

If you run into trouble while following the previous steps, there's a fair chance it has to do with file permissions. SSH is pretty picky on your permissions, and it's not always easy to figure out what's going on.

Unfortunately, your My Cloud instance doesn't have logging enabled for its sshd service. (Well, it is enabled, but there's no syslog configuration telling it where to go.)

In order to fix that, ssh into your My Cloud as the admin user (sshd), and add the following line to your /etc/syslog.conf file:

auth,authpriv.* /var/log/auth.log

Then restart the syslogd service:

pkill -HUP syslogd

To get more information on what's going on in your sshd service, add the following line to your /etc/ssh/sshd_config file:

LogLevel VERBOSE

… and restart the sshd service, just like you were doing it before:

pkill -HUP sshd

Now, you should be able to get some meaningful information from /var/log/auth.log.

@hyacin75
Copy link
Copy Markdown

hyacin75 commented Sep 26, 2018

Thank you so much for this!

For anyone else trying to do this on an EX2 Ultra with a hex edited package file (details here - https://community.wd.com/t/how-to-install-git-server-on-my-wd-my-cloud-mirror-gen-2/214994/4 ) the useradd command above (which is not present on an EX2 Ultra for some reason) becomes - adduser -h /usr/share/git git (see below)

And a typo in step 4 - "chown go-w ." is actually "chmod go-w ."

Also if you suck at git like I do, you might sit at step 7 spamming that command a few times wondering why it isn't working ... you need to initialize the repository on the box you're pushing from first ... instructions here - https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/

Aaaaannnnndddddd /usr/share/git gets wiped on reboot on an EX2 Ultra ... the user also got deleted, so I'll have to go right back to the drawing board on this one ...

Ok, what we actually need to do with an EX2 Ultra -

Step 1: Create a new share via the web interface to host your repositories
Step 2: Create a git user via the web interface
Step 3: Same as above
Step 4: Same as above, but instead of /usr/share/git, it will be /shares/SHARE_NAME - so if you called your Step 1 share "git", it's /shares/git instead of /usr/share/git
Step 5: Same as above
Step 6: Same as above
Step 7: Same as above, but your remote origin ends with :/shares/SHARENAME/test.git
Step 8: When the box reboots, git's home directory will change back to the default, and SSH access will be lost, to redo these changes after a reboot, add the following to the very bottom of /mnt/HD/HD_a2/Nas_Prog/git/start.sh (editing as needed if you didn't call your user git, or your share git) -

#change git user home dir, and allow ssh login
sed -ir 's/git:x:([0-9]*):.*/git:x:\1:1000:,,,:\/shares\/git:\/bin\/sh/' /etc/passwd
sed -ir 's/(AllowUsers .*)/\1 git/' /etc/ssh/sshd_config
kill -HUP `cat /var/run/sshd.pid`

After all this, the user, SSH access, and repositories survive reboots.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment