Day 7 Task: Understanding Package Manager and Systemctl

Syed DadapeerSyed Dadapeer
14 min read

In the world of Linux, two essential tools that you'll frequently encounter are Package Managers and Systemctl. These are the magical wands that help manage software and services on your system, but what exactly do they do? ๐Ÿงฉ

๐Ÿ“ฆ What is a Package Manager?

A Package Manager is like a personal assistant for your system. It handles the installation, updating, and removal of software packages, making your life easier. Instead of manually installing each piece of software (which can be messy ๐Ÿงน), the package manager ensures you get the latest versions with all the dependencies sorted out.

๐Ÿ› ๏ธ What Can a Package Manager Do?

  • ๐Ÿ“ฅInstall Software: Easily download and install applications from a repository.
    ๐Ÿ”ง Example: sudo apt install vlc (for Ubuntu/Debian)

  • ๐Ÿ”„Update Software: Keep all your installed apps up to date with a single command!
    ๐Ÿ”ง Example: sudo dnf update (for Fedora)

  • ๐Ÿ—‘๏ธRemove Software: Uninstall apps you no longer need without leaving traces behind.
    ๐Ÿ”ง Example: sudo yum remove firefox (for CentOS)

  • ๐Ÿ“ฆDependency Management: It handles all the required software components (dependencies) for your app to work correctly. Think of it like downloading all the LEGO blocks to build the perfect set! ๐Ÿงฉ

  • APT: Used in Debian/Ubuntu systems.

  • YUM/DNF: Found in Red Hat/CentOS/Fedora systems.

  • Pacman: The go-to for Arch Linux users.


Tasks:

  1. Install Docker and Jenkins:

Install Docker and Jenkins on your system from your terminal using package managers.

To install Docker and Jenkins on your system via the terminal, follow these steps based on your operating system.

1. Install Docker๐Ÿณ**:**

  1. Update your package index:

     sudo apt update
    
  2. Install dependencies:

     sudo apt install apt-transport-https ca-certificates curl software-properties-common
    
  3. Add Docker's official GPG key:

     curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
    
  4. Set up the Docker stable repository:

     echo "deb [arch=amd64 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
    
  5. Install Docker:

     sudo apt update
     sudo apt install docker-ce docker-ce-cli containerd.io
    
  6. Start Docker and enable it to start on boot:

     sudo systemctl start docker
     sudo systemctl enable docker
    
  7. Verify the Docker installation:

     sudo docker --version
    

2. Install Jenkinsโ™พ๏ธ:

  1. Install Java (Jenkins requires Java)

     sudo apt update
     sudo apt install fontconfig openjdk-17-jre
    
  2. Add Jenkins repository key:

     sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
     https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
    
  3. Add Jenkins apt repository:

     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
    
  4. Update and install Jenkins:

     sudo apt update
     sudo apt-get install jenkins
    
  5. Start Jenkins:

     sudo systemctl start jenkins
    
  6. Enable Jenkins to start on boot:

     sudo systemctl enable jenkins
    
  7. Verify Jenkins is running:

     sudo systemctl status jenkins
    

    Once Jenkins is installed, you can access it by navigating to http://localhost:8080 in your browser and follow the instructions to unlock and set up Jenkins for the first time. Make sure your firewall allows port 8080.

That's it! You now have both Docker and Jenkins installed on your system. ๐Ÿš€


  1. Write a Blog or Article:

  • Write a small blog or article on how to install these tools using package managers on Ubuntu and CentOS.

