Understanding Package Manager and Systemctl in Linux

Spoorti ShettySpoorti Shetty
6 min read

In this blog, we’re diving into package managers and systemctl, two essential components of managing Linux systems. These tools allow you to install software and control services efficiently, making them crucial for system administration and DevOps tasks.

What is a Package Manager in Linux?

At its core, a package manager is a tool that helps users manage software on their system. Whether it’s installing new programs, removing old ones, or keeping everything up-to-date, the package manager handles the entire process. It can be used as a graphical application or through the command line, depending on your preference and the distribution you’re using.

What is a Package?

Before we move forward, let’s break down what a package is. A package is essentially a compressed archive that contains everything needed for a specific piece of software. This includes:

  • The binary executable (the actual program).

  • Configuration files to adjust how it behaves.

  • Dependencies or other software the package needs to function properly.

Different Kinds of Package Managers

Different Linux distributions use different package managers. Here are some popular ones:

  • RPM-based systems (e.g., Red Hat, CentOS): Use package managers like Yum and DNF.

  • Debian-based systems (e.g., Ubuntu, Debian): Use apt-get and aptitude.

Tasks: Install Docker and Jenkins

To practice package management, we’ll install Docker and Jenkins using the package manager on both Ubuntu and CentOS.

Installing Docker on Ubuntu

  1. Update the package index:

     sudo apt-get update
    
  2. Install Docker’s dependencies:

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

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

     sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
    
  5. Install Docker:

     sudo apt-get update
     sudo apt-get install docker-ce
    
  6. Grant user permissions to use Docker:

    Initially, the user may not have permission to use Docker because the user is not assigned to the Docker group by default. To check group permissions:

     cat /etc/group
    

    Now to add the user to Docker group, use the command:

     sudo usermod -aG docker $USER
    

    To refresh group membership without logging out, you can use:

     newgrp docker
    

    Alternatively, you can log out and log back in to apply the group changes.

Installing Jenkins on Ubuntu

To install Jenkins, you can start by visiting the official Jenkins website at pkg.jenkins.io and selecting the appropriate OS and distribution for your machine.

Since we are installing Jenkins on an Ubuntu machine, we will choose the Debian-stable version.

You can follow the steps provided there or continue with the steps below.

  1. Add Jenkins GPG key:

     sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
         https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
    
  2. Add the Jenkins repository:

     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
    
  3. Update your local package index and Install Jenkins along with OpenJDK 17:

     sudo apt-get update
     sudo apt-get install fontconfig openjdk-17-jre
     sudo apt-get install jenkins
    

Installing Docker and Jenkins on CentOS

For CentOS, you will use the yum or dnf package manager.

Installing Docker on CentOS

  1. Update the package index:

     sudo yum update
    
  2. Install Docker:

     sudo yum install docker
    

Installing Jenkins on CentOS

  1. Update the package index:

     sudo yum update
    
  2. Install Jenkins:

     sudo yum install jenkins
    

Systemctl and Systemd

Now that we have Docker and Jenkins installed, let’s explore systemctl, which is used to control services in systems running systemd. The systemd system and service manager provide an efficient way to manage system boot and services.

Checking Docker Service Status

After installing Docker, you can check its status using systemctl:

sudo systemctl status docker

Sample output:

ubuntu@ip-172-31-23-226:~$ sudo systemctl status docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; pres>
     Active: active (running) since Sat 2024-10-19 15:59:36 UTC; 7min ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 9831 (dockerd)
      Tasks: 8
     Memory: 29.8M (peak: 30.5M)
        CPU: 242ms
     CGroup: /system.slice/docker.service
             └─9831 /usr/bin/dockerd -H fd:// --containerd=/run/containerd>

You should see the current state of the Docker service, whether it's active, inactive, or failed.

Managing Jenkins Service

To manage the Jenkins service, you can start with checking the current state/status using:

sudo systemctl status jenkins

Sample output:

