Skip to main content

Command Palette

Search for a command to run...

Day 7 Task: Understanding package manager and systemctl

Published
9 min read
A

DevOps | Cloud Practitioner | AWS | GIT | Kubernetes | Terraform | ArgoCD | Gitlab

What is a Package Manager in Linux?

A package manager in Linux is a tool that automates installing, updating, and removing software. It handles dependencies, ensuring all needed libraries are installed with the main software, making software management easier.

Key Functions of a Package Manager:

  1. Install Software: Package managers download and install software along with all necessary dependencies.

  2. Update Software: It checks for updates and installs them, ensuring the system and software are up-to-date.

  3. Remove Software: It can cleanly remove software, along with unnecessary dependencies or files.

  4. Handle Dependencies: Ensures that all libraries and other dependent software required by a package are installed or updated together.

Types of Package Managers:

There are different types of pacDEB-based Package Managers (used in Debian, Ubuntu, and derivatives):

  • APT (Advanced Package Tool): A powerful command-line tool that helps manage .deb packages.

    • Example commands:

      • sudo apt update (updates the package list).

      • sudo apt install <package> (installs a package).

      • sudo apt remove <package> (removes a package).

  • dpkg: The lower-level tool for managing .deb packages directly.

    • Example: sudo dpkg -i <package.deb> installs a .deb package.
  • RPM-based Package Managers (used in Red Hat, CentOS, Fedora):

    • YUM (Yellowdog Updater Modified): A higher-level tool that works with .rpm packages and handles dependencies.

      • Example commands:

        • sudo yum install <package> (installs a package).

        • sudo yum update <package> (updates a package).

        • sudo yum remove <package> (removes a package).

    • DNF: The newer package manager used in Fedora and CentOS, replacing yum in newer versions.

      • Example commands:

        • sudo dnf install <package> (installs a package).

        • sudo dnf upgrade (upgrades all packages).

These are some important package manager you should be aware of.

What is a Package?

A package is essentially an archive file containing all the files needed to install and run a specific software application. This includes:

  • Binary files (the executable files).

  • Configuration files.

  • Metadata about the package, such as version, dependencies, etc.

Packages can also include libraries and modules required by other software to function.

Why Use a Package Manager?

  • Simplicity: Users don't need to manually download software, compile it, or track dependencies.

  • Automation: Handles upgrades and security patches automatically, improving system stability and security.

  • Consistency: Package managers ensure software is installed and removed cleanly, reducing the chance of conflicts or broken systems.

In summary, a package manager is an essential tool in Linux for efficiently managing software installation, updates, and removal, taking care of complexities like dependencies and version control.

Tasks

  1. You have to install docker and jenkins in your system from your terminal using package managers Ubuntu and CentOS.

Installing Docker on Ubuntu

Step 1: Update the Package List

Before installing any software, always update the local package index to ensure you're working with the latest versions.

sudo apt update

Step 2: Install prerequisites

sudo apt install apt-transport-https ca-certificates curl software-properties-common -y

Step 3: Add Docker’s official GPG key and repository

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Step 4: Install Docker

sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io -y

Step 5: Start Docker and enable it to run at startup

sudo systemctl start docker
sudo systemctl enable docker

Step 6: Verify Docker installation

docker --version

2. Install Jenkins

Step 1: Add Jenkins repository and GPG key

sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
  https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]" \
  https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins

Step 2: Installation of java required for Jenkins

sudo apt update
sudo apt install fontconfig openjdk-17-jre
java -version
openjdk version "17.0.8" 2023-07-18
OpenJDK Runtime Environment (build 17.0.8+7-Debian-1deb12u1)
OpenJDK 64-Bit Server VM (build 17.0.8+7-Debian-1deb12u1, mixed mode, sharing)

Step 3: Start Jenkins and enable it at startup

sudo systemctl start jenkins
sudo systemctl enable jenkins

Step 4: Verify Jenkins installation

systemctl status jenkins

After this, you can access Jenkins by visiting http://localhost:8080 in your web browser. The initial password can be found using this command:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Installing Docker on CentOS

Step 1: Update Your System

Ensure your system’s packages are up to date:

sudo yum update

Step 2: Install Required Packages

sudo yum install -y yum-utils device-mapper-persistent-data lvm2

Step 3: Add Docker’s Repository

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

Step 4: Install Docker

sudo yum install docker-ce docker-ce-cli containerd.io -y

Step 5: Start and Enable Docker

sudo systemctl start docker
sudo systemctl enable docker

Step 6: Verify Installation

docker --version

Installing Jenkins on CentOS

Step 1: Add Jenkins Repository

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

Step 2: Install Jenkins

sudo yum install jenkins -y

Step 3: Start and Enable Jenkins

sudo systemctl start jenkins
sudo systemctl enable jenkins

Step 4: Access Jenkins

Jenkins runs on port 8080 by default. Open http://localhost:8080 in your browser. Retrieve the initial admin password:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Conclusion

By following these steps, you can easily install Docker and Jenkins on both Ubuntu and CentOS using their respective package managers. With Docker’s containerization power and Jenkins’ automation capabilities, you can easily streamline your development and deployment workflows with ease!

Systemctl and Systemd