In the world of DevOps and automation, Docker and Jenkins are two essential tools that every developer and system engineer needs. Docker makes it simple to containerize applications, while Jenkins automates software builds, testing, and deployment.

  1. ๐Ÿณ Installing Docker

    1. On Ubuntu

    Step 1: Update the system and install dependencies

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

    Step 2: 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 3: Install Docker

     sudo apt update
     sudo apt install docker-ce docker-ce-cli containerd.io
    

    Step 4: Start and verify the Docker

     sudo systemctl start docker
     docker --version
    

    2. On CentOS

    Step 1: Install the required packages and set up the Docker repository

     sudo yum install -y yum-utils
     sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    

    Step 2: Install Docker

     sudo yum upgrade
     sudo yum install docker-ce docker-ce-cli containerd.io
    

    Step 3: Start and enable Docker

     sudo systemctl start docker
     sudo systemctl enable docker
    

    Step 4: Verify Docker installation

     docker --version
    

    Congratulations, Docker is now installed on both Ubuntu and CentOS! ๐ŸŽ‰


    ๐Ÿค– Installing Jenkins

    1. On Ubuntu

    Step 1: Install Java (Jenkins requires Java)

     sudo apt update
     sudo apt install openjdk-11-jdk
    

    Step 2: Add Jenkins repository and GPG key

     curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.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 binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
    

    Step 3: Install Jenkins

     sudo apt update
     sudo apt install jenkins
    

    Step 4: Start and enable Jenkins

     sudo systemctl start jenkins
     sudo systemctl enable jenkins
    

    Step 5: Verify Jenkins installation

     sudo systemctl status jenkins
    

    2. On CentOS

    Step 1: Install Java

     sudo yum install fontconfig java-17-openjdk
    

    Step 2: Add the Jenkins repository

     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
    

    Step 3: Install Jenkins

     sudo yum upgrade
     sudo yum install jenkins
    

    Step 4: Start and enable Jenkins

     sudo systemctl start jenkins
     sudo systemctl enable jenkins
    

    Step 5: Check Jenkins's status

     sudo systemctl status jenkins
    

    After Jenkins is installed, navigate to http://<your-server-ip>:8080 in your web browser to unlock Jenkins and complete the initial setup. ๐Ÿ–ฅ๏ธ

    ๐ŸŽฏ Final Thoughts

    And there you have it! Whether you're running Ubuntu or CentOS, installing Docker and Jenkins is just a few terminal commands away. These tools empower you to streamline development and automate tasks, making them essential for modern IT environments. So, roll up your sleeves and dive into the world of containers and automation! ๐Ÿš€


๐Ÿ”ง What is Systemctl?

Systemctl is your one-stop solution to manage services (background processes) in Linux systems. It interacts with systemd, the service manager for Linux, to start, stop, enable, and check the status of services. ๐Ÿ–ฅ๏ธ

๐Ÿ› ๏ธ What Can You Do with Systemctl?

  • ๐Ÿš€Start a Service: Fire up a service whenever you need it.
    ๐Ÿ”ง Example: sudo systemctl start nginx

  • ๐Ÿ›‘Stop a Service: Bring a service to a halt when you no longer need it.
    ๐Ÿ”ง Example: sudo systemctl stop nginx

  • ๐Ÿ” Restart a service: Stops and then starts a service to apply changes.

    ๐Ÿ”ง Example: sudo systemctl restart nginx

  • โš™๏ธEnable a Service: Set services to start automatically when your system boots up.
    ๐Ÿ”ง Example: sudo systemctl enable nginx

  • โš™๏ธDisable a Service: Prevent services from starting at boot time.
    ๐Ÿ”ง Example: sudo systemctl disable nginx

  • Check Service Status: Find out if a service is running, inactive, or failed.
    ๐Ÿ”ง Example: sudo systemctl status nginx


