Package Manager and Systemctl

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.

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.

  1. Installation of Docker :

Step 1: Update Your System

First, it’s always a good idea to update your package database. Open your terminal and run:

sudo apt update
sudo apt upgrade

Step 2: Install Required Packages

Docker needs a few packages to install correctly. You can install them with:

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

Step 3: Add Docker’s Official GPG Key

Next, you need to add Docker’s GPG key to ensure the software is authentic. Run the following command:

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

Step 4: Add the Docker Repository

Now, add the Docker repository to your APT sources:

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Step 5: Update the Package Database Again

After adding the Docker repository, you need to update your package database once more:

sudo apt update

Step 6: Install Docker

Now, you can install Docker itself:

sudo apt install docker-ce

Step 7: Start and Enable Docker

To start Docker and enable it to run on boot, use these commands:

sudo systemctl start docker
sudo systemctl enable docker

Step 8: Verify the Installation

You can check if Docker is installed correctly by running:

sudo docker --version

To check if Docker is running, you can also run:

sudo systemctl status docker
  1. Installation of Jenkins :

Step 1: Update Your System

Start by updating your package database:

sudo apt update
sudo apt upgrade

Step 2: Install Java

Jenkins requires Java to run. You can install OpenJDK, which is a popular choice:

sudo apt install openjdk-11-jdk

You can verify the installation with:

java -version

Step 3: Add the Jenkins Repository

Next, you need to add the Jenkins repository to your system. First, add the GPG key:

wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -

Then, add the Jenkins repository:

echo deb http://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list

Step 4: Update the Package Database Again

Now that you’ve added the Jenkins repository, update your package list:

sudo apt update

Step 5: Install Jenkins

Now you can install Jenkins:

sudo apt install jenkins

Step 6: Start and Enable Jenkins

Once installed, start Jenkins and enable it to start on boot:

sudo systemctl start jenkins
sudo systemctl enable jenkins

Step 7: Adjust Firewall (if necessary)

If you have a firewall enabled, you’ll need to allow traffic on Jenkins' default port (8080):

sudo ufw allow 8080

Step 8: Access Jenkins

Now, you can access Jenkins by opening your web browser and navigating to:

http://your_server_ip:8080

Step 9: Unlock Jenkins

The first time you access Jenkins, you’ll need to unlock it using the initial admin password. You can find this password by running:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword
  1. 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).

    The systemctl and service commands are both used to manage services on a Linux system, but they come from different init systems and have some key differences. Here’s a breakdown of both commands:

    1. Init System

    • systemctl:

      • Part of systemd, which is the modern init system used by many Linux distributions, including Ubuntu (from version 15.04 onwards), CentOS 7, and others.

      • Provides a more comprehensive interface for managing services and system resources.

    • service:

      • A more traditional command that is often used with SysVinit or upstart. It is present on older distributions or those that haven’t migrated to systemd.

2. Functionality

  • systemctl:

    • More powerful and versatile.

    • Allows you to manage not just services but also targets, sockets, timers, and more.

    • Supports advanced features like dependency management, parallel startup, and logging.

    • Syntax is generally: systemctl [action] [service].

  • service:

    • A simpler command primarily for starting, stopping, and checking the status of services.

    • Limited to services and doesn’t support other systemd features.

    • Syntax is: service [service] [action].

3. Examples

  • Starting a Service:

    • systemctl:

          sudo systemctl start nginx
      
    • service:

          sudo service nginx start
      
  • Stopping a Service:

    • systemctl:

          sudo systemctl stop nginx
      
    • service:

          sudo service nginx stop
      
  • Checking Status:

    • systemctl:

          sudo systemctl status nginx
      
    • service:

          sudo service nginx status
      

4. Additional Features of systemctl

  • Enable/Disable Services:

    • To enable a service to start on boot:

          sudo systemctl enable nginx
      
    • To disable it:

          sudo systemctl disable nginx
      
  • Restarting Services:

    • To restart a service:

          sudo systemctl restart nginx
      
  • Viewing Logs:

    • To view logs for a specific service:

          journalctl -u nginx
      

5. Best Practices

  • If you are on a modern Linux distribution that uses systemd, it’s generally recommended to use systemctl for managing services, as it provides a more robust set of features and capabilities.

  • The service command may still be available for backward compatibility, but it is less preferred for managing services in a systemd environment.

Check Docker Service Status:

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

  • use command systemctl status docker

Manage Jenkins Service:

    • Stop the Jenkins service and post before and after screenshots.

      • before stopping jenkins

      • after stopping jenkins

  1. Automate Service Management:

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

    #!/bin/bash

    echo "Do you want to Start or Stop Docker? (Type 'start' or 'stop')"
    read start_stop

    if [ "$start_stop" = "start" ]; then
        sudo systemctl start docker
        echo "Docker service started successfully."
        exit 0
    elif [ "$start_stop" = "stop" ]; then
        sudo systemctl stop docker
        sudo systemctl stop docker.socket
        echo "Docker service stopped successfully."
        exit 0
    else
        echo "Invalid input. Please type 'start' or 'stop'."
        exit 1
    fi

Script to automate the starting and stopping of Docker

Output:

#!/bin/bash

echo "Do you want to Start or Stop Jenkins? (Type 'start' or 'stop')"
read start_stop

if [ "$start_stop" = "start" ]; then
    sudo systemctl start jenkins
    echo "Jenkins service started successfully."
    exit 0
elif [ "$start_stop" = "stop" ]; then
    sudo systemctl stop jenkins
    echo "Jenkins service stopped successfully."
    exit 0
else
    echo "Invalid input. Please type 'start' or 'stop'."
    exit 1
fi

Script to automate the starting and stopping of Jenkins

Output:

  1. Enable and Disable Services:

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

      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
      
  2. Analyze Logs:

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

View Docker Logs:

    sudo journalctl -u docker

View Jenkins Logs:

    sudo journalctl -u jenkins

journalctl is a command-line utility in Linux used to query and display logs from the systemd journal. The journal is a centralized logging system that collects and manages logs from the operating system and various services.

journalctl is a powerful tool for managing and viewing logs in a systemd-based system. It helps you troubleshoot issues, monitor system activity, and maintain the health of services. By using various options, you can tailor the log output to suit your needs.

Keep Learning, Sharing and Growing

Subscribe to my newsletter

I’m excited to share that I’m currently writing blogs focused on DevOps-related content. My goal is to explore various topics, including CI/CD, containerization, infrastructure as code, and best practices for collaboration between development and operations teams.

If you’re interested in DevOps or have specific topics you'd like to see covered, feel free to reach out or share your thoughts! I’m looking forward to engaging with all of you and sharing knowledge on this dynamic field.

Happy coding!