USB drive). 2. Copy all of the relevant template files into this directory, making sure to remove any unnecessary data and format them correctly. For example, create a new file named `template_file` with the following content:
# Preseed file for Kali Linux installation
# Set the locale to en_US.UTF-8
d-i debian-installer/locale string en_US.UTF-8
# Set the country to US
d-i debian-installer/country string us
# Select the interface for network configuration
d-i netcfg/choose_interface select eth0
# Set the hostname for the installation
d-i netcfg/get_hostname string mykalihost
# Set the static IP address for the installation
d-i netcfg/static/ipaddress string 192.168.1.100
# Set the netmask for the static IP address
d-i netcfg/static/netmask string 255.255.255.0
# Set the gateway for the static IP address
d-i netcfg/static/gateway string 192.168.1.1
# Set the nameservers for the installation
d-i netcfg/static/nameservers string 8.8.8.8 8.8.4.4
3. Save the file with a .preseed extension (e.g., install.preseed) in your desired location (e.g., USB drive). 4. Create a script named `preseeder.sh` that extracts data from each template file and creates a preseed file in your desired location using the following code:
#!/bin/bash
# Set variables for input and output files
input_folder="/path/to/templates" # Set the path to the folder containing template files
output_file="/path/to/preseed.txt" # Set the path to the output file
if [ $# -lt 2 ]; then # Check if the number of arguments is less than 2
echo "Usage: ./preseeder.sh /path/to/template_files /path/to/output_file" # Print usage instructions
exit 1 # Exit the script with an error code
fi
# Loop through each template file and extract data using sed command
for file in "$input_folder"/*; do # Loop through each file in the input folder
if [ -f "$file" ]; then # Check if the file is a regular file
# Extract data from template file using sed command
echo "Extracting data from $file..." # Print a message indicating the file being processed
data=$(sed 's/^# //' "$file") # Use sed command to remove comments from the beginning of each line
# Remove any blank lines or comments at the beginning of each line
data=$(echo "$data" | grep -vE '^\s*$|^[[:space:]]*#[[:space:]]*') # Use grep command to remove blank lines and comments
# Split data into an array using newline as delimiter
IFS=$'\n' read -ra lines < "$data" # Use IFS to split the data into an array using newline as delimiter
# Loop through each line and write it to output file with correct format
for line in "${lines[@]}"; do # Loop through each line in the array
if [ ! -z "$line" ]; then # Check if the line is not empty
echo "d-i debian-installer/$1 \"$line\"" "$output_file" # Print the line in the correct format to the output file
# Replace placeholders (e.g., $1) with actual values using sed command
sed -i "s/\$$1\"/$(echo "$line" | sed 's/\$1/"'"${2}"'"\")/g" "$output_file" # Use sed command to replace placeholders with actual values
fi
done
fi
done
# Print success message and exit script
echo "Preseed file created successfully!" # Print a success message
5. Run the script with the following command:
#!/bin/bash
# This is a bash script that takes two arguments, a path to a template directory and a path to an output file.
# The first argument is the path to the template directory, where the script will look for template files to use.
# The second argument is the path to the output file, where the script will save the results.
# The script is executed with the following command, where the first argument is the path to the template directory and the second argument is the path to the output file:
# ./preseeder.sh /path/to/templates /path/to/output_file
if [ $# -ne 2 ]; then
# If the number of arguments is not equal to 2, an error message is displayed and the script exits.
echo "Error: Incorrect number of arguments provided."
exit 1
fi
# The script then checks if the first argument, the path to the template directory, is a valid directory.
if [ ! -d "$1" ]; then
# If the first argument is not a valid directory, an error message is displayed and the script exits.
echo "Error: The first argument must be a valid directory."
exit 1
fi
# The script then checks if the second argument, the path to the output file, is a valid file.
if [ -f "$2" ]; then
# If the second argument is a valid file, the user is prompted to confirm if they want to overwrite the file.
read -p "The output file already exists. Do you want to overwrite it? (y/n) " response
# If the user responds with "y" or "Y", the script continues. Otherwise, the script exits.
if [ "$response" != "y" ] && [ "$response" != "Y" ]; then
exit 1
fi
fi
# The script then creates the output file using the second argument provided.
touch "$2"
# The script then loops through all the files in the template directory.
for file in "$1"/*; do
# The script checks if the file is a regular file.
if [ -f "$file" ]; then
# If the file is a regular file, the contents of the file are appended to the output file.
cat "$file" >> "$2"
fi
done
# The script then displays a success message and exits.
echo "Success: Output file created with contents from template directory."
exit 0
6. The script will extract data from each template file and create a preseed file in your desired location (e.g., USB drive). 7. Boot from Kali Linux installation media, select “Install”, choose “Yes” when asked about preseeding, enter the path to your output_file, and press enter! This script can save you time and ensure consistency across all of your installations by automating the process of creating a preseed file. However, it’s essential to note that using preseeding may not always be the best option for every installation scenario. For instance, if you have specific hardware requirements or need to customize certain settings during installation, preseeding might not allow for those modifications. In such cases, it’s better to manually configure Kali Linux through the installer interface instead of relying on a preseed file.