Tasks:

  1. Check Docker Service Status:

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

      To check the status of the Docker service on your system after completing the installation, you can run the following command:

        systemctl status docker
      

      OR

        service docker status
      

      This command will give you a detailed report of the Docker service's status, including whether it's running, enabled, and any potential errors or warnings.

      If Docker is running successfully, you should see output similar to this:

        โ— docker.service - Docker Application Container Engine
           Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
           Active: active (running) since [date and time]
             Docs: https://docs.docker.com
           Main PID: [PID] ([docker])
            Tasks: [number]
           Memory: [size]
           CPU: [time]
           CGroup: /system.slice/docker.service
                   โ””โ”€[PID] /usr/bin/dockerd
      

      If it's not running, you can start the service using:

        systemctl start docker
      

      And to enable Docker to start on boot:

        systemctl enable docker
      

  1. Manage Jenkins Service:

Stop the Jenkins service and post before and after screenshots.

To stop the Jenkins service on your system, use the following command:

systemctl stop jenkins

This will stop the Jenkins service. You can verify that it has stopped by checking its status with:

systemctl status jenkins

If the service has successfully stopped, the output will indicate that Jenkins is no longer running:

โ— jenkins.service - LSB: Start Jenkins at boot time
   Loaded: loaded (/etc/init.d/jenkins; enabled)
   Active: inactive (dead) since [date and time]

  1. Read About Systemctl vs. Service:

    • Read about the differences between the systemctl and service commands.

In Linux, both systemctl and service are used to manage services, but they come from different init systems and have different capabilities.

1. Background of systemctl and service

  • systemctl: This is part of systemd, the newer and more modern service manager for Linux. It is widely adopted across many modern Linux distributions, including Ubuntu (since 15.04), CentOS 7+, Fedora, and others.

  • service: This command is used to interact with SysVinit, the older initialization system that was used in Linux before systemd. Though it's still available for compatibility in some systems, its usage is gradually being phased out in favor of systemd.

2. Syntax Differences

systemctl Command (for systemd)

  • Syntax: systemctl <action> <service>

  • Example:

    • To check the status of Docker:

        systemctl status docker
      

service Command (for SysVinit or systemd in compatibility mode)

  • Syntax: service <service> <action>

  • Example:

    • To check the status of Docker:

        service docker status
      
  • Example: systemctl status docker vs. service docker status.

    • With systemctl:

        systemctl status docker
      

      Output (for systemd):

        โ— docker.service - Docker Application Container Engine
           Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
           Active: active (running) since ...
      
    • With service:

        service docker status
      

      Output (on a systemd system):

        โ— docker.service - Docker Application Container Engine
           Active: active (running) ...
      

Key Differences in Summary:

Featuresystemctl (systemd)service (SysVinit)
Init SystemWorks with systemdWorks with SysVinit, or systemd via compatibility
Control LevelAdvanced control (enable/disable, reload)Basic control (start, stop, status)
Manage System StatesYes (reboot, poweroff, rescue)No
Mask/Unmask ServicesYesNo
Enable/Disable at BootYes (via enable/disable)No direct control (can edit /etc/rc.d)
Future-ProofPreferred for modern Linux systemsLegacy, mainly for backward compatibility

๐Ÿ” Understanding the Difference:

  • Package Manager deals with installing, updating, and removing software packages.

    • ๐Ÿ“ฆ Think of it as your software installer/uninstaller.
  • Systemctl is for managing services on your system (starting, stopping, enabling services).

    • ๐Ÿ› ๏ธ It controls how services behave.

๐Ÿ”„ Real-Life Example:

Imagine you're hosting a web server on your Linux machine using Nginx (a popular web server). You'd use systemctl to control when this web server starts, stops, or restarts based on your needs. Pretty cool, right? ๐Ÿ˜Ž

โš™๏ธ How Do Package Managers and Systemctl Work Together?

Package Managers help you install software like Nginx, while systemctl ensures the software is running as a service. Once you install Nginx using a package manager like APT, you can use systemctl to start the Nginx service and even make it auto-start when your system boots up!

Here's a simple workflow to illustrate:

  1. Install Nginx using APT:
    sudo apt install nginx ๐Ÿ“ฆ

  2. Start Nginx as a service:
    sudo systemctl start nginx ๐Ÿ”ง

  3. Enable Nginx to start on boot:
    sudo systemctl enable nginx ๐Ÿ”„

