Setup Remote GIT Repository Server on CentOS 7

By | October 13, 2018

Bismillahi-r-Rahmani-r-Rahim (In the name of Allah, the most Compassionate, the most Merciful)

Assalamu alaikum wa rahmatullahi wa barakatuh (May the peace, mercy, and blessings of Allah be with you)


This is the ad hoc tutorial on how to setup remote GIT repository server on CentOS 7.

Below content is complementary to the video tutorial above.

Server Side Configuration

1. Initial Configuration

Update the system and install some standard utilities

yum update -y && yum upgrade -y
yum -y install nano

Install Git and enable Git shell

yum install -y git
echo "/bin/git-shell" >> /etc/shells

Create new group for Git users and specify passwordless authentication for this group:

groupadd git_users

Add the following lines to /etc/ssh/sshd_config

Match Group git_users
PasswordAuthentication no

Restart SSH service

service sshd restart

Create new folder to hold the future git remote repositories:

mkdir /repositories

2. User Management

Create a Git user and authorized_keys file to hold SSH public keys:

useradd -g git_users git_user1
mkdir /home/git_user1/.ssh
chmod 700 /home/git_user1/.ssh
echo ""> /home/git_user1/.ssh/authorized_keys
chmod 600 /home/git_user1/.ssh/authorized_keys
chown -R git_user1:git_users /home/git_user1/.ssh
usermod --shell /bin/git-shell git_user1

3. Remote Repository Creation

mkdir /repositories/git_repo1.git
cd /repositories/git_repo1.git
git init --bare --shared=group
sudo chgrp -R git_users .

4. SSH Authorization and Access Management

Provide access for git_user1 to repository git_repo1:

unlink /home/git_user1/git_repo1.git
sudo ln -s /repositories/git_repo1.git /home/git_user1/
chown -R git_user1:git_users /home/git_user1

Client Side Configuration

1. Download and install Git from https://git-scm.com/downloads. We will need Git bash tool to generate public/private key pair and to establish ssh connection to the server.

2. Open Git Bash and generate public/private key pair using the following command:

ssh-keygen -t rsa -C "[email protected]"

3. Copy the contents of the public key file (by default: path_to_key/id_rsa.pub)

Server Side Configuration (continued)

5. Use the following commands to authorize your public key for the previously created user:

usersPubKey='contents_of_your_public_key_file'
echo $usersPubKey > /home/git_user1/.ssh/authorized_keys

Testing

1. Create new folder on the client side, initialize git here and push it to git repository server (replace username and git server address and repo name with your own):

mkdir test1 && cd test1
git init .
git config --global user.email "[email protected]"
git config --global user.name "Your Name"
echo test_content1 > test1.txt
echo test_content2 > test2.txt
git add .
git commit -m "initial commit"
git remote add origin git_user1@192.168.70.136:git_repo1.git
git push origin master

2. Create another folder and clone the remote repository to this folder:

mkdir test2
cd test2
git clone git_user1@192.168.70.136:git_repo1.git .
ls

Related resources: