banner



How To Set Up Git Server On Local Network

Cover image for Set Up A GIT Server The Easy Way!

brandon_wallace

Prepare Upwardly A GIT Server The Like shooting fish in a barrel Style!

Introduction

In this tutorial I will evidence you stride-by-step how to fix upwards a local Git server. This is an piece of cake and fun projection that is swell for a dwelling house server, a low-powered unmarried board estimator such as a Beaglebone Blackness or Raspberry Pi, or even a Virtual Individual Server. All you need is Git installed on the server and an SSH key on the client computer.

The benefit to this is that are you will have your own private git server you control, where yous can push, pull, and clone repositories from.

client-server.png

Let'southward brainstorm…

On the server

First affair is to log into the server and install Git.

For an .deb based organization run.

                          $                            sudo              apt update              $                            sudo              apt              install              git                      

Enter fullscreen way Exit fullscreen style

For an .rpm based system run.

                          $                            sudo              yum              install              git  OR              $                            sudo              dnf              install              git                      

Enter fullscreen fashion Exit fullscreen mode

The next pace is to create a git user.

                          $                            sudo              useradd              -r              -m              -U              -s              /bin/fustigate git                      

Enter fullscreen mode Leave fullscreen mode

-r

creates a system user

-g

creates a dwelling directory

-U

create a group name the same as the username.

-south /path/to/shell

Set up the default beat for the user.

You should meet the git user'south home directory created nether /habitation.

                          $                            ls              -lF              /domicile/ full eight drwxr-xr-x  2 git git 4096 Jan 29 20:09 git/                      

Enter fullscreen fashion Exit fullscreen fashion

Set a password for the git user.

                          $                            sudo              passwd git                      

Enter fullscreen mode Exit fullscreen mode

Switch accounts to become the git user.

                          $              su - git                      

Enter fullscreen mode Exit fullscreen mode

Create a .ssh directory in the git user's home folder.

                          $                            mkdir              /habitation/git/.ssh                      

Enter fullscreen mode Exit fullscreen mode

You will encounter that the default permissions are 0755 (drwxr-xr-x).

                          $                            ls              -ld              /home/git/.ssh drwxr-xr-10 2 git git 4096 January 29 xx:25 /home/git/.ssh                      

Enter fullscreen mode Exit fullscreen fashion

We demand to set the permissions on the .ssh directory to 0700 and then that SSH will part correctly. We will gear up the directory permission to 0700 (drwx------) with this command.

                          $                            sudo chmod              0700 /home/git/.ssh                      

Enter fullscreen manner Exit fullscreen mode

Verifying the permissions are set correctly past running "ls -ld".

                          $                            ls              -ld              /home/git/.ssh drwx------ 2 git git 4096 Jan 29 twenty:25 /home/git/.ssh                      

Enter fullscreen mode Exit fullscreen mode

Allow create our offset repository on the server. By convention nosotros append .git to the stop of the directory name. This reminds us that the directory is a repository and not a regular directory.

                          $              git init              --bare              my_project.git                      

Enter fullscreen manner Exit fullscreen mode

Overnice! Let'due south work on the client computer.

On the client estimator

At this time we will create SSH keys with this command.

                          $              ssh-keygen              -t              rsa              -b              4096              -C              "my_email"              -f              ~/.ssh/id_rsa                      

Enter fullscreen way Exit fullscreen mode

-t <type>

Blazon of key (rsa or dsa). Apply rsa.

-b <bits>

Specifies the number of bits in the fundamental to create. Use 4096.

-C "annotate"

Add together a comment.

-f /path/to/key

Specify the path to the rsa file. It is common to name information technology id_rsa.

Upon running the command you lot will see output similar to what is below. Fix a countersign when prompted.

            Generating public/individual rsa central pair. Enter passphrase              (empty              for              no passphrase):              ************              Enter same passphrase once again:              ************              Your identification has been saved              in              .ssh/id_rsa. Your public key has been saved              in              .ssh/id_rsa.pub. The fundamental fingerprint is: SHA256:Yg5D1kaIN9M6ln3sJlHzEggLoKpDYFkhXFR1ZOwP/sI my_email The fundamental's randomart image is: +---[RSA 4096]----+ |oo+*+=ooo+       | |..=.=+o *.       | |oo .+*oo.+       | |+  o=.o +o.      | |.. .o.o+South.o      | |o    =..o. .     | |o     .o. .      | | .       E .     | |          .      | +----[SHA256]-----+                                    

