# 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:

```bash
sudo apt update
sudo apt upgrade
```

**Step 2: Install Required Packages**

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

```bash
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:

```bash
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:

```bash
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:

```bash
sudo apt update
```

**Step 6: Install Docker**

Now, you can install Docker itself:

```bash
sudo apt install docker-ce
```

**Step 7: Start and Enable Docker**

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

```bash
sudo systemctl start docker
sudo systemctl enable docker
```

**Step 8: Verify the Installation**

You can check if Docker is installed correctly by running:

```bash
sudo docker --version
```

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

```bash
sudo systemctl status docker
```

2. **Installation of Jenkins :**
    

**Step 1: Update Your System**

Start by updating your package database:

```bash
sudo apt update
sudo apt upgrade
```

**Step 2: Install Java**

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

```bash
sudo apt install openjdk-11-jdk
```

You can verify the installation with:

```bash
java -version
```

**Step 3: Add the Jenkins Repository**

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

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

Then, add the Jenkins repository:

```bash
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:

```bash
sudo apt update
```

**Step 5: Install Jenkins**

Now you can install Jenkins:

```bash
sudo apt install jenkins
```

**Step 6: Start and Enable Jenkins**

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

```bash
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):

```bash
sudo ufw allow 8080
```

**Step 8: Access Jenkins**

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

```bash
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:

```bash
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
```

2. **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`:
        
        ```bash
          sudo systemctl start nginx
        ```
        
    * `service`:
        
        ```bash
          sudo service nginx start
        ```
        
* **Stopping a Service**:
    
    * `systemctl`:
        
        ```bash
          sudo systemctl stop nginx
        ```
        
    * `service`:
        
        ```bash
          sudo service nginx stop
        ```
        
* **Checking Status**:
    
    * `systemctl`:
        
        ```bash
          sudo systemctl status nginx
        ```
        
    * `service`:
        
        ```bash
          sudo service nginx status
        ```
        

**4\. Additional Features of** `systemctl`

* **Enable/Disable Services**:
    
    * To enable a service to start on boot:
        
        ```bash
          sudo systemctl enable nginx
        ```
        
    * To disable it:
        
        ```bash
          sudo systemctl disable nginx
        ```
        
* **Restarting Services**:
    
    * To restart a service:
        
        ```bash
          sudo systemctl restart nginx
        ```
        
* **Viewing Logs**:
    
    * To view logs for a specific service:
        
        ```bash
          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).
    
* ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728734659293/4d491f9b-860b-4f2b-8df3-7de13a18442a.png?auto=compress,format&format=webp align="left")
    
    use command **systemctl status docker**
    

**Manage Jenkins Service:**

* * Stop the Jenkins service and post before and after screenshots.
        
        * ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728734810104/5786fcc8-5f0b-4b15-89f2-1fb48140d5a3.png?auto=compress,format&format=webp align="left")
            
            before stopping jenkins
            
        * ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728734842301/f98ef674-43ff-46e2-9145-664a2210b3f9.png?auto=compress,format&format=webp align="left")
            
            after stopping jenkins
            

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

```bash

    #!/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:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728740841910/5c271eda-2c3d-499c-8091-e918f0a356f4.png?auto=compress,format&format=webp align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728740875178/e102d651-01d3-4eee-a37f-82096e903c2c.png?auto=compress,format&format=webp align="left")

```bash
#!/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:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728740928289/15c8f1c9-66a2-45fd-b1b9-e4c3837cd892.png?auto=compress,format&format=webp align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728740940831/e8c31fba-6680-4b54-93df-7a019546f0b0.png?auto=compress,format&format=webp align="left")

4. **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:
        
        ```bash
          sudo systemctl enable docker
        ```
        
        #### Disable Jenkins from Starting on Boot:
        
        ```bash
          sudo systemctl disable jenkins
        ```
        
        You can verify if Docker is enabled and Jenkins is disabled by running:
        
        ```bash
          systemctl is-enabled docker
          systemctl is-enabled jenkins
        ```
        
5. **Analyze Logs:**
    
    * Use `journalctl` to analyze the logs of the Docker and Jenkins services.
        

#### **View Docker Logs:**

```bash
    sudo journalctl -u docker
```

#### View Jenkins Logs:

```bash
    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!
