Day 7 Understanding Package Manager and systemctl

·

7 min read

Day 7 Understanding Package Manager and systemctl

Introduction:
In this post, we'll explore what package managers are, how they work, and their importance in installing and managing software on Linux systems. Then, we'll walk through the steps to install Docker and Jenkins using package managers on Ubuntu and CentOS.


What is a Package Manager in Linux?

A package manager is a tool that simplifies the process of installing, removing, and managing software on an operating system. Whether you're working on Ubuntu or CentOS, package managers save you time by handling dependencies and software installation automatically.

What is a Package?

Packages are files that contain the necessary components to run the software, including executable files and configuration data. They could be GUI applications, command-line tools, or even libraries used by other programs.

Different Kinds of Package Managers

Each Linux distribution typically uses its package manager:

  • For Ubuntu (DEB-based systems), apt-get is commonly used.

  • For CentOS (RPM-based systems), yum or dnf is used.


Installing Docker and Jenkins on Ubuntu and CentOS Using Package Managers

Introduction

In modern DevOps practices, Docker and Jenkins play essential roles. Docker enables containerization, making deploying and managing applications in isolated environments easier, while Jenkins automates the software development lifecycle, providing Continuous Integration and Continuous Deployment (CI/CD).

This guide will walk you through installing Docker and Jenkins on both Ubuntu and CentOS operating systems using their respective package managers.

Installing Docker on Ubuntu

To install Docker on Ubuntu, follow these steps:

  1. Update the package list and install required packages:

     sudo apt update
     sudo apt install apt-transport-https ca-certificates curl software-properties-common
    
  2. Add Docker’s official GPG key:

     curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    
  3. Add the Docker APT repository:

     sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
    
  4. Update the package list again:

     sudo apt update
    
  5. Install Docker:

     sudo apt install docker-ce
    
  6. Check Docker installation:

     sudo systemctl status docker
    

Installing Docker on CentOS

For CentOS, Docker installation involves similar steps:

  1. Update the package manager:

     sudo yum update
    
  2. Install required dependencies:

     sudo yum install yum-utils device-mapper-persistent-data lvm2
    
  3. Add the Docker repository:

     sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    
  4. Install Docker:

     sudo yum install docker-ce
    
  5. Start Docker and enable it to run at startup:

     sudo systemctl start docker
     sudo systemctl enable docker
    
  6. Check Docker status:

     sudo systemctl status docker
    

Installing Jenkins on Ubuntu

Follow these steps to install Jenkins on Ubuntu:

  1. Add the Jenkins repository key:

     curl -fsSL https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
    
  2. Add the Jenkins repository:

     sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
    
  3. Update the package list:

     sudo apt update
    
  4. Install Jenkins:

     sudo apt install jenkins
    
  5. Start Jenkins:

     sudo systemctl start jenkins
    
  6. Ensure JAVA is installed (Jenkins requires Java):

     java -version
    

    If Java is not installed:

     sudo apt install default-jre
    

Installing Jenkins on CentOS

To install Jenkins on CentOS, follow these steps:

  1. Install Java:

     sudo yum install java-11-openjdk
    
  2. Add the Jenkins repository:

     sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
     sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
    
  3. Install Jenkins:

     sudo yum install jenkins
    
  4. Start Jenkins:

     sudo systemctl start jenkins
     sudo systemctl enable jenkins
    

systemctl and Systemd

Systemctl is a command-line utility that allows you to interact with the systemd system and service manager. Systemd is responsible for managing system processes and services on Unix-like operating systems.

Examples:

  • Checking Docker Service Status:

      sudo systemctl status docker
    
  • Stopping the Jenkins Service:

      sudo systemctl stop jenkins
    

You can also use systemctl to automate service management tasks such as starting services at boot time:

  • Enable Docker to start on boot:

      sudo systemctl enable docker
    
  • Disable Jenkins from starting on boot:

      sudo systemctl disable jenkins
    

Conclusion

By following this guide, you’ve learned how to install Docker and Jenkins on both Ubuntu and CentOS using package managers. Understanding how to manage services with systemctl and systemd will further enhance your ability to automate and maintain a robust DevOps pipeline.

1. Check Docker Service Status

After installing Docker, you need to verify if it's running correctly by checking its status using the following command:

sudo systemctl status docker
  • Explanation: This command will display the current status of the Docker service. If it’s running, you’ll see "active (running)" in the output. Make sure to complete the Docker installation steps before running this command.

2. Manage Jenkins Service

