Day 7 Task: Understanding Package Manager and Systemctl

What is a Package Manager in Linux?

In simpler words, a package manager is a tool that allows users to install, remove, upgrade, configure, and manage software packages on an operating system. The package manager can be a graphical application like a software center or a command line tool like apt-get or pacman.

What is a Package?

A package is usually referred to as an application but it could be a GUI application, command line tool, or a software library (required by other software programs). A package is collection of files, including executables, libraries, configuration files, and metadata, bundled together for easy distribution and installation. It contains all the necessary components for a software application to function correctly.

Different Kinds of Package Managers

Package managers differ based on the packaging system but the same packaging system may have more than one package manager.

For example, RPM has Yum and DNF package managers. For DEB, you have apt-get, aptitude command line-based package managers.

The choice of package manager depends on the Linux distribution:

  • Debian Package (.deb):

    • Used by Debian, Ubuntu, and their derivatives.

    • Packages contain the software, dependencies, and metadata in a single file.

    • Examples: dpkg, apt

  • RPM (Red Hat Package Manager):

    • Used by Red Hat, Fedora, CentOS, and other RPM-based distributions.

    • Packages are compressed archives with metadata and dependencies.

    • Examples: rpm, yum, dnf

Based on Functionality and Philosophy

  • Low-Level Package Managers:

    • Handle basic package installation, removal, and querying.

    • Examples: dpkg, rpm

  • High-Level Package Managers:

    • Build on top of low-level package managers to provide advanced features like dependency resolution, package updates, and configuration management.

    • Examples: apt, yum, pacman

  • Source-Based Package Managers:

    • Manage packages from source code, allowing customization and compilation.

    • Examples: portage (Gentoo), emerge (Gentoo)

Other Notable Package Managers

  • Pacman: Used by Arch Linux, focusing on simplicity and efficiency.

  • Zypper: Used by openSUSE, offering advanced features like package patching and dependency resolution.

  • Nix: Functional package manager based on immutable snapshots.

  • Flatpak: Application sandboxing and distribution platform.

  • Snap: Containerized package format for Ubuntu and other distributions.

Key Considerations

  • Package format: Different package managers handle different package formats.

  • Dependency resolution: How the package manager handles dependencies between packages.

  • Repository management: How packages are obtained and updated.

  • User interface: Command-line or graphical tools available.

Common Package Manager Functions

  • Installation: Installs software packages from repositories or local files.

  • Removal: Uninstalls software packages and their dependencies.

  • Updating: Upgrades installed packages to newer versions.

  • Querying: Provides information about installed packages, dependencies, and available packages.

  • Dependency resolution: Automatically handles dependencies between packages.

    1. To check if an RPM package is available, use: rpm -q package_name

    2. To list all installed RPM packages, use: rpm -qa

    3. To see the installed or available packages yum list

    4. To see the list of repo available on the system yum repolist

Example: Installing Docker and Jenkins

To install Docker and Jenkins on a Debian-based system using apt:

sudo apt update
sudo apt install docker.io jenkins

Tasks

  1. Install Docker and Jenkins:
  • Install Docker and Jenkins on your system from your terminal using package managers.

    Step 1 — Installing Docker

    First, update your existing list of packages:

      sudo apt update
    

    Next, install a few prerequisite packages which let apt use packages over HTTPS:

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

    add the GPG key for the official Docker repository to your system:

      curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    

    Add the Docker repository to APT sources:

      sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
    

    This will also update our package database with the Docker packages from the newly added repo.

    Make sure you are about to install from the Docker repo instead of the default Ubuntu repo:

      apt-cache policy docker-ce
    

    You’ll see output like this, although the version number for Docker may be different: Output of apt-cache policy docker-ce

      docker-ce:
        Installed: (none)
        Candidate: 5:19.03.9~3-0~ubuntu-focal
        Version table:
           5:19.03.9~3-0~ubuntu-focal 500
              500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
    

    Notice that docker-ce is not installed, but the candidate for installation is from the Docker repository for Ubuntu 20.04 (focal). Finally, install Docker:

      sudo apt install docker-ce
    

    If you want to avoid typing sudo whenever you run the docker command, add your username to the docker group:

      sudo usermod -aG docker ${USER}
    

    Step-1 Install Java for Jenkins

    Update your system and Install java and after that validate Installation

      sudo apt update
      sudo apt install openjdk-21-jre
      java -version
    

    Step-2 Install Jenkins

    The commands you provided are used to add the Jenkins repository and its GPG key to your system, specifically for Debian-based Linux distributions.

    1. Add Jenkins GPG Key:
    wget -q -O - https://pkg.jenkins.io/debian/jenkins-ci.org.key | sudo apt-key add -
  • wget: This command is used to retrieve content from the specified URL.

  • -q: This option stands for "quiet," which means that wget will not produce any output except for errors.

  • -O -: This option specifies where to write the downloaded content. In this case, it writes to the standard output (-).

  • https://pkg.jenkins.io/debian/jenkins-ci.org.key: This is the URL of the Jenkins GPG key.

2. Add Jenkins Repository to APT Sources:

    echo "deb http://pkg.jenkins.io/debian-stable binary/" | sudo tee -a /etc/apt/sources.list.d/jenkins.list
  • echo "debhttp://pkg.jenkins.io/debian-stablebinary/": This echoes the repository information to the console.

  • | sudo tee -a /etc/apt/sources.list.d/jenkins.list: The tee command is used to write the echoed content to a file. Here, it appends (-a option) the content of the /etc/apt/sources.list.d/jenkins.list file. This file is typically used to store additional APT repositories.

