If it’s really broken, it’s even less fun. Even if someone knows what they’re doing, it sometimes takes a certain level of art and definitely skill to fix a broken packaging system. The term “art” is quite applicable as you get a sense or feeling while you’re running through various troubleshooting steps—especially if you’ve done this often—whether or not something will work out during a reboot.
The risk is always that the packaging system is so badly broken that if you reboot, your Linux instance might not come up again, and that further and potentially still more complex troubleshooting will be required. I would like to state at this point that all of this can be easily avoided by executing a few simple commands—but regrettably, that’s not the case. The fact is, the more you know, the easier it will be to fix, and the more confidence you’ll have to figure out whether you’re ready for a system reboot or not.
This is where we come to dpkg. Dpkg is a package management tool that allows you to install, remove, and manage packages on your Linux machine. It can also help you troubleshoot issues with APT (the default package manager in Kali) when it becomes broken. In this post, we’ll show you how to safely add additional package repositories to your Kali installation, how to upgrade and downgrade them, and how to ensure all of these repositories live in harmony.list directly. For each new package repository you add to your system, create a new file with a descriptive name (like debian-unstable) in the directory /etc/apt/sources.list.d/. This way, if something goes wrong or you want to remove that particular source, it’s easy to do so without affecting other sources.
For example:
# This script adds a new package repository for Debian Unstable (sid) to the system.
# To add a new package repository, a new file with a descriptive name must be created in the directory /etc/apt/sources.list.d/.
# This allows for easy removal or troubleshooting of the specific source without affecting other sources.
# For example, to add the kali-rolling repository from http.kali.org, the following command can be used:
# echo "deb http://http.kali.org/kali kali-rolling main contrib non-free" | sudo tee -a /etc/apt/sources.list.d/kali-bleeding-edge.list
# The above command uses the "echo" command to print the specified repository information and the "tee" command to append it to the file kali-bleeding-edge.list in the sources.list.d directory.
# The "sudo" command is used to run the command with root privileges, as modifying system files requires administrative permissions.
# 1. The repository name should be descriptive and match the repository being added. In this case, it should be named "kali-rolling" instead of "kali-bleeding-edge".
# 2. The repository URL should be enclosed in double quotes to prevent any special characters from being interpreted.
# 3. The "-a" flag in the "tee" command is unnecessary as we are creating a new file, not appending to an existing one.
# 4. The "sudo" command should be placed before the "tee" command to ensure the entire command is run with root privileges.
#!/bin/bash
# Adding a new package repository for Debian Unstable (sid)
echo "deb "http://http.kali.org/kali" kali-rolling main contrib non-free" | sudo tee /etc/apt/sources.list.d/kali-rolling.list
In this example, we’re adding a new package repository for Kali Rolling (the latest stable release of Kali) and the Debian Unstable (sid) branch to our sources list. We’re also specifying that we want to install packages from the main, contrib, and non-free sections of these repositories.
To ensure all package sources are properly configured, run:
#!/bin/bash # This line specifies the interpreter to be used for executing the script
sudo apt update # This command updates the package lists from all configured repositories
sudo apt upgrade -y # This command upgrades all installed packages to their latest versions without prompting for confirmation
# The following commands add the Kali Rolling repository and the Debian Unstable (sid) branch to the sources list
sudo echo "deb http://http.kali.org/kali kali-rolling main contrib non-free" >> /etc/apt/sources.list
sudo echo "deb http://http.debian.net/debian sid main contrib non-free" >> /etc/apt/sources.list
sudo apt update # This command updates the package lists again to include the newly added repositories
# The following commands install the necessary packages from the newly added repositories
sudo apt install -y kali-linux-default # Installs the default Kali Linux tools
sudo apt install -y kali-linux-large # Installs additional Kali Linux tools
sudo apt install -y kali-linux-everything # Installs all Kali Linux tools
# The following commands remove any unnecessary packages and clean up the system
sudo apt autoremove -y # Removes any packages that are no longer needed
sudo apt clean # Removes any downloaded package files from the local repository
sudo apt autoclean # Removes any outdated package files from the local repository
This will download and install any available updates for your system as well as any new packages from the newly added source(s).
Upgrading and Downgrading Packages with APT
When you want to upgrade or downgrade a package, use the following syntax:
# This script is used to upgrade or downgrade packages using the APT package manager.
# Upgrading a specific package (e.g. sqlmap) to the latest version from the kali-bleeding-edge repository.
sudo apt install sqlmap/kali-bleeding-edge
# Downgrading a specific package (e.g. sqlmap) to version 1.0.9 from the kali-rolling/main repository.
sudo apt install sqlmap=1.0.9-1 kali-rolling/main
# Explanation:
# The first command uses the "upgrade" option to update the specified package to the latest version from the kali-bleeding-edge repository.
# The second command uses the "install" option to install the specified package from the kali-rolling/main repository, specifying the desired version using the "=" symbol. This will downgrade the package to the specified version.
# The "sudo" command is used to run the commands with root privileges.
# The "apt" command is used to manage packages on the system.
# The "/kali-bleeding-edge" and "/kali-rolling/main" parts specify the repository from which the package will be downloaded.
# The package name and version are specified after the repository, separated by a forward slash ("/").
# The "-1" at the end of the second command specifies the package release number.
# The "main" part in the second command specifies the component of the repository from which the package will be downloaded.
In this example, we’re upgrading the sqlmap package from Kali Rolling and downgrading it to version 1.0.9 using the -y flag for “yes” (i.e. automatically answer any prompts).
To ensure all packages are properly configured, run:
# This line updates the package lists and ensures that the system has the latest version information.
sudo apt update
# This line upgrades all installed packages to their latest versions, using the -y flag to automatically answer "yes" to any prompts.
sudo apt upgrade -y
# This line installs the specified package, in this case, sqlmap, from the Kali Rolling repository.
sudo apt install sqlmap
# This line downgrades the sqlmap package to version 1.0.9.
sudo apt install sqlmap=1.0.9
# This line ensures that all packages are properly configured after the downgrade.
sudo apt install -f
This will download and install any available updates for your system as well as any new or upgraded packages from the newly added source(s).
Troubleshooting APT Issues with DPKG
If you encounter issues with APT (e.g. broken dependencies), use dpkg to troubleshoot them. For example:
#!/bin/bash
# This script removes the sqlmap package using dpkg instead of apt, without removing its configuration files.
sudo dpkg -r sqlmap # Uses the dpkg command with the -r flag to remove the sqlmap package.
# This command verifies that the sqlmap package is properly installed and configured.
sudo dpkg --verify sqlmap # Uses the dpkg command with the --verify flag to check the status of the sqlmap package.
In this example, we’re removing the sqlmap package using dpkg instead of apt (i.e. without removing its configuration files) and verifying that it’s properly installed and configured.
To ensure all packages are properly removed or verified, run:
#!/bin/bash # This line specifies the interpreter to be used for executing the script
# This script removes the sqlmap package using dpkg instead of apt and verifies its installation and configuration.
# To ensure all packages are properly removed or verified, run:
# bash script_name.sh
# The following command creates a list of all installed packages and saves it in a temporary file named "packages.txt"
sudo dpkg --get-selections | grep -v deinstall > /tmp/packages.txt
# The for loop iterates through each package in the "packages.txt" file and removes it using dpkg -r command
for package in $(cat /tmp/packages.txt); do
sudo dpkg -r $package # The -r flag removes the package without removing its configuration files
done
# The following command removes the temporary file "packages.txt"
rm /tmp/packages.txt
This will list all installed packages (except for those that are marked as “deinstall” or “purge”) and remove them using dpkg instead of apt.