Day 7 - Understanding package manager and systemctl

Day 7 - Understanding package manager and systemctl

Β·

8 min read

Introduction

In this blog we'll delve into packages and package managers, understanding the core concepts, their types and their usage. Also we'll dive into knowing the concepts of SYSTEMD and SYSTEMCTL. We'll end this blog with some scenario based hands on practice that will not only involve concepts of package managers and Systemd, but also concepts learned in the previous blogs. So let's begin.

Package Manager

What is a package?

A package is a collection of resources, files, and dependencies, bundled up together, which helps in installation, configuration, and efficient operation of a particular software, application, or system. Every software comes with their own set of packages which aid in the proper functioning of the software.

In the context of Linux, a "package" typically refers to a compressed archive file that contains software, along with metadata and instructions for its installation and management. Packages are a standardized way of packaging, distributing, and installing software to ensure consistency and ease of management across different Linux distributions.

Contents of a Package:

The package files usually come in an archive and within the archive, it will contain two important items. Firstly, the package file consists of the software binary or code that’s to be installed. Besides that, the package file contains one or more metadata files that specify different information about the software packages.

Types of Packages:

The type of packages are determined based on their file format. These are also called Distro or Linux Distributions. Here are the few most popular types of package file formats in Linux:

  1. " .deb " : This refers to Debian package file format. It is used in Debian based Operating systems. The Most popular of them all is Ubuntu.

  2. " .rpm " : This refers to Red Hat package file format. It is used in Red Hat based Operating systems. The most popular of them all are Fedora and CentOS.

  3. Tar Archive ".tar" : It stands for Tape Archive. It is used to for creating compressed archive files. It achieves so by copying a bunch of files and compressing it into a combined archive file. The archive files have a .tar extension and are called Tarballs.

What is a package manager?

A package manager is a automated program/tool that helps in installing, configuring, updating, maintaining and removing software packages and their dependencies. It acts as a helper, where it carries out all the heavy tasks with least manual intervention and errors.

In Linux, the package managers are specific to the distro of Linux.

Types of Package Managers:

  1. " dpkg " :

    This is a low-level package manager. It is used in Debian and Debian based Linux distributions. It is responsible for the installation, removal, and management of individual Debian packages.

     sudo dpkg -i package_name.deb  #install
     sudo dpkg -r package_name.deb  #remove
    
  2. "APT" (Advanced Package Tool) :

    This is a high-level package management for Debian and Debian based linux distributions. It is an advanced tool that interacts with "dpkg" to provide a more user friendly package management. It handles package dependencies, retrieves packages from repositories, and manages updates.

     sudo apt install package_name #install
     sudo apt remove package_name  #remove
     sudo apt update               #update the system
    
  3. "YUM" (Yellowdog Updater Modifier):

    It is the package managers used for Red-Hat and Red-Hat based distributions.

     sudo yum install package_name
     sudo yum remove package_name
    
  4. " DNF" (Dandified Yum):

    It is the package manager used in Fedora, RHEL (Red Hat Enterprise Linux), and other Red Hat-based Linux distributions. DNF is a successor to Yum and brings improvements in terms of performance, dependency resolution, and additional features.

     sudo dnf install package_name
     sudo dnf remove package_name
    

Working of a Package manager

Installing:

  • A local database is maintained by the package manager which stores all the information about installed packages, their versions and dependencies. This helps the package manager track the software installed on the system.

  • When a package is requested to be installed, then firstly the package manager for dependencies that the package relies on.

  • The next step, it downloads the package from the configured repositories to make sure it delivers the requested version.

  • The package manager installs the software by extracting the package contents to the appropriate directories on the system. It may also execute pre- and post-installation scripts to configure the software or perform other necessary tasks.

Removing:

  • When a user requests to remove a package, the package manager checks for dependencies that are no longer required by other installed packages.

  • A pre-removal script is present in most of the package manager. These scripts perform tasks such as stopping services, backing up configuration files, or notifying the user about potential issues.

  • The package manager removes the files associated with the package i.e. binaries, libraries, configuration files etc. from the system.

  • It then goes through a dependency check which where it checks for dependencies that are no longer needed. If found then they are also removed.

  • Finally it performs a verification to ensure that the package and its dependencies are successfully deleted.

systemctl and systemd

systemd:

