Understanding Package Manager and Systemctl
What is a Package Manager?
Imagine you’ve just bought a new gadget. To make it work, you need to install various components, follow a setup guide, and sometimes, even configure settings. A package manager does all this heavy lifting for software on your Linux system. In simpler terms, a package manager is a tool that automates the process of installing, removing, upgrading, configuring, and managing software packages. It’s like having a personal assistant for your software needs.
Key Functions of a Package Manager:
Installation: Install new software packages with a single command.
Removal: Uninstall software you no longer need without leaving behind any clutter.
Upgrade: Keep your software up to date effortlessly.
Configuration: Handle initial setup and ongoing configuration of software.
Dependency Management: Automatically manage and resolve software dependencies.
What is a Package?
To truly appreciate package managers, you need to understand what a package is. A package can be thought of as a bundle containing everything needed to install a piece of software. This could be a graphical application, a command-line tool, or even a library that other software programs rely on.
Components of a Package:
Binary Executable: The compiled program ready to run on your system.
Configuration Files: Files that help customize the software for your system.
Dependencies: Information about other packages needed for the software to function correctly.
Packages are often distributed in a compressed format to save space and bandwidth. When you install a package, the package manager unpacks it and places the files in the appropriate directories.
Different Kinds of Package Managers
Package managers vary based on the packaging system they support, and sometimes, there are multiple package managers for the same packaging system. Here’s a look at some common ones:
RPM (Red Hat Package Manager)
Yum (Yellowdog Updater Modified): Used in older Red Hat-based distributions like CentOS and Fedora. It's known for its simplicity and ease of use.
DNF (Dandified Yum): The modern replacement for Yum, offering better performance and improved dependency resolution.
DEB (Debian Package)
apt-get: A command-line tool used in Debian-based distributions like Ubuntu. It's powerful and widely used.
aptitude: Offers a text-based interface on top of
apt-get
, providing additional features and a more user-friendly experience.dpkg: The base tool for handling
.deb
packages, often used under the hood byapt-get
andaptitude
.
Pacman (Package Manager for Arch Linux)
- pacman: The package manager for Arch Linux, known for its speed and simplicity. It’s a favorite among Arch users for its efficiency.
Zypper (SUSE Linux)
- zypper: The command-line interface for the ZYpp package management engine, used in openSUSE and SUSE Linux Enterprise. It's robust and versatile.
Portage (Gentoo Linux)
- emerge: The command-line tool for Gentoo Linux, known for its flexibility and the ability to compile software from source. It’s highly customizable.
How Package Managers Work
At the heart of a package manager is a database of software available from one or more repositories. Repositories are servers that host software packages and their metadata. When you install a package, the package manager communicates with these repositories to download and install the required files. Here's a typical workflow:
Workflow of a Package Manager:
Update Package List: The package manager fetches the latest list of available packages from the repositories.
Install Package: You issue a command to install a package. The package manager checks for dependencies and downloads the necessary files.
Resolve Dependencies: The package manager ensures all dependencies are met, downloading and installing any additional packages required.
Configure Software: During installation, you might be prompted to configure certain aspects of the software.
Complete Installation: The package is installed and ready to use.
Example: Using apt-get
on Debian-based Systems
Let’s explore some common commands using apt-get
, a popular package manager for Debian-based distributions like Ubuntu:
Update Package List:
sudo apt-get update
This command updates the local package index with the latest changes made in the repositories.
Install a Package:
sudo apt-get install package_name
Replace
package_name
with the name of the software you want to install.Remove a Package:
sudo apt-get remove package_name
This command removes the specified package from your system.
Upgrade All Packages:
sudo apt-get upgrade
This upgrades all the installed packages to their latest versions.
Why Package Managers Matter
Package managers are the backbone of the Linux software ecosystem. They not only simplify the process of software management but also ensure that your system remains stable and secure by handling dependencies and updates efficiently.
Imagine manually downloading software, tracking dependencies, and configuring everything yourself—it would be a nightmare! Package managers take away that complexity, allowing you to focus on what matters most: using your system productively.
Task1: Install Docker and Jenkins:
- Install Docker and Jenkins on your system from your terminal using package managers.
sudo apt install docker.io
- While installing Jenkins I got below error and to resolve this issue please follow below steps:
It seems like Jenkins is not available in the default repositories of your distribution.
Before adding new repositories, it's always a good idea to update your system to ensure you have the latest package lists.
sudo apt-get update
sudo apt-get upgrade
Jenkins requires Java to run. You can install OpenJDK (an open-source implementation of Java) with the following command.
sudo apt-get install openjdk-11-jdk -y
Verify the Java installation:
java -version
You should see output similar to:
openjdk version "11.0.23" 2024-04-16
Add the Jenkins Debian repository to your system's software repository list.
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null
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
Update your package list to include the Jenkins repository.
sudo apt-get update
Now you can install Jenkins with the following command.
sudo apt-get install jenkins
Start the Jenkins service and enable it to start on boot.
sudo systemctl start jenkins
sudo systemctl enable jenkins
Jenkins runs on port 8080 by default. Open your web browser and navigate to
http://your_server_ip_or_domain:8080
Task2: Check Docker Service Status:
- Check the status of the Docker service on your system (ensure you have completed the installation tasks above).
sudo systemctl status docker
Task3: Manage Jenkins Service:
- Stop the Jenkins service and post before and after screenshots.
sudo systemctl status jenkins
sudo systemctl stop jenkins
Managing services in Linux is a fundamental task, and two of the most common commands for this purpose are systemctl
and service
. Both are used to control and manage system services, but they have differences in usage, functionality, and underlying mechanisms. In this blog post, we'll explore these differences, understand when to use each command, and provide practical examples to illustrate their usage.
Overview of systemctl
and service
systemctl
systemctl
is the command used to interact with the systemd system and service manager. Systemd is a suite of tools that provides a range of functionalities for managing Linux systems, including service management, system initialization, and more. systemctl
is the primary tool to manage systemd services.
service
service
is a command used to interact with the init system, specifically SysVinit and Upstart, which were common before systemd became widely adopted. The service
command provides a way to start, stop, and check the status of system services, and it abstracts the underlying init system to provide a consistent interface.
Key Differences
1. Underlying System
systemctl: Operates with systemd, which is now the default init system for many major Linux distributions, including Ubuntu, Fedora, and Debian.
service: Typically works with older init systems like SysVinit and Upstart. In systemd-based systems,
service
is often implemented as a compatibility layer.
2. Command Syntax
systemctl: Uses a more modern and versatile syntax. For example:
systemctl status docker systemctl start docker systemctl stop docker systemctl restart docker systemctl enable docker systemctl disable docker
service: Uses a simpler and more traditional syntax. For example:
service docker status service docker start service docker stop service docker restart
3. Capabilities
systemctl: Provides extensive control over systemd units (services, sockets, timers, etc.). It can manage dependencies, isolate services, and change service properties on-the-fly. It offers advanced features like:
Reloading service configurations without restarting.
Masking/unmasking services.
Listing dependencies and viewing logs.
service: Primarily focused on starting, stopping, and checking the status of services. It lacks the advanced capabilities and granular control provided by
systemctl
.
4. Service Management
systemctl: Handles unit files, which are configuration files that describe services, sockets, timers, and other entities managed by systemd. It can manage various types of units beyond just services.
service: Limited to managing SysVinit or Upstart scripts located in
/etc/init.d/
.
Practical Examples
Checking Service Status
Using
systemctl
:systemctl status docker
Output:
docker.service - Docker Application Container Engine Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2024-07-07 10:35:21 UTC; 1 day 2h ago Docs: https://docs.docker.com Main PID: 1234 (dockerd) Tasks: 29 Memory: 53.1M CGroup: /system.slice/docker.service └─1234 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Using
service
:service docker status
Output:
docker.service - Docker Application Container Engine Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2024-07-07 10:35:21 UTC; 1 day 2h ago Docs: https://docs.docker.com Main PID: 1234 (dockerd) Tasks: 29 Memory: 53.1M CGroup: /system.slice/docker.service └─1234 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Starting a Service
Using
systemctl
:systemctl start docker
Using
service
:service docker start
Stopping a Service
Using
systemctl
:systemctl stop docker
Using
service
:service docker stop
Enabling a Service to Start on Boot
Using
systemctl
:systemctl enable docker
Using
service
: Note: Theservice
command itself does not handle enabling services at boot. This is typically managed by updating runlevel symlinks directly or usingchkconfig
orupdate-rc.d
.
Subscribe to my newsletter
Read articles from Rajat Chauhan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Rajat Chauhan
Rajat Chauhan
Rajat Chauhan is a skilled Devops Engineer, having experience in automating, configuring, deploying releasing and monitoring the applications on cloud environment. • Good experience in areas of DevOps, CI/CD Pipeline, Build and Release management, Hashicorp Terraform, Containerization, AWS, and Linux/Unix Administration. • As a DevOps Engineer, my objective is to strengthen the company’s applications and system features, configure servers and maintain networks to reinforce the company’s technical performance. • Ensure that environment is performing at its optimum level, manage system backups and provide infrastructure support. • Experience working on various DevOps technologies/ tools like GIT, GitHub Actions, Gitlab, Terraform, Ansible, Docker, Kubernetes, Helm, Jenkins, Prometheus and Grafana, and AWS EKS, DevOps, Jenkins. • Positive attitude, strong work ethic, and ability to work in a highly collaborative team environment. • Self-starter, Fast learner, and a Team player with strong interpersonal skills • Developed shell scripts (Bash) for automating day-to-day maintenance tasks on top of that have good python scripting skills. • Proficient in communication and project management with good experience in resolving issues.