Quick introduction to the a2query Command

The a2query command is a very simple and niffty tool that is used to retrieve information from the configuration of a local Apache 2 HTTP Server.

Lets see a few examples of how to use a2query.

How to list all enabled apache modules

$ a2query -m
access_compat (enabled by maintainer script)
alias (enabled by maintainer script)
auth_basic (enabled by maintainer script)
authn_core (enabled by maintainer script)
authn_file (enabled by maintainer script)
authz_core (enabled by maintainer script)
authz_host (enabled by maintainer script)
authz_user (enabled by maintainer script)
autoindex (enabled by maintainer script)
deflate (enabled by maintainer script)
dir (enabled by maintainer script)
env (enabled by maintainer script)
filter (enabled by maintainer script)
mime (enabled by maintainer script)
mpm_event (enabled by maintainer script)
negotiation (enabled by maintainer script)
reqtimeout (enabled by maintainer script)
setenvif (enabled by maintainer script)
status (enabled by maintainer script)

    How to list all virtual hosts

    $a2query -s
    000-default (enabled by site administrator)
    

    How to check if a module is enabled?

    $a2query -m status
    status (enabled by maintainer script)
    

    How to check the enabled Apache MPM (Multi Processing Module)

    $a2query -M       
    event
    

    How to display the current Apache2 version

    $ a2query -v
    2.4.38
    

    Note: For every successful command, a2query returns a zero (0) exit code and non-zero exit code for every unsuccessful operation.

    How to split string and print over multiple lines in bash

    Sometimes, you get some cli outputs seperated/delimited by a character but you want it printed over multiple lines or used in a loop or pipe each item to another command. Lets solve that easily

    $ echo "1,2,3,4,5"
                        
    1,2,3,4,5
    
    # Now lets print this over multiple lines as follows
    $ echo "1,2,3,4,5" | sed -e 's/,/\n/g'
    
    1
    2
    3
    4
    5
    

    Lets consider a more practical example, lets get all all IPs of a machine using the hostname command.

    $ hostname -I
    
    192.168.1.179 172.17.0.1 172.18.0.1
    
    # Now, lets print this over multiple lines
    $ hostname -I | sed -e  's/ /\n/g'
    
    192.168.1.179
    172.17.0.1
    172.18.0.1
    

    We can parse this output to other commands

    $ hostname -I | sed -e  's/ /\n/g' | xargs -I {} echo "IP = {}"
    
    IP = 192.168.1.179
    IP = 172.17.0.1
    IP = 172.18.0.1
    

    How To Validate The Presence Of A Module In Perl From The Linux Shell

    Perl is a very versatile programming language and it allows for sharing and use of modules. The question occasionally arises, how do I check if a module is present in perl, lets solve that.

    To test if the XML::Parser module is present , just execute a simple perl script as follows

    $ perl -e "use XML::Parser"
    $ echo $?
    0
    

    By returning zero (0) as the exit code, this confirms that the module exists in perl.

    To check a module that is not present, lets do the following

    $ perl -e "use Not::PresentModule"
    
    Can't locate Not/PresentModule.pm in @INC (you may need to install the Not::PresentModule module) (@INC contains: /Library/Perl/5.34/darwin-thread-multi-2level /Library/Perl/5.34 /Network/Library/Perl/5.34/darwin-thread-multi-2level /Network/Library/Perl/5.34 /Library/Perl/Updates/5.34.1 /System/Library/Perl/5.34/darwin-thread-multi-2level /System/Library/Perl/5.34 /System/Library/Perl/Extras/5.34/darwin-thread-multi-2level /System/Library/Perl/Extras/5.34) at -e line 1.
    BEGIN failed--compilation aborted at -e line 1.
    
    $ echo $?                                                                                                                                                       2
    

    As you can see, this returns an error, this shows that the module is not present. Hopefully, this saves you some time.

    How To Generate Random String On Linux

    A quick way to generate random strings on linux is by using the ever versatile openssl application.

    The synopsis of the openssl command is as follow

    openssl rand [-help] [-out file] [-rand file…] [-writerand file] [-base64] [-hexnum

    The openssl command will generate num number of random bytes

    e.g To create 32 bytes random string, you can do

    $ openssl rand -base64 32
    hQwaQTd5WFRIPcfto129nfdX1dBPUOBQraqnSyD56hA=
    
    
    # to check the number of bytes, you can do
    $ openssl rand 32 | wc -c
          32
    

    How To Schedule Tasks on Linux Using the at Command

    The at command is a very useful and powerful tool in Linux that allows users to schedule single tasks to run at a specific time or date. It is an alternative to the cron command, which is used for managing recurring tasks, however the at command is is designed for one-time jobs.

    How to Install and enable the at Command

    The at command usually comes already installed on most modern Linux distributions. However, to check if the at command is installed, you can use the following command:

    $ at -V
    

    If the at command is not installed, you can install it on ubuntu or debian using apt as follows, on other distributions you can just use the package manager on the distribution.

    $ apt install -y at
    
    # then you can enable the daemon service atd as follows
    
    $ sudo systemctl enable --now atd
    

    Basic Syntax of the at Command

    The basic syntax of the at command is as follows:

    $ at [-q queue] [-f file] [-mldv] TIME
    
    • -q queue: Specifies the job queue (default is “a”).
    • -f file: Reads the job from the specified file.
    • -m: Sends an email to the user after the job has been executed.
    • -l: Lists the user’s pending jobs.
    • -d: Deletes a pending job.
    • -v: Displays the time of job execution.
    • TIME: Specifies the time when the job should be executed.

    Examples of how to use the at Command

    1: Scheduling a Command to Run at a Specific Time

    To schedule a command to run at 11:00 PM, you can use the following command:

    $ at 11:00 PM
    at> echo "Welcome to my world" > /tmp/hello.txt
    at> Ctrl+D
    

    This will schedule the echo command to write “Welcome to my world” to the specified file at 11:00 PM.

    2: Using a File to Specify Commands

    You can also use a script file to specify commands. For example, to schedule a script to run at 10:00 PM:

    $ echo "whoami" > /path/to/script.sh
    $ at 11:00 PM -f /path/to/script.sh
    
    warning: commands will be executed using /bin/sh
    job 2 at Wed Aug  7 11:10:00 2024
    

    This will schedule the whoami command to run at 11:00 PM.

    3: System Shutdown at a Specific Date

    To schedule a system shutdown at 11:45 PM on July 31:

    $ at 15:35 PM June 30
    at> shutdown now
    at> Ctrl+D
    

    This will schedule the shutdown of the machine at 15:35 PM on June 30.

    Viewing and Managing Scheduled Jobs

    To view the scheduled jobs, you can use the atq command:

    You can view all the jobs already scheduled for various times by using the atq command

    $ atq
    

    You can remove a specific job from the queue by using the job-id as follows

    $ atrm <job-id>
    

    How To Resolve The “Ubuntu Repository No Longer Has a Release” Issue

    Sometimes, when you run the command sudo apt update -y or sudo apt upgrade -y, you get the error message such as

    The repository 'http://gb.archive.ubuntu.com/ubuntu kinetic Release' no longer has a Release file.
    

    This error is caused because the releases sources for the ubuntu version you are attempting to update has been migrated to an archive server. So, to fix this solution, all you need to do is point the sources to the archived server.

    This can be easily achieved by running the commands below

    # Step 1: backup your existing source list file
    $  cp /etc/apt/sources.list /etc/apt/sources.list.bak 
    
    # Step 2: run sed to replace the sources path
    $ sudo sed -i -re 's/([a-z]{2}.)?archive.ubuntu.com|security.ubuntu.com/old-releases.ubuntu.com/g' /etc/apt/sources.list
    
    # step 3: update the OS and do a dist upgrade
    $ sudo apt-get update && sudo apt-get dist-upgrade
    

    Once the commands listed above has been executed successfully, the error message will disappear and you can then freely update your OS at any time without any hinderances.

    How to conduct a quick MOT (health check) for your disk in linux

    The disk is usually considered by many to be the one of the most important component in the computer since it holds the data and infact the operarting system also. Occasionally, just like in the real world, one needs to conduct a health check for the disk to see that it is in good working condition.

    Luckily for us, linux has a tool that can help conduct a test and it is called the SMART tool, SMART being an acronym for Self-Monitoring, Analysis, and Reporting Technology. It is a tool that can be used to validate the health of a hard disk, so that one can take pre-emptive measures before the disk fails, it works perfectly with a traditional magnetic disk as well as the modern SSD drives.

    How to Use SMART

    The smartctl tool is part of the smartmontools suite of tools on linux and it can be installed easily on ubuntu as follows

    List the hard disks in your system by doing the following

    $ lsblk
    
    NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
    nvme0n1     259:0    0 953.9G  0 disk
    ├─nvme0n1p1 259:1    0   512M  0 part /boot/efi
    └─nvme0n1p2 259:2    0 953.4G  0 part /var/lib/containers/storage/overlay
    

    Now, you can conduct the disk test by doing the following, where (/dev/nvme0n1) is the disk

    $ sudo smartctl -H /dev/nvme0n1
    
    smartctl 7.2 2020-12-30 r5155 [x86_64-linux-5.19.0-38-generic] (local build)
    Copyright (C) 2002-20, Bruce Allen, Christian Franke, www.smartmontools.org
    
    === START OF SMART DATA SECTION ===
    SMART overall-health self-assessment test result: PASSED
    
    
    

    How to install BASH (Bourne Again Shell) in Alpine

    The Alpine Linux docker image is a minimal docker image that is based on the Alpine Linux, in its bare form, it is as small as 5MB in size. However, by default, BASH is not included or installed in alpine, it defaults to /bin/sh. This becomes sometimes difficult if you already have scripts targeted at BASH and this needs to be ported into an alpine image.

    The easy solution is to install BASH into the image, and this can be done easily as demonstrated below

    $ apk update
    $ apk upgrade
    $ apk add bash
    

    This can also be included into a Dockerfile as shown below

    FROM alpine:3.17.2
    
    RUN apk update && \
        apk add bash
    

    Then, you can build the image as follow

    $ docker build -t alpine/demo:v1 -f Dockerfile.
    

    This newly generated docker image can then be used with BASH shell as follow

    $ docker run --rm --it alpine/demo:v1 bash
    

    How to format/prettify a json file in-place using jq.

    A vital tool in the arsenal of every cli lover is jq. This tool is a command line JSON processor that makes querying for information from a json file very easy and seamless.

    However, one feature it is lacking , at least as at the time of this writing is in-place editing, i.e. something similar to what is offerred by the stream editor (sed) , which can edit a file and save the result to the same file., consider the example below

    Using sed to replace every instance of HELLO with WORLD

    $ sed -e 's/HELLO/WORLD' -i input_file.json
    

    To achieve something similar in jq, copy the json document below and save it in a file called unformatted.json

    {
    "order_num" : "O2012019231a",
    "order_date" : "2012-06-27",
    "order_id" : 21934,
    "order_item" : [
    {
    "product_id" : 20933,
    "quantity" : 3,
    "price" : 36000,
    "product_name" : "Thingamagic 2000",
    "unit_price" : 12000
    },
    {
    "product_id" : 10366,
    "quantity" : 1,
    "price" : 100,
    "product_name" : "Super Duper Blooper",
    "unit_price" : 100
    }
    ]
    }
    

    Now, to prettify the document (unformatted.json) in place, you have two options, listed below

    • Using cat & heredoc
    • Using Sponge linux utility

    Using Cat & Heredoc

    A very simple way to achive in-place editing with jq using cat tool is to understand linux redirection, what we will do is to apply jq to the file, then redirect the output to cat and then finally redirect the output to the same filename as shown below.

    $ cat <<< jq '.' unformatted.json > unformatted.json
    

    Explanation

    1. <<< – achieves what is called a cat heredoc. It helps to send multilines into the cat tool
    2. > – This achieves the second redirection by redirecting the output of the cat tool into the filename supplied.

    Using Sponge Linux Utility

    This is a simpler workflow and easier to reason through if you are not a big fan of linux redirections and pipes. Firstly, you need to install the sponge tool by doing the following.

    #Debian / Ubuntu
    apt-get install moreutils
    
    # Fedora flavours
    dnf install moreutils
    
    # Windows (WSL2)
    sudo apt-get update sudo apt-get install moreutils

    Then run the following command

    $ jq '.' unformatted.json | sponge unformatted.json
    

    Both of the options explained above, will prettify the file in place and generate the output shown below

    {
      "order_num": "O2012019231a",
      "order_date": "2012-06-27",
      "order_id": 21934,
      "order_item": [
        {
          "product_id": 20933,
          "quantity": 3,
          "price": 36000,
          "product_name": "Thingamagic 2000",
          "unit_price": 12000
        },
        {
          "product_id": 10366,
          "quantity": 1,
          "price": 100,
          "product_name": "Super Duper Blooper",
          "unit_price": 100
        }
      ]
    }