Day 7 : Understanding Package Manager and Systemctl
Today we will learn about Package managers and System services, and also have tasks for this topic.
What is a Package Manager in Linux?
Package Manager is a tool that can Install, remove, update or manage Packages on Operating system. It also brings dependencies for that package if needed.
What is a Package?
Package is a bundle or a bunch of files, it contains everything that need to run a Software like it’s code, configuration files, libraries, etc. When you download any software, it gives an archive file and when you extract it you see that different files are present in it, that archive file is nothing but a package.
Different Kinds of Package Managers
There are so many types of Package Manager
System package managers :
These are in-built tools to manage packages, likeAPT : For Ubuntu and Debian-based distributions.
Eg.
sudo apt install <package-name>
DNF/YUM : For Red Hat-based distributions like Fedora, CentOS
Eg.
sudo dnf install <package-name>
sudo yum install <package-name>
Pacman : For Arch Linux
Eg.
sudo pacman -S <package-name>
Universal Package Managers : These managers are for all distributions like
Snap : Made by Ubuntu but work on all distributions
Eg.
sudo snap install <package-name>
Flatpak : Work on all distributions
Eg.
flatpak install <remote-name> <package-name>
Manual Install Tools : These are used when packages are not available in repositories
DPKG : For .deb extension files
Eg.
sudo dpkg -i <package-name.deb>
RPM : For .rpm files
Eg. sudo rpm -ivh <package-name.rpm>
Task 1 : Install Docker and Jenkins
- Install Docker and Jenkins on your system from your terminal using package managers.
- Write a small blog or article on how to install these tools using package managers on Ubuntu and CentOS.
Solution :
For Ubuntu
Installing Docker
First update your system, updating your system before any installation is good practice
sudo apt update
sudo apt upgrade -y
Install required dependencies, These packages allow system to download Docker securely and manage repositories
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
Add Docker’s GPG key for secure package verification
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Add Docker’s Repository to your system’s package manager for stable updates
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
And then finally install Docker after again one time updating your system
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io
These will install :
docker-ce
: The main Docker engine.docker-ce-cli
: Command-line interface tools for Docker.containerd.io
: A container runtime.
Check if downloaded successfully
docker --version
it should give something like this Docker version 27.3.1, build ce12230
means it is installed.
Installing Jenkins
Install java, because jenkins need java to run
sudo apt install -y openjdk-17-jdk
Add jenkins repository key, this will download and stores GPG key
curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null
Add jenkins repository
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
And finally install jenkins after updating system one more time
sudo apt update
sudo apt install -y jenkins
Start and enable jenkins service
sudo systemctl start jenkins && sudo systemctl enable jenkins
Now if you are on your local machine then go to browser and type http://localhost:8080
Steps if you are on instance (optional-case)
And if you are on your AWS instance even if on your local machine but connected to instance then go to your Instances → Instance ID → Security → Security groups → Edit inbound rules → Add rule → In port range type 8080
and select Source type as Anywhere-IPv4 → Save rules and then again go to Instances → Insatnce ID → Public IPv4 address (copy that address) and paste it in between below line
Eg. http://15.207.98.161:8080
http://<Public IPv4 address>:8080
After searching this, you’ll be able to see Jenkins web interface on your browser just like this …
as you can see it is asking for a Administrator password, for this password copy that red highlighted path Eg. /var/lib/jenkins/secrets/initialAdminPassword
For CentOS
Installing Docker
First, update your system. Updating your system before any installation is good practice.
sudo yum update -y
Add the Docker repository
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Install Docker
sudo yum install -y docker-ce docker-ce-cli containerd.io
Start and enable Docker
sudo systemctl start docker
sudo systemctl enable docker
Verify Docker installation
docker --version
Installing Jenkins
Add the Jenkins repository
sudo yum install -y wget
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.key
Install Java (required for Jenkins)
sudo yum install -y java-11-openjdk
Install Jenkins
sudo yum install -y jenkins
Start and enable Jenkins
sudo systemctl start jenkins
sudo systemctl enable jenkins
if you are on your local machine then go to browser and type http://localhost:8080
, you will able to see the Jenkins interface like above image, and if you are connected with AWS EC2 instance, read above yellow highlighted para.
What is Systemd?
Systemd is a system manager of Linux that manages boot process and services like to start/stop services, it decides which service should run, and how they are managed
What is Systemctl?
Systemctl is a command line used to control Systemd, it is used to start, stop, restart or enable services.
Example Commands:
Start a service:
sudo systemctl start <service-name>
Stop a service:
sudo systemctl stop <service-name>
Enable a service to start at boot:
sudo systemctl enable <service-name>
Check if a service is running:
sudo systemctl status <service-name>
<service-name> is services like docker, jenkins, nginx, etc.
Task 2 : Check Docker Service Status
- Check the status of the Docker service on your system (ensure you have completed the installation tasks above).
Solution :
sudo systemctl status docker
If your service is running, you’ll be able to see interface like this…
Task 3 : Manage Jenkins Service
- Stop the Jenkins service and post before and after screenshots.
Solution :
If you see, my jenkins service is running.
After we stop jenkins service by command
sudo systemctl stop jenkins
you can see that service is stopped…
Task 4 : Read About Systemctl vs. Service
- Read about the differences between the systemctl
and service
commands. Example: systemctl status docker
vs. service docker status
.
Solution :
Systemctl and Service both commands are used to manage services on Linux.systemctl
is the modern command line tool of Systemd that is used to Start, stop, restart, to check status and much more whileservice
is the old command line tool that use SysVinit or upstart that used to start, stop, restart and to check status only.
Example Commands:
Start a service:
sudo service <service-name> start
Stop a service:
sudo service <service-name> stop
Check status:
sudo service <service-name> status
Restart a service:
sudo service <service-name> restart
<service-name> is services like docker, jenkins, nginx, etc.
Task 5 : Automate Service Management
- Write a script to automate the starting and stopping of Docker and Jenkins services.
Solution :
Script to automate the start or stop Docker and Jenkins simultaneously.
#!/bin/bash
start_service() {
echo "Starting Docker and Jenkins..."
sudo systemctl start docker
sudo systemctl start jenkins
echo "Docker and Jenkins started."
}
stop_service() {
echo "Stopping Docker and Jenkins..."
sudo systemctl stop docker
sudo systemctl stop jenkins
echo "Docker and Jenkins stopped."
}
status_service() {
echo "Docker Status :"
sudo systemctl status docker | head -n 8
echo "Jenkins Status :"
sudo systemctl status jenkins | head -n 8
}
if [ "$1" == "start" ]; then
start_service
elif [ "$1" == "stop" ]; then
stop_service
elif [ "$1" == "status" ]; then
status_service
else
echo "Usage : $0 {start | stop | status}"
fi
First create the script using vim manage-service.sh
, give it the Execute permission sudo chmod +x manage-service.sh
and execute it ./manage-service
. You can see we stopped the service and checked status for it…
Task 6 : Enable and Disable Services
- Use systemctl to enable Docker to start on boot and disable Jenkins from starting on boot.
Solution :
To enable docker to start on boot
sudo systemctl enable docker
To disable jenkins from starting on boot
sudo systemctl disable jenkins
Task 7 : Analyze Logs
- Use journalctl to analyze the logs of the Docker and Jenkins services. Post your findings.
Solution :
Journalctl is also a command line of Systemd which is used to view and analyze logs in Linux. It is useful for monitoring services if they are working or not or if there is any error, warning, etc.
To see logs of Docker
sudo journalctl -u docker
You can see all the logs of Docker like this…
To see logs of Jenkins
sudo jounalctl -u jenkins
You can see all Jenkins logs like this…
Here, we have completed our task. Follow us, keep learning, and continue progressing in your DevOps journey.
Subscribe to my newsletter
Read articles from Saad Asif Mujawar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by