Sunday, 9 December 2018

Git: Configure Local Git Server/Clients in RHEL 7.6

Git Configure Local git Server Clients in RHEL 7.6

git is a famous version control system. git is used to track changes in computer files and to coordinate work on those files among multiple people. git is primarily used for source-code management by software development teams, however, it can be used to keep track of changes in any set of files. git is freeware and open source. It is packaged with almost every distribution of Linux operating systems.

In this article, we will configure a local git server, then we will create our first git repository for our project, and finally access it from a remote client.

 

This Article Provides:

     

    System Specification:

    In this article, we have two virtual machines, one as the git server, and the other as the git client. Their specifications are:

    Hostname: git-server.itlab.com git-client.itlab.com
    IP Address: 192.168.116.129/24 192.168.116.128/24
    Operating System: RHEL 7.6 RHEL 7.6

     

    Install git Server:

    git rpm is provided in RHEL/CentOS ISO, therefore we can easily install git from a local yum repository.

    Connect to git-server.itlab.com using ssh, and install git by using yum command.

    [root@git-server ~]# yum install -y git Loaded plugins: product-id, search-disabled-repos, subscription-manager This system is not registered with an entitlement server. You can use subscription-manager to register. Resolving Dependencies --> Running transaction check ---> Package git.x86_64 0:1.8.3.1-19.el7 will be installed --> Processing Dependency: perl-Git = 1.8.3.1-19.el7 for package: git-1.8.3.1-19.el7.x86_64 --> Processing Dependency: perl(Git) for package: git-1.8.3.1-19.el7.x86_64 --> Running transaction check ---> Package perl-Git.noarch 0:1.8.3.1-19.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: git x86_64 1.8.3.1-19.el7 localyum 4.4 M Installing for dependencies: perl-Git noarch 1.8.3.1-19.el7 localyum 55 k Transaction Summary ================================================================================ Install 1 Package (+1 Dependent package) Total download size: 4.4 M Installed size: 22 M Downloading packages: -------------------------------------------------------------------------------- Total 33 MB/s | 4.4 MB 00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : perl-Git-1.8.3.1-19.el7.noarch 1/2 Installing : git-1.8.3.1-19.el7.x86_64 2/2 Verifying : git-1.8.3.1-19.el7.x86_64 1/2 Verifying : perl-Git-1.8.3.1-19.el7.noarch 2/2 Installed: git.x86_64 0:1.8.3.1-19.el7 Dependency Installed: perl-Git.noarch 0:1.8.3.1-19.el7 Complete! [root@git-server ~]#

    We need a user to own our git repository. Therefore, we add git user and set the password as follows:

    [root@git-server ~]# useradd git [root@git-server ~]# passwd git Changing password for user git. New password: BAD PASSWORD: The password is shorter than 8 characters Retype new password: passwd: all authentication tokens updated successfully.

     

    Create Your First git Repository:

    Now, login with git user and create a local git Repository.

    [root@git-server ~]# su - git [git@git-server ~]$ mkdir ~/repo [git@git-server ~]$ cd ~/repo [git@git-server repo]$ git init --bare --shared project1 Initialized empty shared Git repository in /home/git/repo/project1/

    Here, we have created a directory repo to keep all of our projects' repositories in a single place. Then, we have created a bare repository for our project1 project.

    Note: A bare repository has no working area, therefore, it is not possible to add files to it locally. While a shared repository is used to allow project members to push changes to the git server.

    Enable post-update hook by copying the sample file as follows:

    [git@git-server repo]$ cd ~/repo/project1/hooks/ [git@git-server hooks]$ cp post-update.sample post-update

    git access remote repositories via ssh service, therefore, no explicit Linux firewall configuration is required.

    Our git server has been successfully configured and we have created an empty repository for our first git project.

     

    Configure a git Client:

    Connect to git-client.itlab.com using ssh with root user.

    Configure name resolution by adding following line in /etc/hosts.

    [root@git-client ~]# echo "192.168.116.129 git-server.itlab.com git-server" >> /etc/hosts

    Install git rpm by using yum command.

    [root@git-client ~]# yum install -y git Loaded plugins: product-id, search-disabled-repos, subscription-manager This system is not registered with an entitlement server. You can use subscription-manager to register. Package git-1.8.3.1-19.el7.x86_64 already installed and latest version Nothing to do

    git is already installed on git-client.itlab.com, therefore, yum didn't perform any installation.

    Connect as ahmer user and configure keybased-authentication between ahmer@git-client.itlab.com and git@git-server.itlab.com.

    [ahmer@git-client ~]$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/ahmer/.ssh/id_rsa): Created directory '/home/ahmer/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/ahmer/.ssh/id_rsa. Your public key has been saved in /home/ahmer/.ssh/id_rsa.pub. The key fingerprint is: SHA256:qJKo3QumkS/Rz/LOqR3gCEOdYH9lLaeGgS1B/QkqrkE ahmer@git-client.itlab.com The key's randomart image is: +---[RSA 2048]----+ | o.o= o. | |. +o.=o. o | | . +o.= = | |.E ... * | |=.o o S | |+B.o . | |*o*oo | |+*.*oo | |o.+=O. | +----[SHA256]-----+ [ahmer@git-client ~]$ ssh-copy-id git@git-server.itlab.com /bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/ahmer/.ssh/id_rsa.pub" The authenticity of host 'git-server.itlab.com (192.168.116.129)' can't be established. ECDSA key fingerprint is SHA256:PhsrMh10ZgS3G8P/upEd5bIbjTbUW0Asbbgtsq9y3Xs. ECDSA key fingerprint is MD5:24:af:0c:3a:33:39:2b:2a:d5:3d:64:05:a2:b6:a1:b8. Are you sure you want to continue connecting (yes/no)? yes /bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys git@git-server.itlab.com's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'git@git-server.itlab.com'" and check to make sure that only the key(s) you wanted were added. [ahmer@git-client ~]$ ssh git@git-server.itlab.com Last login: Sun Dec 9 00:22:46 2018 from git-server.itlab.com [git@git-server ~]$

    Set following git variables for the ahmer user.

    [ahmer@git-client ~]$ git config --global user.name "ahmer" [ahmer@git-client ~]$ git config --global user.email "ahmer@git-client.itlab.com"

    Create a directory for keeping local git repositories.

    [ahmer@git-client ~]$ mkdir ~/repo [ahmer@git-client ~]$ cd ~/repo

    Create a clone of project1 git repository.

    [ahmer@git-client repo]$ git clone git@git-server.itlab.com:~/repo/project1 project1 Cloning into 'project1'... warning: You appear to have cloned an empty repository. [ahmer@git-client repo]$ ls project1 [ahmer@git-client repo]$ cd project1 [ahmer@git-client project1]$ ls [ahmer@git-client project1]$

    We have created a clone of project1 repository on our local machine. You can see that currently there isn't any file in the repository. Let's create some files therein.

    [ahmer@git-client project1]$ echo "my first file" > file1.txt [ahmer@git-client project1]$ cp /etc/hosts . [ahmer@git-client project1]$ ls file1.txt hosts

    Add these two files to our git repository.

    [ahmer@git-client project1]$ git add . [ahmer@git-client project1]$ git commit -am "My First Commit" [master (root-commit) 10394a7] My First Commit 2 files changed, 4 insertions(+) create mode 100644 file1.txt create mode 100644 hosts

    Push these changes to the remote git repository at git-server.itlab.com.

    [ahmer@git-client project1]$ git push origin master Counting objects: 4, done. Compressing objects: 100% (3/3), done. Writing objects: 100% (4/4), 362 bytes | 0 bytes/s, done. Total 4 (delta 0), reused 0 (delta 0) To git@git-server.itlab.com:~/repo/project1 * [new branch] master -> master

    To verify the changes, connect to git-server.itlab.com via ssh and check the log.

    [git@git-server project1]$ git log commit 10394a774797bc5c5313b9aae086aa1ab71c69b5 Author: ahmer Date: Sun Dec 9 01:04:16 2018 -0500 My First Commit

    We have successfully configured our git server and client in the Red Hat Enterprise Linux (RHEL) 7.6 environment.

    Git: Configure Local Git Server/Clients in RHEL 7.6


    YOU MIGHT ALSO LIKE:

    6 comments:

    1. Very well explained thank you :!

      ReplyDelete
      Replies
      1. Hello,
        I'm a systems engineer and am trying to set up a git server. so far the tests went great thanks to your guide, when i run git log on the client i see he's committed a few changes. however i can't seem to find them on the git server.
        How doest it work practically? should the client be the web app server and i push the changed from the git server ?
        explanation would be wonderful thanks again

        Delete
      2. Hi, "git log" by default shows commit log of current branch. You should try using "git log --all" to see all commit logs.

        Delete
    2. Hi, what are the best practice for securing git server.

      ReplyDelete
      Replies
      1. Hi, this article was already written by considering the best practices and security considerations.

        Delete