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.
You’ll often find me using the term ‘package’ in tutorials and articles. To understand a package manager, you must understand what a package is.
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 essentially an archive file containing the binary executable, configuration file, and sometimes information about the dependencies.
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.
Tasks
Install Docker and Jenkins:
- Install Docker and Jenkins on your system from your terminal using package managers.
1. Install Docker
Step 1: Update the system
sudo apt update
Step 2: Install prerequisites
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
Step 3: Add Docker’s official GPG key and repository
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
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
Step 4: Install Docker
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io -y
Step 5: Start Docker and enable it to run at startup
sudo systemctl start docker
sudo systemctl enable docker
Step 6: Verify Docker installation
docker --version
2. Install Jenkins
Step 1: Add Jenkins repository and GPG key
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
Step 2: Installation of java required for Jenkins
sudo apt update
sudo apt install fontconfig openjdk-17-jre
java -version
openjdk version "17.0.8" 2023-07-18
OpenJDK Runtime Environment (build 17.0.8+7-Debian-1deb12u1)
OpenJDK 64-Bit Server VM (build 17.0.8+7-Debian-1deb12u1, mixed mode, sharing)
Step 3: Start Jenkins and enable it at startup
sudo systemctl start jenkins
sudo systemctl enable jenkins
Step 4: Verify Jenkins installation
systemctl status jenkins
After this, you can access Jenkins by visiting http://localhost:8080
in your web browser. The initial password can be found using this command:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
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.
How to Install Docker and Jenkins on Ubuntu and CentOS Using Package Managers
Docker and Jenkins are two powerful tools that developers and system administrators often use for automating application deployment and continuous integration. Docker simplifies containerization, while Jenkins automates build and deployment processes. In this article, we’ll cover how to install both Docker and Jenkins on Ubuntu and CentOS using package managers.
Installing Docker on Ubuntu
Step 1: Update Your System
Before installing Docker, ensure that your package list is up to date:
sudo apt update
Step 2: Install Required Packages
Docker requires some prerequisite packages like curl
and ca-certificates
. Install them using:
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
Step 3: Add Docker’s GPG Key and Repository
We need to add Docker's official GPG key and the Docker APT repository to our system:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
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
Step 4: Install Docker
Update the package index and install Docker:
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io -y
Step 5: Start Docker and Enable it at Boot
sudo systemctl start docker
sudo systemctl enable docker
Step 6: Verify Docker Installation
docker --version
Installing Jenkins on Ubuntu
Step 1: Add Jenkins Repository and GPG Key
Add the Jenkins repository and import its GPG key:
curl -fsSL https://pkg.jenkins.io/debian/jenkins.io.key | sudo tee /usr/share/keyrings/jenkins-archive-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-archive-keyring.asc] https://pkg.jenkins.io/debian binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
Step 2: Update Package List and Install Jenkins
sudo apt update
sudo apt install jenkins -y
Step 3: Start and Enable Jenkins
sudo systemctl start jenkins
sudo systemctl enable jenkins
Step 4: Access Jenkins
Jenkins runs on port 8080. Open http://localhost:8080
in your browser and follow the setup wizard. You can find the initial admin password using:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Installing Docker on CentOS
Step 1: Update Your System
Ensure your system’s packages are up to date:
sudo yum update
Step 2: Install Required Packages
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
Step 3: Add Docker’s Repository
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Step 4: Install Docker
sudo yum install docker-ce docker-ce-cli containerd.io -y
Step 5: Start and Enable Docker
sudo systemctl start docker
sudo systemctl enable docker
Step 6: Verify Installation
docker --version
Installing Jenkins on CentOS
Step 1: Add Jenkins Repository
sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key
Step 2: Install Jenkins
sudo yum install jenkins -y
Step 3: Start and Enable Jenkins
sudo systemctl start jenkins
sudo systemctl enable jenkins
Step 4: Access Jenkins
Jenkins runs on port 8080 by default. Open http://localhost:8080
in your browser. Retrieve the initial admin password:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Conclusion
By following these steps, you can easily install Docker and Jenkins on both Ubuntu and CentOS using their respective package managers. With Docker’s containerization power and Jenkins’ automation capabilities, you can streamline your development and deployment workflows with ease!
Systemctl and Systemd
Systemctl is used to examine and control the state of the “systemd” system and service manager. Systemd is a system and service manager for Unix-like operating systems (most distributions, but not all).
Task 1: Check Docker Service Status
Once Docker is installed, you can check the status of the Docker service using systemctl
or service
. Here's how to do it:
Command to check Docker service status:
systemctl status docker
or
service docker status
If Docker is running, the output should indicate "active (running)" status.
Task 2: Manage Jenkins Service
To manage Jenkins, you will use the systemctl
or service
commands to control the service.
Step 1: Check Jenkins status before stopping
systemctl status jenkins
Step 2: Stop Jenkins service
sudo systemctl stop jenkins
Step 3: Check Jenkins status after stopping
systemctl status jenkins
For the task, you can take screenshots of the terminal showing the Jenkins status before and after stopping the service.
Task 3: Read About Systemctl vs. Service
systemctl Command
systemctl
is part of systemd
, the newer and more feature-rich service manager used in most modern Linux distributions. It provides much more control and features than the older service
command.
Syntax Example:
systemctl status docker
Advantages:
Used to manage services, check statuses, enable/disable services to start on boot.
Can be used to reboot, shut down, and manage the entire system.
Offers a more detailed status output, including logs.
service Command
The service
command is part of the older SysV init system, which is still available on some systems. It provides basic start, stop, and status operations for services.
Syntax Example:
service docker status
Advantages:
Simpler and quicker for basic service management tasks.
Can be useful for compatibility with older scripts.
Key Differences:
systemctl is the standard for modern Linux distributions using
systemd
, offering more granular control and richer features.service is simpler and part of the older init system but is still available for backward compatibility on many systems.
Task 4: Automate Service Management
You can write a simple shell script to automate the starting and stopping of Docker and Jenkins services.
Shell Script: manage_
services.sh
#!/bin/bash
# Function to start Docker and Jenkins services
start_services() {
echo "Starting Docker..."
sudo systemctl start docker
echo "Starting Jenkins..."
sudo systemctl start jenkins
echo "Services started successfully!"
}
# Function to stop Docker and Jenkins services
stop_services() {
echo "Stopping Docker..."
sudo systemctl stop docker
echo "Stopping Jenkins..."
sudo systemctl stop jenkins
echo "Services stopped successfully!"
}
# Check the argument passed to the script
case "$1" in
start)
start_services
;;
stop)
stop_services
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
;;
esac
How to use the script:
Save the script as
manage_
services.sh
.Make the script executable:
chmod +x manage_services.sh
Run the script to start or stop the services:
./manage_services.sh start ./manage_services.sh stop
Task 5: Enable and Disable Services
Using systemctl
, you can configure Docker to start automatically at boot and prevent Jenkins from doing so.
Enable Docker to Start on Boot:
sudo systemctl enable docker
Disable Jenkins from Starting on Boot:
sudo systemctl disable jenkins
You can verify if Docker is enabled and Jenkins is disabled by running:
systemctl is-enabled docker
systemctl is-enabled jenkins
Task 6: Analyze Logs Using journalctl
You can use journalctl
to analyze logs for Docker and Jenkins. Here's how to retrieve logs:
View Docker Logs:
sudo journalctl -u docker
View Jenkins Logs:
sudo journalctl -u jenkins
Sample Output from journalctl
(Docker):
Oct 11 15:23:12 ubuntu dockerd[1234]: Docker daemon started
Oct 11 15:23:15 ubuntu dockerd[1234]: Container started: nginx
Sample Output from journalctl
(Jenkins):
Oct 11 16:40:33 ubuntu jenkins[4321]: Jenkins started successfully
Oct 11 16:41:00 ubuntu jenkins[4321]: Admin user logged in
Key Insights from Logs:
Docker Logs: You can see when the Docker daemon started, any container activity, and potential errors if Docker services fail to start or stop.
Jenkins Logs: Jenkins logs provide useful information such as the time Jenkins was started, users logging in, and any errors during startup.
You can further filter logs by date and time, or check for specific errors using:
sudo journalctl -u docker --since "2024-10-11 12:00:00"
sudo journalctl -u jenkins -p err
Conclusion
With the tasks of installing Docker and Jenkins, managing services, automating start/stop processes, enabling and disabling services on boot, and analyzing logs successfully completed, you've tackled all challenges with efficiency and precision. These foundational skills in service management and automation are crucial for maintaining robust systems.
Keep experimenting, keep automating, and as always—happy learning! 🚀
Subscribe to my newsletter
Read articles from Faizan Shaikh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by