Lab: Intro To Git

Introduction

Today we will explore the leading version control software, Git.


Theory

Here's and excellent video from MIT covering Git. The first 30 minutes should be more than enough to get started.

Developers use Git to track changes to software and to be able to 'roll back the clock' to view old versions of files. Increasingly admins are using Git to track configuration files, particularly Ansible playbooks.

Even admins who choose not to use Git to version their own files, will often have to use Git to install and update software.


Overview

First we will experiment with Git to raise our confidence level. Then we will do some real work with Git:


Part A: Provision systems

We will use the same set of nodes we used for the Ansible lab. If you have not kept your VMs, we can use fresh systems. We do not need any of the configuration changes from the Ansible lab. We just need the servers.

server block diagram
  1. Install git on web01 (cn01 does not need git installed)
    yum install git
    
  2. Tell git who we are (run on web01)
    git config --global user.name "Your Name Comes Here"
    git config --global user.email you@example.com
    

Part B: Practice

Before we start using Git, we should practice a bit. Two options are provided here. The first option is for you to build your own test plan based on the excellent introductory tutorial from the Git team. Option two is to follow the cookbook provided.

Option 1: Build your own tutorial

Option 2: Keep going with this lab...


Part C: Create a repository for /etc

These are the detailed steps to place all of /etc in a Git repo.

  1. As root on web01
    1. Create an empty repo
      cd /etc
      git init
      
    2. Add all files in /etc to the staging area
      git add .
      
    3. Commit the files from the staging area to the master branch
      git commit -m "initial commit"
      
    4. Rename the master branch main
      git branch -M main
      

Demonstrate the value of versioning /etc

  1. logon to web01 as root
  2. add a new host to /etc/hosts
    10.1.1.33	grogu
    
  3. use git to see what's changed in /etc since the last commit
    git status
    
  4. use git diff to see how a specific file has changed
    git diff /etc/hosts
    
  5. commit our change
    git add /etc/hosts
    git commit -m "added grogu to hosts"
    

Part E: Configure our own Git server

Git can work with remote repositories very well. GitHub is the best known git server, and is owned by Microsoft. We will use GitHub shortly. Now we will build our own Git server. note: This is largely an ssh exercise.

Git uses ssh to access remote servers. For this to work smoothly we need to set up key based authentication. Our strategy will be to create a Linux user on cn01 for each of our managed systems (web01)

  1. Set up account and keys:
    1. create a user on cn01: web01git
    2. configure ssh such that root on web01 can logon to cn01 as web01git without typing a password.
  2. Check our work...
  3. Setup remote Git repository for web01 (all commands run as root on web01)
    1. create directory to hold the repo
      ssh web01git@cn01 mkdir ./etc.git
      
    2. initialize Git repo
      ssh web01git@cn01  'cd ./etc.git; git init --bare'
      
    3. add the remote repository to web01
      cd /etc
      git remote add origin web01git@cn01:./etc.git
      
    4. push our changes to cn01
      git push origin main
      
      or
      git push --set-upstream origin main
      

Part F: Use Git clone from GitHub

The content for this course is stored on GitHub. Let's get a local copy on w01.

  1. on w01, as alice...
    mkdir ~/projects
    cd ~/projects
    git clone https://github.com/profjamesmohawk/LinSec.git
    
  2. check our work
    firefox LinSec/index.html
    

Part H: Grading