3. Setting up your system to install Jenkins.

    sudo apt-get update
    sudo apt-get install jenkins

Step-3 Start Jenkins

    sudo systemctl enable jenkins        #Enable Jenkins (to start on boot) 
    sudo systemctl start jenkins         #Start Jenkins (immediately)
    sudo systemctl status jenkins        #Check Jenkins Service Status
    sudo service jenkins start           #To start Jenkins

Openyour web browser and enter the following URL in the address bar:

http://localhost:8080

    sudo service jenkins stop           #To stop Jenkins
    sudo service jenkins restart        #To restart Jenkins

2.Write a Blog or Article:

Write a small blog or article on how to install these tools using package managers on Ubuntu and CentOS.

Task

Using Systemctl and Systemd

  1. Setting up your system to install Jenkins:
    sudo apt-get update
    sudo apt-get install jenkins
  1. Starting Jenkins:
    sudo systemctl enable jenkins        # Enable Jenkins to start on boot
    sudo systemctl start jenkins         # Start Jenkins immediately
    sudo systemctl status jenkins        # Check Jenkins service status
    sudo service jenkins start           # Start Jenkins
  1. Accessing Jenkins:

Open your web browser and enter the following URL in the address bar:

    http://localhost:8080
  1. Stopping Jenkins:
    sudo service jenkins stop            # Stop Jenkins
    sudo service jenkins restart         # Restart Jenkins

Systemctl and Systemd

Systemctl is used to examine and control the state of the “systemd” systemand servicemanager. Systemd is a system and service manager for Unix-like operating systems (most distributions, but not all).

Systemd

Systemd is asuite of tools and libraries for managing system resources and processes in Linux-based operatingsystems. It replaced the traditional SysVinit system and offers a more robust and efficient way to manage services, devices, andother system components.

systemctl

systemctl is the command-line tool used to interact with Systemd. It provides a comprehensive interface for controlling and querying the state of systemd units

systemctl vs. service:

  • systemctl is the recommended tool for managing servicesin modern systems.

  • service is an older tool that might be available for compatibility.

Common systemctl Commands

  • systemctl start <service>: Starts a service.

  • systemctl stop <service>: Stops a service.

  • systemctl restart <service>: Restarts a service.

  • systemctl status <service>: Displays the status of a service.

  • systemctl enable <service>: Enables a service to start at boot.

  • systemctldisable <service>: Disables a service from starting at boot.

systemctl list-units: Lists all available units (services, sockets, mounts, etc.).

Task

  1. Check Docker Service Status:

    • Check the status of the Docker service on your system (ensure you have completed the installationtasks above).

  2. Manage Jenkins Service:

    • Stop the Jenkins service and post before and after screenshots.
  3. Read About Systemctl vs.Service:

  • Example: systemctl status docker vs.service docker status.

    Systemctl and service are both commands used to manage services in Linux systems, but they operate within different init systems. Understanding their differences is crucial for effective system administration.

    SysVinit vs. Systemd

    • SysVinit: The traditional initialization system in Linux, using scripts in /etc/init.d to manage services.

    • Systemd: A newer system and service manager that replaced SysVinit in many distributions, offering improved features and efficiency.

systemctl vs. service

  • systemctl:

    • Primarily used in systems with Systemd.

    • Provides a comprehensive interface for managing services, units, and other system components.

    • Offers advanced features like dependency handling, socket activation, and service grouping.

    • Operates on units defined in /lib/systemd/system or /etc/systemd/system.

  • service:

    • Primarily used in systems with SysVinit.

    • Provides basic functionality for starting, stopping, and checking the status of services.

    • Operates on scripts located in /etc/init.d.

    • Limited in features compared to systemctl.

Key Differences

Featuresystemctlservice
Init systemSystemdSysVinit
FunctionalityComprehensive service managementBasic service control
Dependency handlingAdvancedLimited
Configuration files/lib/systemd/system/etc/init.d
Additional featuresSocket activation, timers, mountsLimited
sudo systemctl status dockersudo service docker start

When to Use Which

  • Modern systems with Systemd: Use systemctl for complete control and advanced features.

  • Older systems or compatibility: Use service if Systemd is not available or for basic service management.

Additional Tasks

  1. Automate Service Management:

    • Write a script to automate the starting and stopping of Docker and Jenkins services.

      ```sh ubuntu@ip-172-31-29-217-Khushbu:~/scripts$ cat service.sh #!/bin/bash

read -p "Enter the service name:" service read -p "Action you want to perform start/stop/status:" action

sudo systemctl $action $service

echo "Action performed" ubuntu@ip-172-31-29-217-Khushbu:~/scripts$ ```

  1. Enable and Disable Services:

    • Use systemctl to enable Docker to start on boot and disable Jenkins from starting on boot.

      systemctl enable docker

      systemctl stop jenkins

  2. Analyze Logs:

    • Use journalctl to analyze the logs of the Docker and Jenkins services. Post your findings.

      Docker log:

0
Subscribe to my newsletter

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

Written by

Khushbu Koradiya
Khushbu Koradiya