ubuntu@ip-172-31-23-226:~$ sudo systemctl status jenkins
● jenkins.service - Jenkins Continuous Integration Server
     Loaded: loaded (/usr/lib/systemd/system/jenkins.service; enabled; pre>
     Active: active (running) since Sat 2024-10-19 16:54:51 UTC; 9h ago
   Main PID: 17348 (java)
      Tasks: 40 (limit: 1130)
     Memory: 313.0M (peak: 325.4M)
        CPU: 59.354s
     CGroup: /system.slice/jenkins.service
             └─17348 /usr/bin/java -Djava.awt.headless=true -jar /usr/shar>

To stop the service:

sudo systemctl stop jenkins

To restart the service:

sudo systemctl restart jenkins

Use the systemctl status jenkins command to verify the current state again after these commands.

Automating Service Management

Now, let’s create a script to automate starting and stopping the Docker and Jenkins services. This can be useful in scenarios where you want to manage services without manually entering commands each time.

#!/bin/bash

echo "Managing Docker and Jenkins services."

echo "Enter 1 to start services, 2 to stop services:"
read choice

if [ $choice -eq 1 ]; then
    sudo systemctl start docker
    sudo systemctl start jenkins
    echo "Docker and Jenkins started."
elif [ $choice -eq 2 ]; then
    sudo systemctl stop docker
    sudo systemctl stop jenkins
    echo "Docker and Jenkins stopped."
else
    echo "Invalid choice!"
fi

This script takes user input to either start or stop the Docker and Jenkins services automatically.

Don’t forget to make the file executable before running the script:

chmod +x managing_services.sh

Sample output:

ubuntu@ip-172-31-23-226:~/day7$ ./managing_services.sh
Managing Docker and Jenkins services.
Enter 1 to start services, 2 to stop services:
1
Docker and Jenkins started.

Enabling and Disabling Services

Use systemctl to control whether a service starts automatically on boot. This is especially useful if you want Docker to always start on boot, but not Jenkins.

  • To enable Docker to start on boot:

      sudo systemctl enable docker
    
  • To disable Jenkins from starting on boot:

      sudo systemctl disable jenkins
    

Analyzing Logs

Lastly, we’ll use journalctl to analyze the logs of Docker and Jenkins services. This helps identify any issues or events related to the services.

sudo journalctl -u docker
sudo journalctl -u jenkins

The output will show logs that provide insights into service operations, errors, and restarts.

Example output:

ubuntu@ip-172-31-23-226:~$ sudo journalctl -u jenkins
Oct 20 03:00:52 ip-172-31-23-226 systemd[1]: jenkins.service: Deactivated successfully.
Oct 20 03:00:52 ip-172-31-23-226 systemd[1]: Stopped jenkins.service - Jenkins Continuous Integration Server.
Oct 20 03:00:52 ip-172-31-23-226 systemd[1]: jenkins.service: Consumed 10.333s CPU time, 277.9M memory peak, 0B memory swap peak.
Oct 20 03:03:21 ip-172-31-23-226 systemd[1]: Starting jenkins.service - Jenkins Continuous Integration Server...
Oct 20 03:03:22 ip-172-31-23-226 jenkins[19454]: Running from: /usr/share/java/jenkins.war
Oct 20 03:03:22 ip-172-31-23-226 jenkins[19454]: 2024-10-20 03:03:22.721+0000 [id=1]        INFO        winstone.Logger#logInternal: Beginning extraction from wa>
Oct 20 03:03:22 ip-172-31-23-226 jenkins[19454]: 2024-10-20 03:03:22.891+0000 [id=1]        WARNING        o.e.j.s.handler.ContextHandler#setContextPath: Empty c>
Oct 20 03:03:23 ip-172-31-23-226 jenkins[19454]: 2024-10-20 03:03:23.044+0000 [id=1]        INFO        org.eclipse.jetty.server.Server#doStart: jetty-10.0.24; b>

Conclusion

Understanding package managers and systemctl is crucial for efficient system management in Linux. By mastering these tools, you can install, manage, and automate services like Docker and Jenkins effortlessly. Through systemctl and journalctl, you have control and insight into your system’s services, helping you to run your infrastructure smoothly.

0
Subscribe to my newsletter

Read articles from Spoorti Shetty directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Spoorti Shetty
Spoorti Shetty