Enter fullscreen mode Leave fullscreen manner

Yous will see that two files have been created. id_rsa is the individual cardinal, id_rsa.pub is the public primal. The permissions should exist prepare to 0600 (-rw-------).

                          $                            ls              -la              .ssh/ total 36 drwx------  2 bw bw 4096 Jan 29 20:43              .              drwxr-xr-ten 54 bw bw 4096 Jan 29 13:17 ..              -rw-------              ane bw bw 3414 January 29 xx:43 id_rsa              -rw-------              1 bw bw  733 Jan 29 20:43 id_rsa.pub                      

Enter fullscreen mode Exit fullscreen mode

The public key needs to be copied over to the git server. To practise that use the ssh-copy-id command. Since my server's IP accost is 192.168.12.34 I run the command similar this to copy the public key over to the server. Modify the IP accost to the IP address of your server.

                          $              ssh-copy-id              -i              ~/.ssh/id_rsa.pub git@192.168.12.34                      

Enter fullscreen way Exit fullscreen mode

-i /path/to/public_key_file

Specify the path to the public primal.

After copying the public key over to the server test logging into the server via SSH. Alter the IP address to the IP address of your server.

                          $              ssh git@192.168.12.34 git@192.168.12.34s countersign:  Last login: Fri Jan 29 xx:35:46 2022 from 192.168.12.100 git@192.168.12.34:~              $                      

Enter fullscreen style Exit fullscreen mode

Type leave to disconnect from the server.

            git@192.168.12.34:~              $                            go out                      

Enter fullscreen fashion Exit fullscreen mode

Now we are set to connect to the repository that nosotros made earlier on the server. Create a directory of the aforementioned name as the repository minus the .git extension. On the server we have a directory called my_project.git then on the client computer y'all create a directory called my_project/.

                          $                            mkdir              my_project/              $                            cd              my_project/                      

Enter fullscreen style Leave fullscreen mode

Add a few of the standard git files.

                          $                            bear on              readme.physician .gitignore LICENSE                      

Enter fullscreen manner Exit fullscreen mode

Add some content to the readme.medico.

                          $                            echo              '# My First Project!'              >>              readme.doc                      

Enter fullscreen manner Exit fullscreen mode

Add all the files to the git staging area so they can be committed.

                          $              git add              --all                      

Enter fullscreen manner Exit fullscreen mode

Commit the files to git. The -chiliad flag allows you to create a short commit message without opening an editor.

                          $              git commit              -m              "Offset commit"                      

Enter fullscreen fashion Leave fullscreen fashion

We need to specify the git server nosotros will be using.
This control will specify a remote git server repository where commits will be stored. The syntax is

$ git remote add together <remote_name> git@<server_ip>:<git_repo>

can be any label yous want. The default <remote_name> you will often see is commonly origin. I gear up my "remote_name" to "local" since I have a local server. Change the IP address to the IP accost of your server.

                          (master)my_project              $              git remote add              local              git@192.168.12.34:my_project.git                      

Enter fullscreen mode Go out fullscreen mode

Push the first commit to the server.

                          (master)my_project              $              git button              local              master                      

Enter fullscreen mode Exit fullscreen mode

Check your remote servers similar this. Right now only 1 repository is

prepare which is called 'local'.

                          (master)my_project              $              git remote              -v              local              git@192.168.12.34:my_project.git              (fetch)              local              git@192.168.12.34:my_project.git              (push)                      

Enter fullscreen fashion Exit fullscreen way

Remember that I said we were going to do this the easy way?
Since we have finished the initial ready up on the server we can create a new repository from the client computer using SSH. No need to log into the server to create the repository. Here is the workflow for creating a new repository on your Git server.

To run a command on a remote server just suspend the control you lot wish to run after the SSH command. This will allow you to run the "git init --bare " command to create a new repository without logging into the server. Run this control changing the IP accost to your server'southward IP address.

                          $              ssh git@192.168.12.34 "git init              --bare              my_second_project.git"                      

Enter fullscreen way Exit fullscreen style

Check that the directory was created on the server.

            ~              $              ssh git@192.168.12.34              "ls -lF"              total 8 drwxr-xr-x vii git git 4096 February  five 17:47 my_project.git/ drwxr-xr-ten seven git git 4096 Feb  six 21:48 my_second_project.git/                      

