Common commands for setting up, updating and fixing a Kali Linux pentesting VM.
A collection of the setup and maintenance commands I end up running on every fresh Kali VM, gathered in one place instead of spread across a dozen bookmarks.
Add the Kali repository (Debian)
echo 'deb http://kali.download/kali kali-rolling main non-free contrib' > /etc/apt/sources.listIf apt complains about a missing or expired signing key:
wget https://archive.kali.org/archive-key.asc -O /etc/apt/trusted.gpg.d/kali-archive-keyring.asc# orsudo apt-key adv --keyserver hkp://keys.gnupg.net --recv-keys 7D8D0BF6Add the Kali repository (Ubuntu)
Ubuntu needs the repository added as its own source file and pinned to a low priority, so Kali packages are only installed when you explicitly ask for them.
sudo apt update && sudo apt upgrade && sudo apt dist-upgradesudo apt install curl wget gnupg git
wget -q -O - https://archive.kali.org/archive-key.asc | sudo apt-key addsudo sh -c "echo 'deb https://http.kali.org/kali kali-rolling main non-free contrib' > /etc/apt/sources.list.d/kali.list"sudo sh -c "echo 'Package: *' > /etc/apt/preferences.d/kali.pref; echo 'Pin: release a=kali-rolling' >> /etc/apt/preferences.d/kali.pref; echo 'Pin-Priority: 50' >> /etc/apt/preferences.d/kali.pref"
wget http://http.kali.org/kali/pool/main/k/kali-archive-keyring/kali-archive-keyring_2022.1_all.debsudo dpkg -i kali-archive-keyring_2022.1_all.debrm kali-archive-keyring_2022.1_all.deb
sudo apt update --fix-missingsudo apt --fix-broken installsudo apt upgrade(Adapted from Linux Carnival’s Kali-on-Ubuntu guide.)
Check the Kali version and update
grep VERSION /etc/os-releaseuname -vuname -rcat /etc/apt/sources.list # Should show the kali-rolling repository
sudo apt updatesudo apt full-upgrade -yFix the apt-key deprecation warning (Debian/Ubuntu)
cd /etc/aptsudo cp trusted.gpg trusted.gpg.dIncrease Kali VM disk space
sudo fdisk -l # Check total disk size and partition sizesgparted # Right-click the swap partition and select "Swap off", then right-click the extended # partition and select "Resize/Move" to grow it into the freed space
sudo thunar # Open the file manager as root, if you need it while resizingSee this walkthrough for the full GParted steps.
Remove or change the kernel version
uname -sr # Currently running kerneldpkg -l | grep linux-image # All installed kernel imagesdpkg --get-selections | grep linux-image
sudo apt remove --purge linux-image-6.5.0-kali1-amd64 # Remove a kernel you no longer needsudo update-grub2To pin a specific kernel and stop apt from removing it on the next update:
sudo apt-mark hold linux-image-6.3.0-kali1-amd64sudo apt-mark hold linux-image-amd64
apt-mark showhold # List held packagessudo apt-mark unhold linux-image-6.3.0-kali1-amd64 # Remove the holdsudo apt autoremove --purge linux-image-6.3.0-kali1-amd64Fix a slow “Loading initial ramdisk” boot
Booting should reach this stage in a couple of seconds. If it’s taking much longer, the initramfs image is larger than it needs to be.
sudo nano /etc/initramfs-tools/initramfs.conf # Set MODULES=depsudo update-initramfs -uPackage management tricks
sudo apt-get update --fix-missing # Fix repeated 404 errors when installing packagesapt list --installed # List everything installed on the systemsudo apt --purge remove burpsuite # Fully remove a package, including its config



