preface

Install git services on your personal VPS. All the content of this article is carried out under the Ubuntu 14.04 system of Aliyun ECS.

background

The development project of the company’s headquarters can not be put on the Github open source repository, which requires a certain cost. How can the free Coder be constrained in this way? Think of putting the source code on your company’s servers, along with a version management tool, preferably Git! Next, let’s discuss how to set up Git service on the server. Finally, if the function is rich enough, plus the front-end page, it can also be as on Github! It sounds lofty, in fact, everything is very simple after learning.

Configuring the Server

What information is the developer of the original version of the most clear and comprehensive, first Po reference article: “Git official documentation – server on the Git- configuration server” in fact, the above is enough comprehensive, but for small white, or can not be very good understanding, here I will supplement perfect. PS: The following is a general convention

Terminal A is the git-bash or other terminal for logging in to the remote server. Terminal B is the git-bash or other terminal for local operations

Install and configure the Git tool on the Linux server

Install git tools:

Update the installation source
$ sudo apt-get update
Install git tools
$ sudo apt-get install git
Copy the code

The git configuration:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"
Copy the code

Git configuration:

git config --list
Copy the code

Create a Linux server user and add an SSH trust

Create a new git user for your Linux server. My Linux server is a test server rented by ali. Log in as root user first and then operate the following. PS: the user prompt $in the code can be treated as #, because it is root superuser, not written as #, because it will become comment terminal A

$ adduser -d /home/git -m git  Git will be created if the user directory does not exist
Enter the password of the git user. This step will not be shown here
$ chmod 755 /home/git
$ su git
$ mkdir .ssh && chmod 700 .ssh
$ touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys
Copy the code

PS1: Once you have created a user, you will automatically create a folder in the /home/git directory corresponding to the user name. After the above operation, you will automatically create a folder in the /home/git directory. Assuming that we have established SSH trust with Github before, or have read my “One-click Deployment of front-end code to server” and can create the local SSH private key and public key files, let’s continue. Open a new git-bash terminal B to find our id_rsa.pub public key file directory and copy it remotely to the server: terminal B

$ cd ~/.ssh/
$ ls ./id_rsa.pub
$ scp ./id_rsa.pub [email protected]:/home/git
Copy the code

Go back to terminal A and continue: Terminal A

$ su git  Switch to the git user
$ cd /home/git  Git user's home directory
$ cat ./id_rsa.pub >> .ssh/authorized_keys  # append id_rsa.pub to authorized_keys
Copy the code

Log out of SSH and log in as the Git user again without entering the password of the git user

Test whether you can log in without password:

ssh -v git@SERVER
Copy the code

Build a Git repository for the newly created Git user

Log in to the server remotely using SSH as the Git user:

$SSH [email protected] Linux creates a new repository for developers with the 'git init' command with the '--bare' option, which does not create a working directory when initializing the repository: 'sh $'cd /home/git  # The new repository will be located in a folder that is convenient for organization and management. I will directly place it in the home directory of git users
$ mkdir testProject.git Create a new repository directory
$ cd testProject.git Enter the new warehouse directory
$ git init --bare Initialize the new repository
Initialized empty Git repository in /home/git/testProject.git/
Copy the code

At this point, a git repository exists in their server setup, the warehouse address is: [email protected]: / home/git/testProject. Git, a look, wow, with projects on making address format is the same! For example: [email protected]: ironmaxtory/ironmaxtory. Making. IO. Git, the in the mind is really exciting! Your own server’s /home/git repository is now like a small, humble Github repository, with the same ability to add new repositories and store new project code indefinitely.

Associate the Git repository on the operation server

After the above two steps, you can associate the git repository on the server and upload and save your own code:

$ cd testProject  # Enter your project code root directory locally first
$ git remote add tsOrigin [email protected]:/home/git/testProject.git  Add remote server repository and nickname it tsOrigin, different from origin repository on Github
$ git push tsOrigin master  Push code to remote repository
Copy the code

Add multiplayer collaboration

This scenario is mandatory and common; the company’s project code is generally maintained by multiple people; Even if it’s not maintained by one person, the ultimate purpose of uploading to the test server is to download and upload the submission code on multiple computers, so add collaboration! Actually is also very simple, only need the cooperation of PC on the SSH public key appended to the git user buyers directory license key file/home/git /. SSH/authorized_keys can, using the cat command.

Restrict developers from remotely logging in to Linux servers

Note that currently all (authorized) developer users can log into the server as the system user Git to get a normal shell. With the help of a limited shell tool called Git-shell, you can easily limit the user’s Git activities to those related to Git. This tool comes with the Git package. If you set git-shell to user git’s login shell, user Git will not have normal shell access to this server. To use git-shell, you need to replace bash or CSH with it as the login shell for system users. To do this, you must first make sure that the git-shell exists in the /etc/shells file and add the location of the git-shell to the /etc/shells directory

$ cat /etc/shells   # see if `git-shell` is already in there. If not...
$ which git-shell   # make sure git-shell is installed on your system.
$ sudo vim /etc/shells  # and add the path to git-shell from last command
Copy the code

You can then use CHSH

to change the shell of any system user:

$ sudo chsh git  # and enter the path to git-shell, usually: /usr/bin/git-shell
Copy the code

In this way, the user git can only use SSH connection to push and pull the Git repository, but can not log in to the machine and get the ordinary shell. If you try to log in, you will find that the attempt is rejected, like this:

$ ssh [email protected]
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.
Connection to gitserver closed.
Copy the code

Welfare dedication

User-defined domain name mapping the IP address of the remote server

Every time remote login should be such SSH [email protected], very troublesome to have? ! No domain name and more disgusting after the IP address complex? ! Let’s configure the domain name for the local server! Edit/c/Windows/system32 / drivers/etc directory hosts file, use editplus open can, add a line as follow:

114.xxx.xxx.xxx (space) ts.aliyun.com (return to line)Copy the code

Save exit, generally, does not work immediately. There are several solutions, but I used all of them anyway

  1. Under DOS interface
  2. Network configuration, add hosts file
  3. Control Panel -> Administrative Tools -> Services ->DNS Client manual

In the future, this can be done:

ssh [email protected]
Copy the code

About me

Welcome to [program ape uncle He] public account your attention is my motivation