Build a Git server under Linux

Source: Internet
Author: User
Tags ssh port git clone

Environment: Server CentOS6.6 + git (version 1.7.1)
Client Windows10 + git (version 2.8.4.windows.1)
① Installing Git

Linux as a server-side system, Windows as a client system, install Git separately

Server-side:

#yum install-y git

When you're done installing, check out the Git version

[[email protected] ~]# git--versiongit version 1.7.1

Client:

Download Git for Windows, address: https://git-for-windows.github.io/

After installation, you can use Git Bash as the command line client.

When you're done installing, check out the Git version

$ git--versiongit version 2.8.4.windows.1

② server-side create git users to manage GIT services and set passwords for Git users
[[email protected] home]# ID gitid:git: No this user [[email protected] home]# useradd git[[email protected] home]# passwd git

③ server-side Create Git repository

Set/home/data/git/gittest.git to Git repository

Then change the owner of the Git repository to git.

[[email protected] home]# mkdir-p data/git/gittest.git[[email protected] home]# git init--bare data/git/gittest.gitinit  ialized empty Git repository In/home/data/git/gittest.git/[[email protected] home]# CD Data/git/[[email protected] git]# Chown-r Git:git gittest.git/

④ Client Clone Remote repository

Enter the Git Bash command line client, create the project address (set at D:/wamp64/www/gittest_gitbash) and enter:

[Email protected] mingw64/d$ CD Wamp64/wwwd[email protected] mingw64/d/wamp64/www$ mkdir gittest_gitbash[email protected] Mingw64/d/wamp 64/www$ CD Gittest_gitbash[email protected] mingw64/d/wamp64/www/gittest_gitbash$

Then clone the project from the Linux Git server:

$ git clone [email protected]:/home/data/gittest.git

If SSH is not using the default port 22, you will need to use the following command (assuming the SSH port number is 7700):

$ git clone ssh://[email protected]:7700/home/data/gittest.git

  

When you first connect to a target Git server, you get a hint:

Select Yes:

warning:permanently added ' 192.168.56.101 ' (RSA) to the list of known hosts.

At this time C:\Users\ user name \.ssh will be a file known_hosts, and later on this computer to connect to the target Git server again will not prompt the above statement.

You are prompted to enter a password, which can be verified using the SSH public key.

⑤ client creates SSH public and private keys
$ ssh-keygen-t rsa-c "[Email protected]"

At this time C:\Users\ user name \.ssh will be two more files Id_rsa and id_rsa.pub

Id_rsa is the private key

Id_rsa.pub is the public key

⑥ server-side Git opens RSA authentication

Enter the/etc/ssh directory, edit the Sshd_config, and open the comments for the following three configurations:

Rsaauthentication yespubkeyauthentication yesauthorizedkeysfile. Ssh/authorized_keys

Save and restart the SSHD service:

[Email protected] ssh]#/etc/rc.d/init.d/sshd restart

The path that the public key is stored by Authorizedkeysfile is. Ssh/authorized_keys, which is actually $Home/.ssh/authorized_keys, because the user who manages the git service is git, so the paths that actually store the public key are /home/git/.ssh/authorized_keys

Create a directory under/home/git/. SSH

[Email protected] git]# Pwd/home/git
[[email protected] git]# mkdir. SSH

. .. . bash_logout. Bash_profile. bashrc. gnome2. Mozilla SSH

Then change the owner of the. ssh folder to git

[[email protected] git]# chown-r git:git. Ssh[[email protected] git]# ll-a total consumption 32drwx------. 5 git  git  4096 August  20:04 drwxr-xr-x. 8 root root 4096 August  28 19:32.. -rw-r--r--. 1 git  git    18 October. bash_logout-rw-r--r--. 1 git  git   176 October. Bash_profile-rw-r--r--. 1 git< C8/>git   124 October bashrcdrwxr-xr-x. 2 git  git  4096 November gnome2drwxr-xr-x. 4 git  git
   
    4096 May   8 12:22. mozilladrwxr-xr-x. 2 git  git  4096 August  20:08. SSH
   

⑦ Importing the client public key to the server-side/home/git/.ssh/authorized_keys file

Go back to Git Bash and import the file:

$ SSH [email protected] ' cat >>. Ssh/authorized_keys ' < ~/.ssh/id_rsa.pub

Need to enter password for server-side git user

Go back to the server and see if the Authorized_keys file exists under. SSH:

[[Email protected] git]# CD. Ssh[[email protected]. ssh]# ll total dosage 4-rw-rw-r--. 1 git git 398 August  20:08 Authorized_keys

You can see if it is the client-generated public key.

Important:

Modify the permissions for the. SSH directory to 700

Modify the permissions for the. ssh/authorized_keys file to 600

⑧ Client clone remote repository again
$ git clone [email protected]:/home/data/git/gittest.git

To view the client project directory:

The project has been clone.

You can also use the Tortoisegit client to manage projects:

Clone

⑨ disable git user SSH login server

Git users who were previously created on the server do not allow SSH to log on to the server

Edit/etc/passwd

Found it:

Git:x:502:504::/home/git:/bin/bash

Revision changed to

Git:x:502:504::/home/git:/bin/git-shell

At this point, git users can use git through ssh, but they can't login to the system via SSH.

Reference: Build your own git server under CentOS

Build a Git server under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.