Enter fullscreen manner Exit fullscreen mode

On the client computer create a directory with the same name as the git repository you created on the server minus the .git extension.

On the server we already have a my_second_project.git directory, and then create the my_second_project/ directory on the client computer.

                          $                            mkdir              my_second_project/              $                            cd              my_second_project/              $                            touch              readme.dr. .gitignore LICENSE                      

Enter fullscreen fashion Exit fullscreen style

Add together some content to the readme.dr..

                          $                            repeat              '# My Second Project!'              >>              readme.md              $              git add              --all              $              git commit              -m              "First commit"              $              git remote add              local              git@192.168.12.34:my_second_project.git              $              git push              local              principal git@192.168.12.34's countersign: ************ Enumerating objects: iv, washed. Counting objects: 100% (4/4), done. Delta compression using up to 2 threads Compressing objects: 100% (ii/2), washed. Writing objects: 100% (4/4), 306 bytes | 306.00 KiB/due south, done. Total 4 (delta 0), reused 0 (delta 0) To 192.168.12.34:my_project.git  * [new co-operative]      primary -> master Co-operative 'primary' set to rails remote branch 'master' from '              local              '.                                    

Enter fullscreen mode Exit fullscreen manner

All done.

Just to review, to create a new local repository you lot merely run the following commands.

Total example:

                          $              ssh git@192.168.12.34 "git init              --bare              nodejs_app.git"              $                            mkdir              nodejs_app/              $                            cd              nodejs_app/              $                            touch              readme.md .gitignore LICENSE              $                            echo              '# My Nodejs App'              >>              readme.md              $              git init              $              git add              --all              $              git commit              -one thousand              "First commit"              $              git remote add              local              git@192.168.12.34:nodejs_app.git              $              git push              local              master                      

Enter fullscreen mode Exit fullscreen fashion

*Annotation:

If you wish to you could easily add a 2d connection to Github if you want to accept that aforementioned repository online by running the 'git remote add' command.
First create a new repository on Github with the same name (my_project) without whatsoever files such every bit the readme.md, .gitignore, or LICENSE because we already created them.

Second run the git remote add command. Here is an example:

                          (chief)my_project              $              git remote add origin git@github.com/brandon-wallace:my_project.git                      

Enter fullscreen mode Exit fullscreen mode

Look at the remote repositories available now.

                          (main)my_project              $              git remote              -5              local              git@192.168.12.34:my_project.git              (fetch)              local              git@192.168.12.34:my_project.git              (push)              origin    git@github.com/brandon-wallace:my_project.git              (fetch)              origin    git@github.com/brandon-wallace:my_project.git              (push button)                      

Enter fullscreen mode Exit fullscreen mode

Now it shows that I have two git servers I can employ, one is called local (my server) the other origin (Github).

BONUS TIP!

Later on logging into your computer yous tin can use ssh-amanuensis to store the rsa key password so that yous do not take to keep typing your SSH private key password every time you do a push to a repository. To store the SSH password run.

                          $                            eval              $(ssh-agent              -due south              )              $              ssh-add /home/brandon/.ssh/id_rsa Enter passphrase              for              /home/bw/.ssh/id_rsa:              ***********              Identity added: /home/bw/.ssh/id_rsa              (comment)                      

Enter fullscreen manner Exit fullscreen manner

See that the environment variable is exists.

                          $                            env              |              grep              SSH              SSH_AUTH_SOCK              =/tmp/ssh-08AsdfQWRTYE/agent.69007              SSH_AGENT_PID              =69007                      

Enter fullscreen way Exit fullscreen mode

Now all your git push commands will not require you typing the password.

                          $              git push origin master Enumerating objects: 6,              washed              .              Counting objects: 100%              (half-dozen/half-dozen),              done              .              Delta pinch using upwardly to 2 threads Compressing objects: 100%              (4/4),              done              .              Writing objects: 100%              (four/4), 518 bytes | 518.00 KiB/southward,              done              .              Total 4              (delta 2), reused 0              (delta 0)              To 192.168.12.34:my_project.git    d06bb48..beee582  master -> master                      

Enter fullscreen way Leave fullscreen mode

Feel free to go out comments, questions, and suggestions.

Source: https://dev.to/brandonwallace/set-up-a-git-server-the-easy-way-jke

0 Response to "How To Set Up Git Server On Local Network"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel