Hacking Lab Setup with Kali Linux
local graph
Chapter 2 — Hacking Lab Setup with Kali Linux
Section titled “Chapter 2 — Hacking Lab Setup with Kali Linux”In this chapter, you will learn how to set up your personal cybersecurity playground—a safe, controlled environment where you can practice hacking techniques, experiment with tools, and deepen your understanding of vulnerabilities without the risk of legal consequences or real-world system damage.
We will cover the necessary hardware and software requirements for your ethical hacking lab, including the use of virtualization software like VirtualBox and the installation of Kali Linux and Metasploitable 2. You will also discover how to configure a host-only network between your virtual machines, as well as explore an automated approach using Vagrant to set up our lab. By the end of this chapter, you will have a fully functional ethical hacking lab ready to support your security research and penetration testing activities.
In this chapter, we are going to cover the following main topics:
- Introduction to Kali Linux and its tools
- Manually setting up your ethical hacking lab
- Automating lab setup with Vagrant
Technical requirements
Section titled “Technical requirements”For this chapter, you will need the following:
- A windows host computer with a CPU that supports virtualization (you can check this in the Task Manager on Windows)
- At least 6-8 GB of RAM (16 GB or more is recommended for running multiple VMs)
- Oracle VirtualBox (free virtualization software)
- Vagrant (optional, for automating the lab setup)
- A code editor like Visual Studio Code but feel free to use any editor of your choice.
The code for this chapter is available in the book’s GitHub repository.
Introduction to Kali Linux and its tools
Section titled “Introduction to Kali Linux and its tools”Kali Linux is a popular and powerful Linux distribution explicitly designed for cybersecurity professionals, penetration testers, and ethical hackers. It is a comprehensive platform that provides a wide range of tools and utilities for various security testing and hacking tasks.
Kali Linux is a Debian-based distribution, which means it is built upon the solid foundation of the Debian Linux distribution. This ensures that Kali Linux inherits the stability, reliability, and extensive package ecosystem of Debian while also providing a specialized set of security-focused tools and utilities.
One of the primary advantages of Kali Linux is its extensive collection of pre-installed security tools. These tools cover a wide range of cybersecurity domains, including:
- Vulnerability assessment: Kali Linux includes powerful tools like Nmap, Metasploit, and Burp Suite, which can be used to scan networks, identify vulnerabilities, and perform in-depth security assessments.
- Wireless security: Kali Linux provides a robust set of wireless security tools, such as Aircrack-ng, Kismet, and Reaver, which can be used to assess the security of wireless networks and perform wireless penetration testing.
- Forensics and reverse engineering: Kali Linux includes tools like Wireshark, Volatility, and IDA Pro, which can be used for forensic analysis, malware analysis, and reverse engineering of software.
- Password cracking: Kali Linux includes various password-cracking tools, such as John the Ripper, Hashcat, and Hydra, which can be used to assess the strength of passwords and perform password-based attacks.
- Web application testing: Kali Linux provides a range of tools for web application testing, including OWASP ZAP, Nikto, and w3af, which can be used to identify and exploit vulnerabilities in web-based applications.
In addition to these pre-installed tools, Kali Linux also provides a robust package management system, which allows users to install additional tools and utilities as needed. This makes Kali Linux a highly customizable and extensible platform for cybersecurity professionals.
One of Kali Linux’s key features is its live USB/live ISO functionality. This allows users to boot Kali Linux directly from a USB drive or a live CD/DVD without installing it on the host system. This is particularly useful for performing security assessments on the go or in scenarios where the user cannot or does not want to install Kali Linux directly on the target system.
Overall, Kali Linux is a comprehensive and powerful Linux distribution that has become a go-to choice for cybersecurity professionals and ethical hackers. Its extensive collection of security tools, live USB/live ISO functionality, and Debian-based foundation make it a versatile and reliable platform for security testing, penetration testing, and ethical hacking activities.
Manually setting up your ethical hacking lab
Section titled “Manually setting up your ethical hacking lab”An ethical hacking lab is your own personal cybersecurity playground. It’s a safe, controlled environment where you can practice hacking techniques, experiment with tools, and deepen your understanding of vulnerabilities—all without the risk of breaking laws or harming real-world systems.
Why set up a local lab?
Section titled “Why set up a local lab?”Here are a few compelling reasons why you should set up your own local lab:
- Cost-effectiveness: The primary advantage of a virtual hacking lab is its cost-effectiveness. Building your own lab can be done entirely for free, without the need for any paid services or external resources. This accessibility enables you to dive into the world of ethical hacking without financial constraints.
- Customization: A virtual lab offers unparalleled flexibility. You can customize the environment to suit your specific learning needs, experiment with various vulnerable machines and network configurations, and even chain them together to practice techniques like pivoting. This level of customization is challenging to achieve with paid services or public platforms.
- Hands-on experience: No substitute for practicing attacks in a realistic environment.
- Unlimited time to work on your targets: A virtual lab provides you with unlimited time to work on your targets. You can leave the vulnerable machines running, save their state, and resume your practice sessions whenever convenient, allowing for an immersive and uninterrupted learning experience.
Before we dive into setting up your lab, let’s discuss the necessary preparations.
Preparation
Section titled “Preparation”Let’s gather the necessary tools before diving in, both hardware and software.
In order to set up an ethical hacking lab, you’ll need the following hardware components:
- CPU: Your host computer’s CPU should support virtualization. You can check this by opening the Task Manager on Windows (ctrl+shift+esc), clicking the performance tab, and verifying that Virtualization is enabled as shown in the following figure:

Figure 2.1 – Windows Task Manager
Figure 2.1 – Windows Task Manager
In our lab setup, we’ll assign 2 CPUs to the attacker machine and 1 CPU to the target machine. This should work well on most modern systems, but you can adjust these values depending on your host machine’s capabilities. If you’re unsure how many CPUs to allocate, a good starting point is to check how many cores your system has (shown in the Task Manager), and avoid assigning more than half of them to virtual machines to keep your host system responsive.
- RAM: For a basic setup, you’ll need at least 6-8GB of RAM. However, as you scale up your lab by running multiple virtual machines, you’ll want to have at least 16GB of RAM to ensure smooth performance.
- Storage: A high-capacity hard drive or solid-state drive (SSD) will provide the necessary storage space to accommodate your virtual machines and any data generated during your hacking exercises.
- External Wi-Fi adapter (optional): If you want to experiment with wireless attacks, you might need an adapter that supports monitor mode. Once you have the necessary hardware in place, you’ll need to install the required software. Software required for ethical hacking lab setup
On the software side, you’ll need the following:
- Virtualization software: This enables you to create virtual machines. Popular options include:
- Oracle VirtualBox (free)
- VMware Workstation Player (free for personal use)
- Oracle VirtualBox (free)
In this lab setup, we are going to make use of Oracle VirtualBox.
- Attacker operating system: Kali Linux is packed with hacking tools.
- Vulnerable targets: Intentionally insecure machines to practice attacking.
For the examples in this book, we are going to make use of Metasploitable 2.
Setting up your virtual lab manually
Section titled “Setting up your virtual lab manually”With the hardware and software requirements covered, let’s dive into setting up the virtual lab manually. We begin setting up your virtual lab by installing VirtualBox on your computer. Here is what our lab would look like:

Figure 2.2 – Lab overview
Figure 2.2 – Lab overview
Installing VirtualBox on your computer
Section titled “Installing VirtualBox on your computer”VirtualBox is powerful virtualization software that allows you to run multiple operating systems simultaneously on your computer. It is available for Windows, macOS, Linux, and other platforms. For our lab setup, I’ll walk you through the process of installing VirtualBox on a Windows system, but the steps are similar for other operating systems.
- To begin, go to the VirtualBox downloads page (https://www.virtualbox.org/wiki/Downloads) and select the package that matches your operating system (in case you are not using Windows). For a Windows computer, you’ll want to choose the Windows hosts option:

Figure 2.3 – Download VirtualBox page
Figure 2.3 – Download VirtualBox page
- Double-click the VirtualBox installer file once the download is complete and follow the on-screen instructions. The standard installation settings should be fine for most users:

Figure 2.4 – VirtualBox installation wizard
Figure 2.4 – VirtualBox installation wizard
The next step is to download and install the Kali Linux operating system.
Downloading and installing Kali Linux
Section titled “Downloading and installing Kali Linux”There are two ways to get Kali Linux running in VirtualBox: using a pre-configured virtual machine image, or manually installing it from an ISO file.
The easiest method is to download the Kali Linux VirtualBox image file from the Kali Linux website.
- Go to the Kali Linux download page (https://www.kali.org/get-kali/#kali-platforms). Choose the Virtual Machines option.

Figure 2.5 – Kali Linux download page
Figure 2.5 – Kali Linux download page
- Next, you will find out that Kali Linux supports different virtualization platforms. In our case, we will choose the VirtualBox options. So go ahead and click the big Download card and your download will start automatically:

Figure 2.6 – Virtualization platforms
Figure 2.6 – Virtualization platforms
- Once the file has been downloaded, extract the contents using a tool like WinRAR or 7-Zip.

Figure 2.7 – WinRAR extracting Kali Linux
Figure 2.7 – WinRAR extracting Kali Linux
- Then go to the folder where you extracted Kali Linux and double-click the extracted
.vboxfile to open it in VirtualBox.
Figure 2.8 – Extracted Kali Linux
Figure 2.8 – Extracted Kali Linux
- The Kali Linux virtual machine will now appear in your VirtualBox manager, ready to be started.

Figure 2.9 – Kali Linux in VirtualBox
Figure 2.9 – Kali Linux in VirtualBox
The default login credentials for this pre-configured image are:
- Username:
kali - Password:
kali
And that’s it! You now have a fully functional Kali Linux system running inside your VirtualBox environment. Feel free to navigate around and get familiar with it. The Kali Linux desktop should look similar to the following figure:

Figure 2.10 – A running Kali Linux operating system
Figure 2.10 – A running Kali Linux operating system
We now have Kali Linux installed. Let’s download and install a vulnerable target machine to practice on.
Downloading and installing Metasploitable 2
Section titled “Downloading and installing Metasploitable 2”Now that we have our Kali Linux up and running, it’s time to install Metasploitable 2.
- Visit the official Metasploitable 2 website or repository (https://sourceforge.net/projects/metasploitable/files/Metasploitable2/) and download the latest version of the virtual machine image.

Figure 2.11 – Metasploitable download page
Figure 2.11 – Metasploitable download page
- Once the download is complete, locate the compressed file you downloaded and extract it.

Figure 2.12 – Extracting Metasploitable using WinRAR
Figure 2.12 – Extracting Metasploitable using WinRAR
- Inside the folder, you will see a
.vmdkfile. A.vmdkfile requires a few extra steps to get it up and running as compared to the.vboxfile:
Figure 2.13 – Extracted Metasploitable
Figure 2.13 – Extracted Metasploitable
- Open VirtualBox and click on New.

Figure 2.14 – Adding a new Metasploitable virtual machine
Figure 2.14 – Adding a new Metasploitable virtual machine
You will see a pop-up window similar to the one in the following figure. In the window, provide the virtual machine name, type, and version as shown:

Figure 2.15 – Virtual machine name and operating system
Figure 2.15 – Virtual machine name and operating system
- Once you’ve done this, hit the Next button. You will be prompted to customize specific hardware settings, such as EFI, processor, and base memory. It’s recommended that you keep the default settings unless you have specific requirements. Hit Next again.

Figure 2.16 – Hardware settings
Figure 2.16 – Hardware settings
- Select Use an existing virtual hard disk file on the Hard Disk screen and click the folder icon. A Hard Disk Selector window will pop up. Click Add and navigate to the folder where you extracted Metasploitable and choose the
.vmdkfile:
Figure 2.17 – Virtual hard disk settings
Figure 2.17 – Virtual hard disk settings
Finally, click Finish and you’ll see the Metasploitable 2 virtual machine listed in the VirtualBox manager:

Figure 2.18 – Virtual Machine summary
Figure 2.18 – Virtual Machine summary
- Simply double-click the virtual machine to boot it up.
You’ll be prompted to enter the login credentials when the virtual machine has finished loading. The default username is msfadmin, and the default password is also msfadmin.

Figure 2.19 – Metasploitable running
Figure 2.19 – Metasploitable running
Remember to use the Metasploitable 2 VM responsibly and only for legitimate security research and testing purposes, as it is intentionally vulnerable and should not be exposed to any production environment.
Setting up network adapter
Section titled “Setting up network adapter”When working with virtual machines, it’s often desirable to create an isolated network environment where the VMs can communicate with each other without directly interacting with the host machine’s network or the broader internet. This is where the host-only network adapter in VirtualBox comes into play.
By creating a host-only network adapter, we establish a private network between the virtual machines, allowing them to communicate securely and without the risk of impacting any external networks or systems. This is particularly important when working with a vulnerable virtual machine like Metasploitable 2, as it ensures that any potential security breaches or attacks are contained within the isolated environment.
Let’s walk through the process of setting up the host-only network adapter:
- To set up the host-only network adapter, start by opening the VirtualBox application on your host machine. In the VirtualBox menu, navigate to File > Tools and select the Network Manager, or simply use the shortcut key CTRL + H.

Figure 2.20 – VirtualBox tools settings
Figure 2.20 – VirtualBox tools settings
- Click the Host-only Networks tab, then click the Create button to add a new host-only adapter.

Figure 2.21 – Network Manager settings
Figure 2.21 – Network Manager settings
- Once the network adaptor has been created, select it and choose Configure Adapter Manually (see Figure 2.21). Change the automatically generated IP Address to
192.168.5.1and leave the IPv4 Network Mask untouched. - We also need to enable the DHCP Server on our network adaptor to allow automatic IP address allocation. Click on the DHCP Server tab and check Enable Server. Change the IP configurations to the following:
- Server Address:
192.168.5.2- Server Mask:
255.255.255.0 - Lower Address Bound:
192.168.5.3 - Upper Address Bound:
192.168.5.254
- Server Mask:
- Server Address:
- Once you are done, click on Apply to create our host-only network adapter:

Figure 2.22 – Configuring DHCP server
Figure 2.22 – Configuring DHCP server
- Next, configure the network settings for the Kali Linux virtual machine. Select the Kali Linux VM in the VirtualBox Manager, click the Settings button, and navigate to the Network tab. Here, you’ll want to keep the first network adapter in its default NAT setting to maintain internet connectivity for the Kali Linux VM. For the second network adapter, check Enable Network Adapter. Under the Attached to dropdown, select Host-only Adapter and choose the host-only adapter you just created from the Name dropdown. This will allow the Kali Linux VM to communicate with the Metasploitable 2 VM over the host-only network. Click OK to save the settings:

Figure 2.23 – Applying host-only adapter to Kali Linux
Figure 2.23 – Applying host-only adapter to Kali Linux
- For the Metasploitable 2 virtual machine, the configuration is slightly different. Select the Metasploitable 2 VM in the VirtualBox Manager, click the Settings button, and navigate to the Network tab. Under the Attached to dropdown, select Host-only Adapter and choose the same host-only adapter you selected for the Kali Linux VM. Since Metasploitable 2 is an intentionally vulnerable machine, it does not require internet access, so we can safely isolate it within the host-only network. Click OK to save the settings.

Figure 2.24 – Applying Host-only Adapter to Metasploitable
Figure 2.24 – Applying Host-only Adapter to Metasploitable
By setting up the host-only network adapter, we ensure that the Kali Linux and Metasploitable 2 VMs can communicate with each other over a private network. While the Kali Linux VM maintains its internet connectivity through the first network adapter, the Metasploitable 2 VM is isolated within the host-only network, preventing any unintended consequences or potential security breaches from affecting external systems. This configuration creates a secure environment for security testing, where you can safely interact with the vulnerable Metasploitable 2 machine without exposing it to the broader internet.
With the host-only network in place, you can now proceed to start the virtual machines and begin exploring.
Automating lab setup with Vagrant
Section titled “Automating lab setup with Vagrant”Manually setting up a testing lab can be tedious and time-consuming. However, as this book focuses on automation, we have a solution: Vagrant.
Vagrant is an open-source software tool that enables developers and security professionals to create and manage lightweight, reproducible, and portable development environments. By using a Vagrantfile, you can define the configuration of your virtual machines, including the operating system, network settings, CPU, memory, and other parameters, allowing you to spin up consistent environments across different systems quickly.
This section walks you through the process of creating a Vagrant environment that provisions both a Kali Linux VM and a Metasploitable 2 VM, with a host-only network established between them.
Important note – Prerequisites
Ensure you have Vagrant installed on your host system. You can download it from the official Vagrant website (https://www.vagrantup.com/downloads).
We also need VirtualBox, as Vagrant will use it as the default provider for creating virtual machines.
A code editor of your choice that will help us manage the Vagrantfile. For demonstration, we will be using Microsoft Visual Studio Code (VS Code).
We will use the following steps:
- Creating a new Vagrant project directory: Create a new project in VS Code and name it
vagrant-lab. In the VS Code terminal, navigate to the directory of your project and run the following command to initialize a new Vagrant environment:vagrant init
This will create a Vagrantfile in the current directory, which is the central configuration file for your Vagrant setup.
- Configuring the Vagrantfile: Open the generated
Vagrantfileand replace all its contents with the followingVagrantfileconfiguration:# -*- mode: ruby -*-# vi: set ft=ruby :Vagrant.configure("2") do |config|# Kali Linux VMconfig.vm.define "kali" do |kali|kali.vm.box = "kalilinux/rolling"kali.vm.hostname = "kali"kali.vm.network "private_network", ip: "192.168.52.101"kali.vm.provider "virtualbox" do |vb|vb.name = "Kali Linux"vb.memory = 4048vb.cpus = 2endend# Metasploitable 2 VMconfig.vm.define "metasploitable2" do |metasploitable2|metasploitable2.vm.box = "Sliim/metasploitable2"metasploitable2.vm.box_version = "2.0.0"metasploitable2.vm.hostname = "metasploitable2"# Run the inline shell script for configurationmetasploitable2.vm.provision "shell", inline: <<-SHELLif ! grep -q "metasploitable2" /etc/hosts; thenecho "127.0.0.1 metasploitable2 metasploitable2" | sudo tee -a /etc/hostsfiSHELLmetasploitable2.vm.network "private_network", ip: "192.168.52.102"# Disable shared foldersmetasploitable2.vm.synced_folder "", "/vagrant", disabled: true# SSH Configuration for Metasploitable 2metasploitable2.ssh.sudo_command = "sudo %c"metasploitable2.ssh.username = "msfadmin"metasploitable2.ssh.password = "msfadmin"metasploitable2.vm.provider "virtualbox" do |vb|vb.name = "Metasploitable 2"vb.memory = 1024vb.cpus = 1endendend
This Vagrantfile configuration sets up the following:
- A Kali Linux virtual machine with 4GB of RAM and 2 CPUs, assigned the IP address
192.168.52.101. - A Metasploitable 2 virtual machine with 1GB of RAM and 1 CPU, assigned the IP address
192.168.52.102.
Let’s go ahead and run our virtual machines.
- Bringing up the virtual machines: Run the following command to start the Kali Linux and Metasploitable 2 virtual machines:
vagrant up
This command will download the necessary base boxes, create the virtual machines, and start them according to the Vagrantfile configuration.
Important note
This command should be run inside our project folder where the Vagrantfile is.
It is important to note that when running the vagrant up command, you might encounter the following error:
There was an error loading a Vagrantfile. The file being loaded and the error message are shown below. This is usually caused by a syntax error.Path: <provider config: vmware_desktop>Line number: 21Message: NameError: undefined local variable or method \`v' for main:Object v.force_vmware_license = "workstation" ^This error typically occurs due to an unnecessary reference to VMware in your Vagrantfile. To resolve it, follow these steps:
- Navigate to the directory where your Vagrant boxes are stored, typically located at:
%USERPROFILE%\\.vagrant.d\\boxes
- Open the folder for the Kali Linux box (
kalilinux-VAGRANTSLASH-rolling). Inside, locate the folder for the box version you downloaded (for example,2024.3.0), and then navigate toamd64 > virtualbox.
Inside the virtualbox folder, you’ll find the Vagrantfile. Open it and remove the following lines, as they configure VMware, which is unnecessary when using VirtualBox as your provider:
config.vm.provider :vmware_desktop do |vmware| v.force_vmware_license = "workstation" vmware.gui = true vmware.vmx["ide0:0.clientdevice"] = "FALSE" vmware.vmx["ide0:0.devicetype"] = "cdrom-raw" vmware.vmx["ide0:0.filename"] = "auto detect"end- Save and close the file.
Now, when you run the vagrant up command again, the error should be resolved, and the virtual machine will boot using VirtualBox as the provider.
The default username and password for the Kali box are both vagrant, while for the Metasploitable box, the default credentials are msfadmin for both the username and the password.
- Once the
vagrant upcommand has completed, you can check the status of the VMs by running:vagrant status
You should see both the kali and metasploitable2 VMs in the running state.
The simplest and easiest way to access the VMs is by accessing them through VirtualBox.
We can also connect to our VMs via SSH using the hostnames we configured in the Vagrant file. To connect to the Kali Linux VM, run:
vagrant ssh kaliSimilarly, to connect to the Metasploitable 2 VM, run:
vagrant ssh metasploitable2- If you want to stop the virtual machines, run the following command:
vagrant halt
To reboot, simply run:
vagrant reload- If you feel like you no longer need the virtual machine or you want to restart the provisioning, use the destroy argument with the
vagrantcommand:vagrant destroy
And all your virtual machines will be deleted from the system. You can then run vagrant up to provision them again.
By following these steps, you have successfully automated the setup of a testing lab environment using Vagrant. This configuration creates a Kali Linux VM and a Metasploitable 2 VM with a host-only network between them, providing a consistent and reproducible environment for security testing and penetration practice. This automated approach saves time, ensures consistency across different systems, and allows you to easily recreate the lab environment whenever needed.
We’ve only scratched the surface of what you can do with Vagrant—there’s a lot more to explore. If you’re interested in learning more, check out the official documentation: https://developer.hashicorp.com/vagrant/docs
Summary
Section titled “Summary”That’s it! We have come to the end of this chapter on setting up your hacking playground with Kali Linux. I hope you enjoyed reading it as much as I enjoyed writing it.
This chapter covered how to set up your own ethical hacking lab using free tools like VirtualBox, Kali Linux, and Metasploitable 2. You learned to manually configure virtual machines and networking to create a safe, isolated environment for practicing hacking techniques and security research. Additionally, we explored using Vagrant to automate the provisioning process, allowing you to quickly spin up consistent lab environments across different systems. Setting up a personal hacking lab offers crucial benefits—customizing your learning environment, working on targets without constraints, and gaining hands-on experience in a realistic setting.
Now, as we move forward, the next chapter will take you on a deeper dive into the fundamental concepts of shell scripting. You will learn how to read input from the user, whether it’s from the keyboard or from files. You will also learn how to display output on the screen. Additionally, we’ll explore redirecting input and output.
Questions
Section titled “Questions”- What is the primary advantage of using Kali Linux’s pre-configured VirtualBox image over manually installing from an ISO file?
- It provides better security features. 2. It is faster and requires fewer setup steps. 3. It includes more hacking tools. 4. It automatically configures all network settings.
- Why should Metasploitable 2 never be exposed to production environments or the broader internet?
- It consumes too much bandwidth when connected to the internet. 2. It is intentionally vulnerable and could be easily exploited by attackers. 3. It requires a license for internet connectivity. 4. It will automatically attack other systems on the network.
- Which Linux distribution serves as the foundation for Kali Linux?
- Ubuntu 2. Red Hat 3. Debian 4. Fedora
- In the VirtualBox network configuration described in this chapter, why does Kali Linux require two network adapters, while Metasploitable 2 only needs one?
- Kali Linux needs more processing power for network operations. 2. Kali Linux needs internet access (via NAT) and host-only network communication, whereas Metasploitable 2 should be isolated. 3. Metasploitable 2 is incompatible with dual network adapters. 4. The second adapter provides backup connectivity in case the first one fails.
- Which Vagrant command is used to stop running virtual machines without destroying them?
- vagrant stop 2. vagrant halt 3. vagrant pause 4. vagrant suspend
Answers
Section titled “Answers”- b.
- b.
- c.
- b.
- c.