Operations 11 min read

Step-by-Step Guide to Building a Secure Git Server and GitLab on CentOS 7

This tutorial walks you through creating a dedicated git user, configuring a bare repository, setting up SSH key authentication, installing GitLab on CentOS 7, managing groups and permissions, and performing backup and restore operations, all with concrete command examples and screenshots.

Ops Development Stories
Ops Development Stories
Ops Development Stories
Step-by-Step Guide to Building a Secure Git Server and GitLab on CentOS 7

Creating the git user and group

First create a git group and a git user, then restrict the user to git-shell by editing /etc/passwd:

groupadd git
useradd -g git git
git:x:1001:1002::/home/git:/usr/bin/git-shell

This allows SSH access for Git operations while preventing interactive shell login.

Creating a bare repository

Switch to the git home directory, create a directory for the repository, and initialize it as a bare repo:

cd /home/git/
mkdir study.git
git init --bare study.git

Set ownership to the git user:

chown -R git:git study.git

Cloning the repository

Clone the repository via SSH. If the SSH daemon uses a non‑standard port, specify it in the URL:

git clone [email protected]:/home/git/study.git
git clone ssh://[email protected]:500/home/git/study.git

Setting up SSH key authentication

Edit /etc/ssh/sshd_config to enable public‑key authentication:

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

Create the .ssh directory for the git user and set proper permissions:

mkdir .ssh
chmod 700 .ssh
touch .ssh/authorized_keys
chmod 600 .ssh/authorized_keys

Generate a key pair on the client with ssh-keygen and copy the public key to /home/git/.ssh/authorized_keys.

Installing GitLab on CentOS 7

Install required packages, start and enable postfix, then download and install the GitLab CE package:

yum -y install policycoreutils openssh-server openssh-clients postfix
systemctl enable postfix && systemctl start postfix
wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-8.9.5-ce.0.el7.x86_64.rpm
rpm -i gitlab-ce-8.9.5-ce.0.el7.x86_64.rpm

Configure the external URL in /etc/gitlab/gitlab.rb (replace with your server IP): external_url 'http://192.168.188.222' Run the reconfiguration script to install all components, then restart GitLab:

gitlab-ctl reconfigure
gitlab-ctl restart

Using GitLab

Access GitLab via the configured IP address, set an admin password, then create groups, projects, and users through the web UI. Permissions can be set at the group level (Private, Internal, Public) and per‑user role (Guest, Reporter, Developer, Master, Owner).

Backup and restore

Edit /etc/gitlab/gitlab.rb to enable backups (default directory /var/opt/gitlab/backups) and set the retention period (default 7 days). Then run: /usr/bin/gitlab-rake gitlab:backup:create To restore, stop services, run the restore command with the backup timestamp, and restart services:

gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
gitlab-rake gitlab:backup:restore BACKUP=1533281464
gitlab-ctl start unicorn
gitlab-ctl start sidekiq

After restoration the projects are available again.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

GitLabGitBackupCentOSServer Setup
Ops Development Stories
Written by

Ops Development Stories

Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.