excerpt: "I'll show you how to setup your own git server easily"
featured_image: "uploads/2018/07/gitout.jpg"
author: "danix"
excerpt: "I'll show you how to setup your own git server easily"
featured_image: "uploads/2018/07/gitout.jpg"
-tags: ["git", "server", "setup", "howto", "do it yourself", "ssh"]
-categories: ["code", "diy", "linux"]
+tags:
+ - git
+ - server
+ - setup
+ - howto
+ - do it yourself
+ - ssh
+categories:
+ - code
+ - diy
+ - linux
---
Hello everyone,
---
Hello everyone,
@@ -32,16+41,16 @@ Installing a git server is quite simple once you know how it works, on my server
I've added a new user and group to my server but before doing so I added /usr/bin/git-shell to /etc/shells in order to use it as login shell for my git user.
I've added a new user and group to my server but before doing so I added /usr/bin/git-shell to /etc/shells in order to use it as login shell for my git user.
now the user is all set and ready to be used. Next step will be to create the .ssh directory and the authorized_keys file to hold the keys for the developers that have to access the git server. Here's how I did it:
now the user is all set and ready to be used. Next step will be to create the .ssh directory and the authorized_keys file to hold the keys for the developers that have to access the git server. Here's how I did it:
-```
+```bash
mkdir /var/git/.ssh
touch /var/git/.ssh/authorized_keys
chown -R git:git /var/git
mkdir /var/git/.ssh
touch /var/git/.ssh/authorized_keys
chown -R git:git /var/git
@@ -55,7+64,7 @@ Let's head back to my working computer, I created an ssh keypair for my usual us
Since I have ssh access to the same server for my normal user I used the ~/.ssh/config file on my computer to set a new host that will ease my access routine for the git user as well as my regular user, that's my config (more or less):
Since I have ssh access to the same server for my normal user I used the ~/.ssh/config file on my computer to set a new host that will ease my access routine for the git user as well as my regular user, that's my config (more or less):
-```
+```bash
cat ~/.ssh/config
Host regular_ssh
HostName server.tld
cat ~/.ssh/config
Host regular_ssh
HostName server.tld
@@ -80,7+89,7 @@ and ssh will take care of all the options and start the connection with the corr
Now that the access for the git user is setup we have one last thing to do before being able to use it. We'll give him only limited commands to use, That way the git user will be even more limited and much more secure. Inside the documentation shipped with git there's a lot of scripts to get you started with this, so we'll copy them inside a special directory called git-shell-commands, like this:
Now that the access for the git user is setup we have one last thing to do before being able to use it. We'll give him only limited commands to use, That way the git user will be even more limited and much more secure. Inside the documentation shipped with git there's a lot of scripts to get you started with this, so we'll copy them inside a special directory called git-shell-commands, like this:
Now we have 2 commands inside the git-shell-commands directory, list and help, the first will show all projects inside the /var/git directory and the other will show a simple help text and a list of all the commands available. Now to give you an example of how easy it is to add commands to the git-shell I will create a simple command that acts as the clear command, it will clean the screen, to do so, from inside the /var/git directory I did:
Now we have 2 commands inside the git-shell-commands directory, list and help, the first will show all projects inside the /var/git directory and the other will show a simple help text and a list of all the commands available. Now to give you an example of how easy it is to add commands to the git-shell I will create a simple command that acts as the clear command, it will clean the screen, to do so, from inside the /var/git directory I did:
-```
+```bash
echo $(which clear) > git-shell-commands/clear
chmod 0755 git-shell-commands/clear
echo $(which clear) > git-shell-commands/clear
chmod 0755 git-shell-commands/clear
@@ -102,7+111,8 @@ Let's focus on the two most important scripts for our git server, the **create**
This is the create script, as you can see it's a bit complex because it will do a few things for me:
This is the create script, as you can see it's a bit complex because it will do a few things for me:
@@ -246,15+257,18 @@ This is the create script, as you can see it's a bit complex because it will do
So after saving this script as create inside the git-shell-commands directory, we'll give it executable permissions and we can move to the delete script.
So after saving this script as create inside the git-shell-commands directory, we'll give it executable permissions and we can move to the delete script.
# usage: delete <REPOSITORY> - PERMANENTLY delete a repository if existing.
# CAREFUL, this action cannot be undone. This command will ask for confirmation.
# usage: delete <REPOSITORY> - PERMANENTLY delete a repository if existing.
# CAREFUL, this action cannot be undone. This command will ask for confirmation.
@@ -304,7+318,8 @@ if [ -d ${GITDIR}/${PROJECT}.git ]; then
echo "\"${PROJECT}.git\" is an empty directory, Skipping. Check with your System Administrator."
exit 177
fi
echo "\"${PROJECT}.git\" is an empty directory, Skipping. Check with your System Administrator."
exit 177
fi
-fi</code></pre>
+fi
+{{< /highlight >}}
This script is much simpler than the previous one, it'll accept the name of the project as argument on the command line or will ask for it and will only delete it if it is a proper git repository, otherwise it will just exit with an error code.
This script is much simpler than the previous one, it'll accept the name of the project as argument on the command line or will ask for it and will only delete it if it is a proper git repository, otherwise it will just exit with an error code.
@@ -312,7+327,8 @@ I improved the way those scripts recognise a git repository from simply relying
Since we are here let's modify the help command to make it show a short description of every available command.
Since we are here let's modify the help command to make it show a short description of every available command.
The main thing I added is the support for a command line argument, now I'm able to run it by itself and display the usual output with a list of available commands, or followed by a command name to give a brief explanation like this:
The main thing I added is the support for a command line argument, now I'm able to run it by itself and display the usual output with a list of available commands, or followed by a command name to give a brief explanation like this:
-<pre class="wp-block-code language-bash"><code>git> help
+{{< highlight bash >}}
+git> help
Hi git, Run 'help' for help, 'help <command>' for specific help on a command, run 'exit' to exit. Available commands:
clear
create
Hi git, Run 'help' for help, 'help <command>' for specific help on a command, run 'exit' to exit. Available commands:
usage: create - create a git bare repository named PROJECT.git
this command will setup the repo and send a mail for confirmation.
git>
usage: create - create a git bare repository named PROJECT.git
this command will setup the repo and send a mail for confirmation.
git>
-</code></pre>
+{{< /highlight >}}
Pretty nice isn't it?! Now it's much more user friendly, and to show the description I used awk and cut to parse the comment at the top of every script I have in the `git-shell-commands` directory.
Pretty nice isn't it?! Now it's much more user friendly, and to show the description I used awk and cut to parse the comment at the top of every script I have in the `git-shell-commands` directory.
@@ -368,18+386,18 @@ In the next page we'll see the usual routine I follow when working with this new
Let's say I had a new idea for a WordPress plugin, I can't wait to start writing, so the setup of the GIT environment should be as fast as possible. That's where my setup will come in handy. Let's open the terminal, I'll go inside my testing directory and from there I'll run:
Let's say I had a new idea for a WordPress plugin, I can't wait to start writing, so the setup of the GIT environment should be as fast as possible. That's where my setup will come in handy. Let's open the terminal, I'll go inside my testing directory and from there I'll run: