Description
The purpose of this document is to help you setup as many Git projects on a remote server that multiple developers can access, checkout, push to and pull from. This keeps all of your code off of public servers and allows you to control and restrict interaction in a fine-grained and simple setup.
This method can also be adapted to incorporate plugins or themes on a live development site so that code changes can be pushed and viewed in action immediately. Stay tuned for a supplemental document that outlines how to accomplish this.
Also, see this great tool (gitk) for viewing all the details of your local repositories, just run it from the command line within any local git repository, most major Linux distributions have it.
Step 1 – Remote Server
Everything in this section is done on the remote server. We’re using ‘johndoe’ and ‘janedoe’ as our developers and ‘gitrepo’ as our user for the Git repository.
- Create a new Git user & initial ssh directory setup.
- Add the Git user to the system and set a password if you want, it’s not necessary for keyed entry though.
- Create the .ssh directory and authorized_keys keyring file and set proper permissions on each.
- Setup sshd authentication Into the Git repository for multiple users
- Create the group ‘sshusers’ and add all of your users to that group:
- Copy their ssh public keys into user ‘gitrepo’ authorized_keys keyring:
- Edit the sshd configuration.
- Open port 33936 in the firewall.
- Setup 1st Empty Repository
- Check see if
git-shell
is already in there. - If not.. make sure git-shell is installed on your system.
- Add the path to git-shell from last command.
- Change the Git users shell by entering the path to git-shell, usually:
/usr/bin/git-shell
- Setup ‘project1’ and push to the git master.
- Verify Your Local Git Configuration For ‘project1’
- At this point, the others can clone it down and push changes back up just as easily:
adduser gitrepo
passwd gitrepo
su - gitrepo
mkdir .ssh && chmod 700 .ssh
cd .ssh
touch authorized_keys && chmod 600 authorized_keys
groupadd sshusers
usermod -a -G janedoe sshusers
usermod -a -G johndoe sshusers
usermod -a -G gitrepo sshusers
cat /tmp/id_rsa.johndoe.pub >> ~/.ssh/authorized_keys
cat /tmp/id_rsa.janedoe.pub >> ~/.ssh/authorized_keys
We’re going to set this sshd server up more securely than most but we won’t go overboard here, if you want to read more see the Securing sshd document here. Change the values in the sshd_config like the example below.
nano /etc/ssh/sshd_config
1 2 3 4 5 6 7 8 9 10 11 12 | Port 33936 (this can be any port you'd like but make it a high port as many lower ports are reserved or standard application ports) ListenAddress 192.168.126.1 (this is the IP address of your server) LoginGraceTime 1m PermitRootLogin no RSAAuthentication yes PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys AllowGroups sshusers AllowUsers johndoe janedoe gitrepo |
iptables -A INPUT -p tcp -m tcp --dport 33936 -j ACCEPT
cd ~
mkdir -p repos/project
cd repos/project1
git init --bare
Extra Security
If you don’t want the developers logging in with an ssh shell and pulling files from projects outside of Git or potentially damaging or deleting things, you can easily restrict the Git user to only doing Git activities with a limited shell tool called git-shell that comes with Git. If you set this as your git user’s login shell, then the git user can’t have normal shell access to your server. To use this, specify git-shell instead of bash for your user’s login shell. To do so, you must first add git-shell to /etc/shells if it’s not already there:
cat /etc/shells
which git-shell
sudo nano /etc/shells
sudo chsh git
Now, the git user can only use the SSH connection to push and pull Git repositories and can’t shell onto the machine. If you try, you’ll see a login rejection like this:
1 2 3 4 | $ ssh gitrepo@192.168.126.1 fatal: Interactive git shell is not enabled. hint: ~/git-shell-commands should exist and have read and execute access. Connection to 192.168.126.1 closed. |
Step 2 – John Doe’s Local Computer
cd ~
mkdir -p git/repos/project1
git init
git add *
git commit -m "Initial Commit"
git remote add master ssh://gitrepo@192.168.126.1:33936/home/gitdeploy/repos/project1/
git remote set-url master ssh://gitrepo@192.168.126.1:33936/home/gitdeploy/repos/project1/
git push -u origin master
1 2 3 4 5 6 7 8 9 | gitrepo@192.168.126.1's password: Counting objects: 25, done. Delta compression using up to 8 threads. Compressing objects: 100% (24/24), done. Writing objects: 100% (25/25), 35.45 KiB | 0 bytes/s, done. Total 25 (delta 0), reused 0 (delta 0) To ssh://gitrepo@192.168.126.1:3936/home/gitdeploy/repos/protect/ * [new branch] master -> master Branch master set up to track remote branch master from origin. |
cat .git/config
1 2 3 4 5 6 7 8 9 10 11 | [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true [remote "origin"] url = ssh://gitrepo@192.168.126.1:33936/home/gitdeploy/repos/project1/ fetch = +refs/heads/*:refs/remotes/origin/* [branch "master"] remote = origin merge = refs/heads/master |
Step 3 – Clone The New Repo for Jane Doe’s Local System or Any Other User
git clone ssh://gitrepo@192.168.126.1:33936/home/gitrepo/repos/protect1/
cd project
nano README
git commit -am 'fix for the README file'
git push origin master
And that brings us full circle, to add, remove or temporarily lockout users from this setup see the Quick Reference below.
Quick Reference
Add another user
adduser jimdoe
usermod -a -G jimdoe sshusers
- Copy their ssh public keys into user ‘gitrepo‘ authorized_keys keyring:
cat /tmp/id_rsa.jimdoe.pub >> ~/.ssh/authorized_keys
Remove a user
userdel -f jimdoe
Important: see steps below also for total removal.
Temporarily Lock out a user without changing any passwords
There are at least two methods that will work here; remove the ssh pubkey of the user, or remove the user from the sshd_config ‘AllowUsers’ directive.
Remove users ssh key. (if you wish to restore this later, make a backup of authorized_hosts or have an archive copy of the users .pub keyfile handy)
nano /home/gitdeploy/.ssh/authorized_hosts
- Find the line with the users RSA key, their username@machine name will be at the end of the string.
CNTL + K
will cut the line out.CNTL + X
will exit the nano editor, say Y to write the new file.
Remove user ‘johndoe‘ from the sshd_config and the ‘sshusers‘ group.
nano /etc/ssh/sshd_config
- Locate the ‘AllowUsers’ and find the name of the user you want to revoke and erase it with the backspace key.
CNTL + X
will exit the nano editor, say Y to write the new file.- gpasswd -d johndoe sshusers
- Restart sshd:
C6: service sshd restart
C7: systemctl restart sshd.service
Additional Reading
http://rogerdudler.github.io/git-guide/
https://www.kernel.org/pub/software/scm/git/docs/git-pull.html#URLS