Setting Up a Git Server with Hooks and SSH Access
In this tutorial, we will guide you through setting up a Git server with Git hooks, specifically focusing on the post-receive
hook, and SSH access. We will cover permissions, common mistakes to avoid, and best practices.
Prerequisites
Before we begin, make sure you have:
- A Linux server (e.g., Ubuntu) with SSH access.
- Git installed on the server.
- A user account with sudo privileges.
Step 1: Create a Git User
It's a good practice to create a dedicated user for Git on your server. Replace <username>
with your desired username.
sudo adduser <username>
Step 2: Set Up SSH Key Authentication
To allow SSH access to the Git server, you should set up SSH key authentication for your users. Each user who wants to access the Git server needs to generate an SSH key pair.
- On the user's local machine, generate an SSH key pair if not already done:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
- Copy the public key (
~/.ssh/id_rsa.pub
) to the Git server:
ssh-copy-id <username>@<server-ip>
- Test SSH access to the server:
ssh <username>@<server-ip>
Step 3: Create a Git Repository
Let's create a sample Git repository on the server. Replace <repo-name>
with your desired repository name.
sudo mkdir /srv/git/<repo-name>.git
sudo chown -R <username>:<username> /srv/git/<repo-name>.git
sudo chmod -R 770 /srv/git/<repo-name>.git
cd /srv/git/<repo-name>.git
git init --bare
Step 4: Configure Git Hooks
Git hooks are scripts that execute automatically in response to specific events. We'll use the post-receive
hook to update the working directory when changes are pushed to the repository.
- Create the
post-receive
hook script:
cd /srv/git/<repo-name>.git/hooks
sudo nano post-receive
- Add the following content to the
post-receive
script. Replace<working-directory>
with the path to your working directory.
#!/bin/bash
while read oldrev newrev refname; do
branch=$(git rev-parse --symbolic --abbrev-ref $refname)
if [ "master" == "$branch" ]; then
git --work-tree=<working-directory> --git-dir=/srv/git/<repo-name>.git checkout -f
fi
done
- Make the script executable:
sudo chmod +x post-receive
Step 5: Configure Git Remote
On your local machine, configure the Git remote to point to the Git server:
cd <your-local-repo>
git remote add origin ssh://<username>@<server-ip>/srv/git/<repo-name>.git
Step 6: Push to the Git Server
You can now push your local repository to the Git server:
git push origin master
Permissions and Common Mistakes
-
File Permissions: Ensure that the Git repository directory (
/srv/git/<repo-name>.git
) and thepost-receive
hook script have appropriate permissions (chmod
andchown
). Usesudo
as needed to modify permissions. -
SSH Key Setup: Make sure SSH key authentication is set up correctly for each user who needs access. Test SSH access before proceeding.
-
Repository Paths: Verify that repository paths and URLs are correctly configured in both the Git remote and the
post-receive
hook script. -
Working Directory: Ensure that the
post-receive
hook script correctly updates the working directory. Replace<working-directory>
with the actual path to your working directory. -
Branch Handling: The example script above only updates the working directory for the
master
branch. Modify the script if you want to handle other branches differently.
Conclusion
You have successfully set up a Git server with Git hooks and SSH access. This setup allows you to automate actions on your Git server when code is pushed, making it a valuable tool for team collaboration and continuous integration. Always ensure that your Git server is secure, and follow best practices to manage your repositories effectively.