๐Ÿ” Pro Tip:

Always run package management or systemctl commands with sudo to ensure you have the necessary permissions!


Additional Tasks:

  1. Automate Service Management:
  • Write a script to automate the starting and stopping of Docker and Jenkins services.

    This script will allow you to easily control both services from the terminal by passing either a "start" or "stop" argument.

    Script: manage_services.sh

      #!/bin/bash
    
      # Function to start services
      start_services() {
          echo "Starting Docker and Jenkins services..."
    
          # Start Docker
          sudo systemctl start docker
          if [ $? -eq 0 ]; then
              echo "Docker started successfully."
          else
              echo "Failed to start Docker."
          fi
    
          # Start Jenkins
          sudo systemctl start jenkins
          if [ $? -eq 0 ]; then
              echo "Jenkins started successfully."
          else
              echo "Failed to start Jenkins."
          fi
      }
    
      # Function to stop services
      stop_services() {
          echo "Stopping Docker and Jenkins services..."
    
          # Stop Docker
          sudo systemctl stop docker
          if [ $? -eq 0 ]; then
              echo "Docker stopped successfully."
          else
              echo "Failed to stop Docker."
          fi
    
          # Stop Jenkins
          sudo systemctl stop jenkins
          if [ $? -eq 0 ]; then
              echo "Jenkins stopped successfully."
          else
              echo "Failed to stop Jenkins."
          fi
      }
    
      # Check if an argument is passed
      if [ "$#" -ne 1 ]; then
          echo "Usage: $0 {start|stop}"
          exit 1
      fi
    
      # Check if the argument is "start" or "stop"
      if [ "$1" = "start" ]; then
          start_services
      elif [ "$1" = "stop" ]; then
          stop_services
      else
          echo "Invalid argument. Usage: $0 {start|stop}"
          exit 1
      fi
    

    How to Use the Script:

    • Save the first script as manage_services.sh.

    • Make it executable: chmod +x manage_services.sh.

    • Run the script to start or stop both services:

      • To start Docker and Jenkins:

          ./manage_services.sh start
        
      • To stop Docker and Jenkins:

          ./manage_services.sh stop
        

  1. Enable and Disable Services:
  • Use systemctl to enable Docker to start on boot and disable Jenkins from starting on boot.

    To configure services to start or not start on boot using systemctl, you can use the enable and disable commands.

    1. Enable Docker to start on boot

This ensures Docker starts automatically every time the system boots up.

    systemctl enable docker

2. Disable Jenkins from starting on boot

This will prevent Jenkins from starting automatically on boot.

    systemctl disable jenkins

Explanation:

  • enable: This command creates the necessary symbolic links so that the service is started automatically when the system boots.

  • disable: This removes the symbolic links, so the service will not be started on boot.

You can verify the results with the following commands:

  • Check if Docker is enabled on boot:

      systemctl is-enabled docker
    

    It should return enabled.

  • Check if Jenkins is disabled on boot:

      systemctl is-enabled jenkins
    

    It should return disabled.


  1. Analyze Logs:

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

      To analyze the logs of the Docker and Jenkins services, you can use the journalctl command. This command is used to query and display messages from the journal, which is the logging system used by systemd.

      1. Analyzing Docker Logs

      To view the logs for the Docker service, you can run the following command:

        journalctl -u docker
      

      This command will display logs specifically related to the Docker service. You can add the -f option to follow the logs in real-time:

        journalctl -u docker -f
      

      Findings from Docker Logs:

      • You should look for messages indicating successful service starts, stops, container creations, or errors.

      • Common entries might include messages about pulling images, starting containers, or warnings about issues like storage or network.

      • Example log entries might include:

          [INFO] 2024-10-11 10:00:00 Started docker.service
          [ERROR] 2024-10-11 10:05:00 Error starting container: <container_id> - Out of memory
        