It is a system and service manager for Linux operating systems which is designed to manage the startup, control, and maintenance of various aspects of the system, including processes, services, and devices. The services it provides are

  • Startup Management

  • Service Management

  • Dependency Management

  • Logging and Journaling

  • User session Management

  • Control Groups

  • Target Units

  • Socket activation

  • System and System Unit files.

To read in depth, Click Here.

systemctl:

systemctl is a command-line utility in Linux that is used to control and manage the systemd system and service manager. It provides a central point for interacting with the system and its services, allowing users and administrators to start, stop, restart, enable, disable, and obtain information about services and the overall system state.

Some Commands:

  1. Start a Service:

     sudo systemctl start service_name
    
    • Starts a specific service.
  2. Stop a Service:

     sudo systemctl stop service_name
    
    • Stops a specific service.
  3. Restart a Service:

     sudo systemctl restart service_name
    
    • Restarts a specific service.
  4. Enable a Service:

     sudo systemctl enable service_name
    
    • Enables a service to start on boot.
  5. Disable a Service:

     sudo systemctl disable service_name
    
    • Disables a service from starting on boot.
  6. Check Service Status:

     systemctl status service_name
    
    • Displays detailed information about the status of a service.
  7. List All Units (Services, Sockets, Targets, etc.):

     systemctl list-units
    
    • Lists all active units in the current system.
  8. List Running Services:

     systemctl list-units --type=service --state=running
    
    • Lists only the running services.
  9. Show Unit Properties:

     systemctl show service_name
    
    • Displays properties and settings of a specific service.
  10. View Journal Logs:

    journalctl
    
    • Views the system's journal logs.
  11. Reload systemd:

    sudo systemctl daemon-reload
    

Scenario based Practice:

Scenario:

Imagine you are tasked with setting up Docker and Jenkins on your system using package managers and checking the status of the Docker service. Once completed, you need to manipulate the Jenkins service by stopping it and providing before-and-after screenshots. Additionally, you are required to familiarize yourself with the commands systemctl and service.

Your tasks are as follows:

  1. Install Docker and Jenkins on your system using the appropriate package managers.

  2. Check the status of the Docker service to ensure successful installation.

  3. Stop the Jenkins service and capture screenshots of the status before and after the manipulation.

  4. Research and explain the differences between the commands systemctl and service.

Please provide step-by-step details for each task, and include any commands used in the process.

Solution:

Task 1: Install Docker and Jenkins

Install Docker:

# Update package list
sudo apt update

# Install required dependencies
sudo apt install -y docker.io

# Add your user to the docker group to run Docker without sudo
sudo usermod -aG docker $USER

# Verify Docker installation
docker --version

To access docker, we need to give permission of Docker Daemon using chmod command.

Install Jenkins:

First install Java JDK

and then Copy These commands and run on your terminal to get it installed.

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

To Know more, Click Here.

Java Installation :

Task 2: Check Docker Service Status

# Check Docker service status
sudo systemctl status docker

Task 3: Stop Jenkins Service and Capture Screenshots

Before Stopping Jenkins:

  • Capture a screenshot or note the current status of the Jenkins service.

Stop Jenkins Service:

sudo systemctl stop jenkins

After Stopping Jenkins:

  • Capture a screenshot or note the status of the Jenkins service after stopping it.

Task 4: Research systemctl vs. service

systemctl and service are commands used to interact with the systemd system and service manager on Linux.

  • systemctl:

    • It is a command-line tool to examine and control the state of the system and service manager.

    • It can start, stop, restart, enable, or disable services.

    • Provides detailed information about services using systemctl status.

  • service:

    • It is a simple command for interacting with the init system.

    • It is backward-compatible with the older init.d scripts.

    • It is less feature-rich compared to systemctl and may not support all systemd features.

In summary, systemctl is more powerful and comprehensive, while service provides a simpler interface, especially for older init.d scripts. It's recommended to use systemctl for systemd-based systems.

Conclusion

This blog covers the basics of Linux packages, package managers (dpkg, APT, YUM, DNF), and systemd/systemctl for service management. It concludes with a hands-on scenario deploying Docker and Jenkins. A concise guide to key elements in Linux system administration. I hope this blog added some value to your learning.

Ending this blog with a quote

The most important thing is to be able to think what you want, not to say what you want. - Paul Graham

Happy Learning.

Did you find this article valuable?

Support DevOps with Aakash by becoming a sponsor. Any amount is appreciated!

Β