Home System Tutorial LINUX Build a Git server under CentOS

Build a Git server under CentOS

Apr 15, 2024 pm 07:13 PM
linux centos git linux tutorial Red Hat linux system linux command it service linux certification red hat linux linux video

Build a Git server under CentOS

1. First, you need to install Git. You can use the yum source to install it online:
[root@localhost Desktop]# yum install -y git
Copy after login
2. Create a git user to run the git service
adduser git
Copy after login
3. Initialize git repository:

Here we choose /data/git/learngit.git as our git repository

[root@localhost git]# git init --bare learngit.git
Initialized empty Git repository in /data/git/learngit.git/
Copy after login

Executing the above command will create a bare warehouse. The bare warehouse does not have a workspace. Because the Git warehouse on the server is purely for sharing, users are not allowed to log in directly to the server to change the workspace, and the Git warehouse on the server usually All end with .git. Then, change the owner to git:

[root@localhost git]# chown git:git learngit.git
Copy after login
4. Here, the Git server is almost ready.

Next we clone the remote warehouse on the client

Zhu@XXX /E/testgit/8.34
$ git clone git@192.168.8.34:/data/git/learngit.git
Cloning into 'learngit'...
The authenticity of host '192.168.8.34 (192.168.8.34)' can't be established.
RSA key fingerprint is 2b:55:45:e7:4c:29:cc:05:33:78:03:bd:a8:cd:08:9d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.8.34' (RSA) to the list of known hosts.
git@192.168.8.34's password:
Copy after login

Two points to note here: First, when you use Git's clone or push command to connect to GitHub for the first time, you will get a warning:

The authenticity of host 'github.com (xx.xx.xx.xx)' can't be established.
RSA key fingerprint is xx.xx.xx.xx.xx.
Are you sure you want to continue connecting (yes/no)?
Copy after login

This is because Git uses SSH connection, and when the SSH connection first verifies the Key of the GitHub server, you need to confirm whether the fingerprint information of the GitHub Key really comes from the GitHub server. Just enter yes and press Enter. Git will output a warning telling you that the GitHub Key has been added to a trust list on this machine:

Warning: Permanently added 'github.com' (RSA) to the list of known hosts.
Copy after login

This warning will only appear once, and there will be no warnings for subsequent operations. If you are really worried about someone impersonating the GitHub server, before entering yes, you can check whether the fingerprint information of GitHub's RSA Key is consistent with that given by the SSH connection. Second, you are prompted to enter a password to clone. Of course, if you know the password, you can type the password to clone, but the more common way is to use the SSH public key to complete the verification.

5. Create SSH Key

First, check if there is a .ssh directory in the user's home directory. If so, then check if there are two files, id_rsa and id_rsa.pub, in this directory. If they already exist, you can jump directly to the next step. .

If not, open Shell (open Git Bash under Windows) and create SSH Key:

$ ssh-keygen -t rsa -C "youremail@example.com"
Copy after login

You need to change the email address to your own email address, then press Enter all the way, and use the default value. Since this Key is not used for military purposes, there is no need to set a password. If everything goes well, you can find the .ssh directory in the user's home directory. There are two files, id_rsa and id_rsa.pub. These two are the SSH Key pair. id_rsa is the private key and cannot be leaked. id_rsa.pub It is a public key and can be shared with anyone with confidence.

6. Turn on RSA authentication on the Git server

Then you can add your public key to the Git server to verify your information.

On the Git server, you first need to turn on RSA authentication in /etc/ssh/sshd_config, that is:

1.RSAAuthentication yes
2.PubkeyAuthentication yes
3.AuthorizedKeysFile .ssh/authorized_keys
Copy after login

Here we can see that the public key is stored in the .ssh/authorized_keys file. So we create the .ssh directory under /home/git, then create the authorized_keys file and import the newly generated public key into it. Then when you clone again, or when you push later, you don’t need to enter the password again:

Zhu@XXX/E/testgit/8.34
$ git clone git@192.168.8.34:/data/git/learngit.git
Cloning into 'learngit'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.
Copy after login
7. Disable shell login for git users

For security reasons, the git user created in the second step is not allowed to log in to the shell. This can be done by editing the /etc/passwd file. Find a line similar to the following:

git:x:1001:1001:,,,:/home/git:/bin/bash
Copy after login

After the last colon, change it to:

git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell
Copy after login

In this way, the git user can use git normally through ssh, but cannot log in to the shell, because the git-shell we specified for the git user automatically logs out every time he logs in.

The above is the detailed content of Build a Git server under CentOS. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

vscode terminal command cannot be used vscode terminal command cannot be used Apr 15, 2025 pm 10:03 PM

Causes and solutions for the VS Code terminal commands not available: The necessary tools are not installed (Windows: WSL; macOS: Xcode command line tools) Path configuration is wrong (add executable files to PATH environment variables) Permission issues (run VS Code as administrator) Firewall or proxy restrictions (check settings, unrestrictions) Terminal settings are incorrect (enable use of external terminals) VS Code installation is corrupt (reinstall or update) Terminal configuration is incompatible (try different terminal types or commands) Specific environment variables are missing (set necessary environment variables)

See all articles