Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Hand in hand to teach you to build Git server on Ubuntu

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Recently, I have been writing some scripts, and I have time at home to catch up in order to maintain continuity, so I have encountered the problem of code synchronization, and there are three ways to think of it:

Using the cloud disk, this is ready to use, but because the cloud disk also stores some other things, it is not always appropriate to synchronize in real time.

Using SVN, the traditional code hosting tool, has been using

Using Git, the latest distributed code hosting tool, is said to be powerful.

Finally decided to use Git, mainly to learn the latest technology. Here are the steps to take notes and give guidance to other students at the same time:

Let's talk about the environment first:

Server: Ubuntu 16.04x64

Client: Windows7 x64

Server configuration

First, use the following command to switch to the root permission to operate:

Sudo bash

When prompted for a password, enter the root password.

After logging in successfully, start installing git, command:

Apt-get install git

When prompted to continue, enter y and enter, and the installation process continues until the installation is complete.

Then start installing ssh, with the command:

Apt-get install openssh-server openssh-client

Also when asking if you want to continue, enter y and enter, and the installation will be completed automatically.

Then we create a new git exclusive user, whose user name is also git, and command:

Adduser git

After the new project is successful, you will be prompted to set the user's password. Please set a password that you can remember and continue. Follow-up details can be filled in as appropriate.

Let's start to build a new git warehouse. We select the warehouse directory under / srv, and the warehouse name is myfiles.git, so the command:

Git init-bare / srv/myfiles.git

Since the current user is root, in order to enable the git dedicated account to operate the warehouse directory, we need to authorize the warehouse directory to git, and command:

Chown-R git:git / srv/myfiles.git/ client operation

First, you need to download the Windows version of git. Download address: click download

Click install when the download is complete, and click "next" as prompted until the installation is complete.

Create a working directory on the client side, such as mine is gitdir, right-click in the working directory and click "Git Bash Here".

After the pop-up command, window clone repository to local:

$git clone git@192.168.252.128:/srv/myfiles.gitCloning into 'myfiles'...The authenticity of host' 192.168.252.128 (192.168.252.128) 'can't be established.ECDSA key fingerprint is SHA256:zqtjAg+FGfWrT3SCp1Qa2KqhE2UOy3PmudhhrTFlm7A.Are you sure you want to continue connecting (yes/no)? YesWarning: Permanently added '192.168.252.128' (ECDSA) to the list of known hosts.git@192.168.252.128's password:warning: You appear to have cloned an empty repository.

Note that please replace "192.168.252.128" with your own server ip, and enter "yes" when confirming, and finally enter the password when creating the git user.

In order for the client to perform subsequent submission operations, we also need to indicate the user information of the current machine, as follows:

Git config-global user.email "you@example.com" git config-global user.name "Your Name"

After registration, commit will use this registration information to record the operator information, and then you can see the corresponding information when using git log, as shown below:

$git logcommit ae72bcc89ea8f5d9d3a44f0e00cf35e91a1afce8 (HEAD-> master, origin/master) Author: sylan215 Date: Wed Oct 18 18:37:41 2017 + 0800 Test submission

At this point, we have completed the entire configuration process.

File modification and synchronization to Git server

After the configuration is completed, we will enter the actual use of the link.

First, we modify several files, copy them to the myfiles directory, then submit them to the server, and run the submit command under myfiles:

Git add .git commit-am "test submission" git push

Command line content with output:

$git add. $git commit-am "Test submission" [master (root-commit) ae72bcc] Test submission 1 file changed, 1 insertion (+) create mode 100644 test.txt$ git pushgit@192.168.252.128's password:Counting objects: 3, done.Writing objects: 100% (3test.txt$ git pushgit@192.168.252.128's password:Counting objects 3), 223 bytes | 223.00 KiB/s, done.Total 3 (delta 0), reused 0 (delta 0) To 192.168.252.128:/srv/myfiles.git * [new branch] master-> master

The place where you are prompted to enter your password is still the password of your git account.

Description: for detailed commands on git operation, please refer to this article

After the submission is successful, we use the command git pull to synchronize the latest content on another machine:

$git pullgit@192.168.252.128's password:remote: Counting objects: 3, done.remote: Total 3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3amp 3), done.From 192.168.252.128:/srv/myfiles ae72bcc..afad897 master-> origin/masterUpdating ae72bcc..afad897Fast-forward test.txt | 3 + +-1 file changed, 2 insertions (+), 1 deletion (-)

For complex multi-user operations, please refer to this article

Other configurations disable shell for git accounts

For security reasons, if you need to disable shell for the git account, you can modify the / etc/passwd file:

Put one of them

Git:x:1001:1001:git-user,:/home/git:/bin/bash

Modify to

Git:x:1001:1001:git-user,:/home/git:/usr/bin/git-shell

The path to / usr/bin/git-shell can be obtained from the command which git-shell.

Use public and private keys to achieve secret-free effect

In the above process, we need to enter the git user's password to interact with the server, which will be very troublesome. At this time, we can configure public and private keys to achieve secret-free.

First, you need to generate a public and private key on the client:

Ssh-keygen-t rsa

Enter will be prompted to enter the password of the private key. If you want to avoid secret, enter directly, otherwise customize a password (if you have customized a password, enter the set password every time you push and pull).

After the command is executed successfully, the files "id_rsa" and "id_rsa.pub" are generated in the .ssh folder of the current user directory (the Windows directory is X:\ Users {username} .ssh, Linux is / home/ {username} / .ssh), where the .pub file is the public key and the other is the private key.

Copy the file "id_rsa.pub" to the server and set it using the following command:

Mkdir / home/git/.sshcp / home/currentuser/Desktop/id_rsa.pub / home/git/.ssh/authorized_keyschown-R git:git / home/git/.ssh

Note: if the authorized_keys file does not exist, you can use the cp command, otherwise use the cat command to append, for example:

Cat / home/currentuser/Desktop/id_rsa.pub > > / home/git/.ssh/authorized_keys

To ensure that the configuration takes effect, you also need to check whether the following settings are enabled in the / etc/ssh/sshd_config file:

AuthorizedKeysFile h/.ssh/authorized_keys

Whether it has been commented out, if so, you need to remove the previous # and restart the ssh service (command service ssh restart).

After all the configuration is complete, let's try the effect:

$git pullAlready up-to-date.

Look, there is no prompt for the password this time, the secret-free setting takes effect.

Non-22 port connection Git

In the .ssh configuration directory, the config file is now available, which reads:

Host ip address port port name

The location of the config configuration file on Windows and Mac systems is: X:/users/username/.ssh directory, where X is the system disk and username is the current login user name

For liunx series systems, the location is the / home/username/.ssh directory, where username is the current login user name.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report