Understanding package manager and systemctl

Understanding package manager and systemctl

What are package managers?

In Linux Package Managers are essentially software applications that help users to: Search, Download, Install, Remove and Update software applications on their computer operating system. These can be either Command Line tools or a complete Graphical User Interface application.

Experienced Linux users will very rarely download software from websites or any other location. The primary reasons for this included aspects such as ease of use, security and the fact that most Linux distributions have a list of sources where users can download free open-source software packages.

These package sources are referred to as repositories. Using a package manager users will automatically download the appropriate, package from a configured repository, install it and complete all the required configuration tasks. There is no need for the user to click through a wizard screen or hunt down configuration settings. If packages version are updated within the package repository, the package manager will update each package to its latest version whenever it is requested by the user to do so.

What is a package?

In Linux a package is a compressed software archive file that contains all the files that comes with a software application that delivers any kind of functionality, this can be any kind of command line utility, GUI application or a software library.

A package may consist of a binary executable, configuration file and other software dependencies. Common types of Linux packages include .deb, .rpm, and .tgz. Linux packages don't usually contain the dependencies necessary to install them, therefore Linux distributions use package managers to automatically read dependencies files and download the packages needed before proceeding with the installation.

The package usually includes a manifest of dependencies that must be satisfied for a software application to execute as expected on any given Linux computer.

Common Package Managers in Linux include:

  • dpkg (Debian Package Manager)

  • APT (Advanced Package Tool)

  • rpm (RedHat Package Manager)

  • yum (Yellowdog Update Modified)

  • dnf (Dandified Yum)

  • Homebrew

  • zypper

  • flatpak

  • pacman

  • snap

  • synaptic

Packages also contain scripts that need to be executed if a package is requested to be removed. Ensuring that everything that is associated with execution of the package is cleared up.

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.

Lets install docker and Jenkins in your system from your terminal using package managers

yum install docker -y   # To install docker

rpm -q docker  #To check software installed or not

To Install Jenkins:

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-2023.key
sudo yum upgrade
# Add required dependencies for the jenkins package
sudo yum install java-11-openjdk
sudo yum install jenkins
sudo systemctl daemon-reload

Systemctl and Systems

The systemctl command is a systemd utility used to manage services, get information about service unit files and service states, and therefore a useful utility to know for managing services on the server while systemd is an array of components for Linux OS.

What is Systemd?

Systemd is an init system used in Linux distributions to bootstrap user space components and manage system processes.

It runs with PID 1 and it is the one responsible for starting the rest of the system.

What is systemctl?

The systemctl command is a command-line utility that interacts with the systemd system and service manager. It is the primary tool used to control and manage systemd services, allowing users to start, stop, enable, disable, and check the status of services.

systemctl [OPTIONS...] COMMAND ...

Managing services.

The init system is responsible for the management of services and daemons at any point the system is running.
The target of most systemd actions is units - these are resources that systemd knows how to manage. They are mostly categorized by the type of resource they represent.

For managing services the units are service units, these have unit files with the suffix .service.

In our examples, we will be using Jenkins service but feel free to use any other application e.g apache2, MySQL, docker etc:

Starting, stopping, restarting, and reloading services.

To start a service, say jenkinks
We write:

$ sudo systemctl start jenkins.service

We can also ignore the .service suffix:

$ sudo systemctl start jenkins

To stop the service we write:

$ sudo systemctl stop jenkins.service

Usually, after changing a configuration for a service we need to restart it, with systemctl, we restart the service as follows:

$ sudo systemctl restart jenkins.service

Instead of stopping and then starting(restarting), we can also reload a service:

$ sudo systemctl reload jenkins.service

We can also combine reloading and restarting, that is if we are not sure about the service functionalities.

$ sudo systemctl reload-or-restart jenkins.service

Service statuses.

If we are not sure if a service has started after trying to start it we check its status as follows:

$ sudo systemctl status jenkins.service

Using systemctl with the is-active argument allows us to check if a unit is active/running.

$ sudo systemctl is-active jenkins.service

The is-enabled argument is used to check if a service starts when the machine boots up:

$ sudo systemctl is-enabled jenkins.service

If we started a service and it fails, we can check this by writing:

$ sudo systemctl is-failed jenkins.service

Enabling and disabling services.

Sometimes we use services frequently so it is convenient to start the service automatically immediately the system boots up.
For this we use enable with systemctl:

For example, to start Jenkins service every time the system boots up we write:

$ sudo systemctl enable jenkins.service

After the command is executed a symbolic link is created in the /lib/systemd/system directory.

To undo the above operation, we use disable:

$ sudo systemctl disable jenkins.service

Thanks for reading my article. Have a nice day.

No alt text provided for this image

For updates follow me on LinkedIn: Swapnil Khairnar


Hashtags:

#90daysofdevops #devops #cloud #aws #awscloud #awscommunity #docker #linux #kubernetes #k8s #ansible #grafana #terraform #github #opensource #90daysofdevops #challenge #learningprogress #freelancer #linkedin #trainwithshubham #devopscommunity #cloudproviders #bash #bashshellscripting #awkward #shellscripting

Did you find this article valuable?

Support Swapnil Khairnar by becoming a sponsor. Any amount is appreciated!