You are tasked with stopping the Jenkins service and comparing its status before and after the change.

  • Check Jenkins status before stopping:

      sudo systemctl status jenkins
    

    This shows if Jenkins is running.

  • Stop the Jenkins service:

      sudo systemctl stop jenkins
    

    This stops the Jenkins service.

  • Check Jenkins status after stopping:

      sudo systemctl status jenkins
    

    This should now show the service as "inactive (dead)" after stopping.

  • Post Before and After Screenshots: Capture the terminal before and after you stop the service to demonstrate the status change.


3. Read About Systemctl vs. Service

The systemctl command is a part of systemd, the default init system on most modern Linux distributions, while the service command is an older utility.

  • systemctl:

    • Used to manage services, check status, enable/disable services at boot, and more.

    • Example to check Docker status using systemctl:

        sudo systemctl status docker
      
  • service:

    • A legacy command is used to start/stop/restart services in older Linux distributions or those using SysVinit.

    • Example to check Docker status using service :

    •     sudo service docker status
      

Key Differences:

  • systemctl is more powerful and versatile, providing extended control over system processes and boot sequence management.

  • service is still available for backward compatibility, but most modern distributions have moved towards systemctl as it is more feature-rich.

You can use both commands to manage services, but systemctl is the recommended option on newer systems.

Automate Service Management

You can create a Bash script to automate the starting and stopping of the Docker and Jenkins services. Here’s a simple script you can use:

Service Management Script: manage_services.sh

#!/bin/bash

# Function to start services
start_services() {
    echo "Starting Docker and Jenkins services..."
    sudo systemctl start docker
    sudo systemctl start jenkins
    echo "Services started."
}

# Function to stop services
stop_services() {
    echo "Stopping Docker and Jenkins services..."
    sudo systemctl stop docker
    sudo systemctl stop jenkins
    echo "Services stopped."
}

# Main Menu
echo "Service Management Script"
echo "1. Start Services"
echo "2. Stop Services"
echo "3. Exit"

read -p "Choose an option (1-3): " option

case $option in
    1)
        start_services
        ;;
    2)
        stop_services
        ;;
    3)
        echo "Exiting..."
        ;;
    *)
        echo "Invalid option. Please choose 1, 2, or 3."
        ;;
esac

How to Use the Script:

  1. Create a new file:

     vim manage_services.sh
    
  2. Copy and paste the above script into the file.

  3. Save and exit (in nano, press esc:wq, then Enter.

  4. Make the script executable: chmod +x manage_services.sh

  5. Run the script: ./manage_services.sh


2. Enable and Disable Services

You can use systemctl to enable Docker to start on boot and disable Jenkins from starting on boot with the following commands:

  • Enable Docker to start on boot:

      sudo systemctl enable docker
    
  • Disable Jenkins from starting on boot:

      sudo systemctl disable jenkins
    

You can verify these settings by checking the status:

sudo systemctl is-enabled docker
sudo systemctl is-enabled jenkins

3. Analyze Logs

You can use journalctl to analyze the logs of the Docker and Jenkins services.

  • Check Docker logs:

      journalctl -u docker.service --since "1 hour ago"
    

    This command will show the logs of the Docker service for the last hour. You can adjust the time frame by changing the --since parameter.

  • Check Jenkins logs:

      journalctl -u jenkins.service --since "1 hour ago"
    

    Sample Output from journalctl (Docker):

      Oct 11 15:23:12 ubuntu dockerd[1234]: Docker daemon started
      Oct 11 15:23:15 ubuntu dockerd[1234]: Container started: nginx
    

    Sample Output from journalctl (Jenkins):

      Oct 11 16:40:33 ubuntu jenkins[4321]: Jenkins started successfully
      Oct 11 16:41:00 ubuntu jenkins[4321]: Admin user logged in
    

    Key Insights from Logs:

    • Docker Logs: You can see when the Docker daemon started, any container activity, and potential errors if Docker services fail to start or stop.

    • Jenkins Logs: Jenkins logs provide useful information such as the time Jenkins was started, users logging in, and any errors during startup.

You can further filter logs by date and time, or check for specific errors using:

    sudo journalctl -u docker --since "2024-10-11 12:00:00"
    sudo journalctl -u jenkins -p err

Conclusion

With installing Docker and Jenkins, managing services, automating start/stop processes, enabling and disabling services on boot, and analyzing logs completed, you've tackled all challenges with efficiency and precision.

These foundational service management and automation skills are crucial for maintaining robust systems.