Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Git server setup

Git server setup

In the previous chapter, we used Github as the remote repository. Github's public projects are free, and since 2019 Github private repositories can also be used without limits.

Of course, we can also set up our own Git server to use as a private repository.

Using a bare repository (Bare Repository)

1. Install Git

Install Git on an Ubuntu server:



sudo apt install git

If your system is Centos/RedHat, the installation command is:



yum -y install git-core

Fedora installation command:

# yum install git (Fedora 21 及之前的版本)

# dnf install git (Fedora 22 and newer versions)

Next, we create a git user group and user to run the git service:



$ groupadd git

$ useradd git -g git

2. Create a bare repository

Log in as the Git user, then create a bare repository in its home directory.

$ sudo su - git

First, we choose a directory as the Git repository, assumed to be /home/gitrepo/runoob.git, and enter the command in the /home/gitrepo directory:



$ cd /home

$ mkdir gitrepo

$ chown git:git gitrepo/

$ cd gitrepo



$ git init --bare runoob.git

The above command creates an empty repository with Git. Git repositories on servers usually end with .git. Then, change the repository owner to git (if operated by another user, such as root):



$ chown -R git:git runoob.git

3. Create certificate login

Add your public key to ~/.ssh/authorized_keys to allow remote access.

Collect the public keys of all users who need to log in (the public keys are in the id_rsa.pub files) and import our public keys into the /home/git/.ssh/authorized_keys file, one per line.

If the file doesn't exist, create it:



$ cd /home/git/

$ mkdir .ssh

$ chmod 755 .ssh

$ touch .ssh/authorized_keys

$ chmod 644 .ssh/authorized_keys

# Add your SSH public key to the file

4. Clone the repository



$ git clone git@192.168.45.4:/home/gitrepo/runoob.git

Cloning into 'runoob'...

warning: You appear to have cloned an empty repository.

Checking connectivity... done.

192.168.45.4 is the IP of the server where Git is located; you need to change it to your own Git server IP.

This completes the installation of our Git server.


Using GitLab

GitLab is a powerful Git service management tool suitable for medium and large teams, providing rich user management, CI/CD, code review, and other features.

** 1. Install GitLab**

based on GitLab official documentation Install GitLab.

For example, on Ubuntu:

# sudo apt-get update

# sudo apt-get install -y curl openssh-server ca-certificates tzdata perl

# curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.deb.sh | sudo bash

# sudo EXTERNAL_URL="http://yourdomain" apt-get install gitlab-ee

EXTERNAL_URL="http://yourdomain" should be set to your own domain name or public IP, for example:



sudo EXTERNAL_URL=101.132.XX.XX yum install -y gitlab-ee

2. Configure GitLab

After installation, open a browser and visit http://yourdomain to set up the administrator account.

When an echo message similar to the following appears, GitLab has been installed successfully.

3. Create a project

Log in to GitLab and create a new project with the username root.

Obtain the login password:

sudo cat /etc/gitlab/initial_root_password

The result is as follows:

Use the username root for the first login:

4. Generate a key pair file and obtain the public key

Install the Git tool (skip if already installed):



sudo apt-get install git

Generate the key pair file id_rsa:



ssh-keygen

During key pair generation, the system will prompt you to enter the key pair storage directory (default is .ssh/id_rsa under the current user's directory, e.g., /home/test/.ssh/id_rsa) and the key pair password. You can enter them manually or press Enter to keep the defaults.

After generation, the information shown below is displayed:

View and copy the content of the public key file id_rsa.pub:



cat .ssh/id_rsa.pub

Information similar to the following is displayed:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDQVwWjF3KXmI549jDI0fuCgl+syJjjn55iMUDRRiCd/B+9TwUda3l9WXH5i7RU53QGRCsDVFZxixLOlmXr9E3VSqkf8xXBnHs/5E2z5PIOCN0nxfB9xeA1db/QxPwK4gkHisep+eNHRn9x+DpCYDoSoYQN0nBg+H3uqfOqL42mJ+tqSfkyqbhjBf1kjtDTlBfVCWtI0siu7owm+c65+8KNyPlj5/0AyJ4Aqk1OX2jv+YE4nTipucn7rHwWuowasPU86l+uBsLNwOSb+H7loJvQyhEINX2FS1KnpRU+ld20t07n+N3ErfX5xBAGfxXpoN9BKKSP+RT7rvTeXTVE**** test@runoob.com****

** 5. Create a project**

On the GitLab homepage, click the Create a project option:

Click Create blank project, set the Project name and Project URL, then click Create project:

This article uses the mywork project as an example.

6. Add SSH key

On the current project page, click Add SSH key:

Paste the content of the public key file id_rsa.pub into the text box for Key:

Click Add key. After the SSH key is added, it is shown below:

Copy the Clone link, which is needed when performing a clone operation:

Using GitLab

1. Configure the information of the person using the Git repository, including username and email.



git config --global user.name "testname" 

 

git config --global user.email "abc@example.com" 

2. Clone the created project locally.



git clone git@101.132.XX.XX:root/mywork.git

** 3. Upload files to the GitLab server:**

Enter the project directory:



cd mywork/ 

Create the target file to upload to GitLab:



echo "test" > /home/test/test.sh

Copy the target file or directory into the project directory:

cp /home/test/test.sh ./ 

Add the test.sh file to the index:

git add test.sh

Commit test.sh to the local repository:

git commit -m "test.sh"

Synchronize the file to the GitLab server:



git push -u origin main

View on the web page that the uploaded test.sh file has been synchronized to the GitLab server: