4
头图

Remark:

This article refers to teacher Liao Xuefeng’s blog Git tutorial . Learn and record according to his blog, thank him for selfless sharing, and welcome everyone to view the original text.

Knowledge points

  • apt-get install git install git, create user adduser username
  • .shh/authorized_keys under the new user's home directory stores the private keys of users who manage to connect to the Git remote repository.
  • git init --bare reponame.git Create an empty remote warehouse, and set the owner of the directory and subdirectories where the remote warehouse is located as a new user, chown -R username:usernameGroup sample.git/
  • Modify the file /etc/passwd disable terminal login for shell

Build a Git server

The Git remote warehouse is essentially the same as the local warehouse, except that it can provide permanent online services

The following demonstrates building a Git server Ubuntu

  • Install git:
$ sudo apt-get install git
  • Create a git user to run the git service
$ sudo adduser git
Use passwd git to change the password for the git user.
  • Create certificate login

Creating a certificate to log in github or gitee . The Git server needs to collect the public key of the client that needs to log in ( id_rsa.pub created by the user),

Collect the public keys of all users who need to log in, that is, the user's id_rsa.pub file. Import the public key into the /home/git/.ssh/authorized_keys file, one per line.

By default, the newly created git user's home directory has no hidden directory .ssh You can create directories and files manually.

Create a new user, the default file generated under the home directory is determined by the content in the /etc/skel

Meanwhile Ubuntu Under New User, home (home) directory desktop, need to use the new directory user login ubuntu the desktop, the desktop will be generated, pictures, documents, downloads, music and other directories.

  • Initialize the Git repository:

Select a directory as the Git repository, assuming it is /srv/sample.git , enter the command in the /src

$ sudo git init --bare sample.git
[sudo] git 的密码:
初始化空的 Git 仓库于 /srv/sample.git/

Git will create an empty warehouse with no workspace, which is only used for sharing, so users should not be allowed to directly log in to the server to modify the workspace.

The server Git repository usually .git with 0612df6ab0d69a.

  • git the owner of the directory to 0612df6ab0d6c5.
$ sudo chown -R git:git sample.git/
  • Disable git user shell login

For security reasons, the newly created git user is not allowed to log in to shell . Set by editing the /etc/passwd file.

Open the passwd file and find the following line:

git:x:1001:1002:,,,:/home/git:/bin/bash

Modify it to

git:x:1001:1002:,,,:/home/git:/usr/bin/git-shell

Login with git at this time will not be allowed

$ su git
密码:
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.

su command is used to switch login users

sudo used to obtain root administrator permissions and execute commands

su [username] Switch to the specified user without changing environment variables

su - [username] Switch and change the environment variable for the specified user (commonly used)

git users can normally by ssh use git, but could not land shell , because as git specified user git-shell every time I log in automatically withdrawn.

  • Clone the remote warehouse.

Now you can git clone the remote warehouse /srv/sample.git/ through 0612df6ab0d888.

You can set a server name to access the cloned remote warehouse. For example, set gitsever to the local hosts, and ip is the address of the Git server.
$ git clone git@gitsever:/srv/sample.git
Cloning into 'sample'...
The authenticity of host 'gitsever (192.168.104.237)' can't be established.
ECDSA key fingerprint is SHA256:SYG7vL********************y597FA.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitsever,192.168.104.237' (ECDSA) to the list of known hosts.
warning: You appear to have cloned an empty repository.

You will be prompted to clone an empty git warehouse, check the contents of the warehouse.

$ cd sample/

/sample (master)$ ls -al
total 4
drwxr-xr-x 1 win7hostsver 197121 0 May  4 14:29 .
drwxr-xr-x 1 win7hostsver 197121 0 May  4 14:29 ..
drwxr-xr-x 1 win7hostsver 197121 0 May  4 14:29 .git

Later, you can use this remote library for git operations, synchronization, etc.

Public key management

As above, a simple git server has been set up. Then you can add other warehouses, other users' public keys, etc. for management.

  • Public key path location: /home/git/.ssh/authorized_keys

But for large-scale or multi-person git servers used and managed, you can use Gitosis manage public keys.

Permission management of git server warehouse

Git itself was linux , so it pays attention to the spirit of open source and does not support permission control.

But Git provides related hooks, which can be used to write a series of scripts to control the submission of the warehouse and other operations, so as to achieve control permissions. Gitolite is a tool that provides management permissions, you can learn more if you need it


代码迷途
27 声望1 粉丝