1. systemd:

  • Definition: systemd is a system and service manager for Linux operating systems. It is responsible for initializing and managing services, processes, and system components during boot time and while the system is running.

  • Function: It manages the entire system, handles dependencies between services, logs events, and ensures proper boot/shutdown sequences. It also oversees resource allocation using control groups (cgroups), and can start services on demand using socket and device activation.

  • Core Component: systemd replaces older init systems like SysVinit or Upstart and is now the default init system in most modern Linux distributions (e.g., Ubuntu, Fedora, CentOS).

  • Scope: systemd is the overarching framework that controls various aspects of the Linux system like:

    • Service management.

    • Device management.

    • Mount points.

    • System logging through journald.

In Short: systemd is the overall system manager that controls all system processes and services.

2. systemctl:

  • Definition: systemctl is a command-line utility that provides an interface to interact with and control systemd. It's the main tool used to start, stop, enable, disable, and manage system services.

  • Function: It allows users and system administrators to manage services, check their statuses, enable services to run at boot, restart services, and even analyze system logs.

  • Examples:

    • Start a service: systemctl start nginx

    • Check service status: systemctl status nginx

    • Enable a service at boot: systemctl enable nginx

    • Stop a service: systemctl stop nginx

In Short: systemctl is the command-line tool that allows you to control and manage the services and processes managed by systemd.

Task 1: Check Docker Service Status

Once Docker is installed, you can check the status of the Docker service using systemctl or service. Here's how to do it:

Command to check Docker service status:

service docker status

OR


systemctl status docker

If Docker is running, the output should indicate "active (running)" status.

Task 2: Manage Jenkins Service

To manage Jenkins, you will use the systemctl or service commands to control the service.

Step 1: Check Jenkins status before stopping

systemctl status jenkins

Step 2: Stop Jenkins service

sudo systemctl stop jenkins

Step 3: Check Jenkins status after stopping

systemctl status jenkins

For the task, you can take screenshots of the terminal showing the Jenkins status before and after stopping the service.

Task 3: Read About Systemctl vs. Service

The systemctl and service commands in Linux are both used to manage system services, but they interact with different service management frameworks, and there are key differences between the two:

1. Underlying System:

  • systemctl: This command is part of systemd, a newer system and service manager used by most modern Linux distributions (e.g., Ubuntu 16.04+, CentOS 7+).

  • service: This command is part of the older SysVinit system, which was the traditional way to manage services before systemd. It’s still available on many distributions for backward compatibility.

2. Command Structure:

  • systemctl: The syntax is more modern and versatile. You can control a service’s lifecycle (start, stop, restart) as well as examine its status and logs. Example:

      systemctl start <service>
      systemctl stop <service>
      systemctl restart <service>
      systemctl status <service>
    
  • service: The syntax is simpler and works only with start, stop, and restart operations. Example:

      service <service> start
      service <service> stop
      service <service> restart
      service <service> status
    

3. Capabilities and Features:

  • systemctl:

    • Can handle more than just services; it can manage sockets, timers, mount points, etc.

    • Can restart services automatically if they fail.

    • Offers integration with journalctl to view logs, making troubleshooting easier.

    • Supports dependencies and parallel service starts, which enhances boot speed.

  • service:

    • Primarily focused on service start, stop, restart, and status.

    • It doesn't provide the richer control and logging features that systemctl offers.

Task 4: Automate Service Management

  • You can write a simple shell script to automate the starting and stopping of Docker and Jenkins services.

    Shell Script: manage_services.sh

      #!/bin/bash
    
      # Function to start Docker and Jenkins services
      start_services() {
          echo "Starting Docker..."
          sudo systemctl start docker
          echo "Starting Jenkins..."
          sudo systemctl start jenkins
          echo "Services started successfully!"
      }
    
      # Function to stop Docker and Jenkins services
      stop_services() {
          echo "Stopping Docker..."
          sudo systemctl stop docker
          echo "Stopping Jenkins..."
          sudo systemctl stop jenkins
          echo "Services stopped successfully!"
      }
    
      # Check the argument passed to the script
      case "$1" in
          start)
              start_services
              ;;
          stop)
              stop_services
              ;;
          *)
              echo "Usage: $0 {start|stop}"
              exit 1
              ;;
      esac
    
  • How to use the script:

    1. Save the script as manage_services.sh.

    2. Make the script executable:

        chmod +x manage_services.sh
       or 
       chmod 766 manage_services.sh
      
    3. Run the script to start or stop the services:

        ./manage_services.sh start
        ./manage_services.sh stop
      
  • Task 5: Enable and Disable Services

    Using systemctl, you can configure Docker to start automatically at boot and prevent Jenkins from doing so.

    Enable Docker to Start on Boot:

      sudo systemctl enable docker
    

    Disable Jenkins from Starting on Boot:

      sudo systemctl disable jenkins
    

    You can verify if Docker is enabled and Jenkins is disabled by running:

      systemctl is-enabled docker
      systemctl is-enabled jenkinsdTask 6: Analyze Logs Using journalctl
    

    You can use journalctl to analyze logs for Docker and Jenkins. Here's how to retrieve logs:

    View Docker Logs:

      sudo journalctl -u docker
    

    View Jenkins Logs:

      sudo journalctl -u jenkins
    

    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):

       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.

*

More from this blog

DevOps Projects and Blog

69 posts