2. Analyzing Jenkins Logs

To view the logs for the Jenkins service, run:

        journalctl -u jenkins

Similarly, you can use the -f option for real-time updates:

        journalctl -u jenkins -f

Findings from Jenkins Logs:

  • Look for log entries that indicate successful startup, any errors during startup, or failures during job executions.

  • You might see logs related to user actions, plugin installations, or build results.

  • Example log entries might include:

      [INFO] 2024-10-11 10:00:00 Jenkins is fully up and running
      [ERROR] 2024-10-11 10:10:00 Failed to connect to plugin repository: <url> - timeout
    

Summary of Findings:

  • For Docker, you may notice frequent logs related to container management, including successful starts and any errors regarding resource constraints.

  • For Jenkins, you may find logs indicating successful job executions or issues with plugins, along with general status updates about the Jenkins server.

Notes:

  • You can filter logs by time with options like --since and --until, e.g.:

      journalctl -u docker --since "2024-10-11" --until "2024-10-11 12:00"
    
  • Use the --no-pager option to prevent the output from being piped to a pager, which can help in viewing long logs more clearly:

      journalctl -u docker --no-pager
    

This method of log analysis can help you troubleshoot issues, monitor service performance, and understand the behavior of your applications running within Docker and Jenkins. If you have specific log messages or issues you encounter, feel free to share them, and I can help interpret those logs! ๐Ÿ˜Š


Today, we covered several essential commands and tasks related to managing Docker and Jenkins services on a Linux system. These commands allow you to effectively manage and monitor the Docker and Jenkins services on your system. The ability to install, start, stop, enable, and disable services, along with analyzing logs, is crucial for maintaining a smooth development and deployment pipeline.

๐Ÿ”—Check out my progress and insights on Hashnode and my code on GitHub!
Hashnode and GitHub

If you have any further questions or need more commands, feel free to ask! ๐Ÿ˜Š

0
Subscribe to my newsletter

Read articles from Syed Dadapeer directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Syed Dadapeer
Syed Dadapeer

Hi, I'm Syed Dadapeer! I'm an experienced ๐‚๐ข๐ญ๐ซ๐ข๐ฑ ๐€๐๐ฆ๐ข๐ง๐ข๐ฌ๐ญ๐ซ๐š๐ญ๐จ๐ซ and ๐’๐ฒ๐ฌ๐ญ๐ž๐ฆ ๐„๐ง๐ ๐ข๐ง๐ž๐ž๐ซ with over 2.5 years of IT experience, focusing on troubleshooting complex issues, cloud computing, virtualization, and Citrix products. I'm also ๐‘๐ž๐ ๐‡๐š๐ญ ๐œ๐ž๐ซ๐ญ๐ข๐Ÿ๐ข๐ž๐! ๐Ÿ’ก Iโ€™m passionate about learning new technologies and applying them to real-world challenges. Iโ€™m also exploring the exciting fields of ๐€๐ซ๐ญ๐ข๐Ÿ๐ข๐œ๐ข๐š๐ฅ ๐ˆ๐ง๐ญ๐ž๐ฅ๐ฅ๐ข๐ ๐ž๐ง๐œ๐ž, ๐‚๐ฅ๐จ๐ฎ๐ ๐‚๐จ๐ฆ๐ฉ๐ฎ๐ญ๐ข๐ง๐ , and ๐ƒ๐ž๐ฏ๐Ž๐ฉ๐ฌ.โ™พ๏ธ ๐Ÿš€ Letโ€™s connect! Iโ€™m always open to new opportunities and collaborations. Feel free to reach out via ๐‹๐ข๐ง๐ค๐ž๐๐ˆ๐ง or at ๐ฌ๐๐š๐๐ฎ2206@๐ ๐ฆ๐š๐ข๐ฅ.๐œ๐จ๐ฆ.