A PowerShell script to simplify adding entries to Windows and WSL (Kali Linux) hosts files, with automatic backups and duplicate checks. Perfect for CTF setups, local development, or network testing.
- Cross-Platform Support: Updates hosts files for both Windows and WSL (Kali Linux) (See below if you want other distros).
- Automatic Backups: Creates
hosts.bakon first run for easy recovery. - Duplicate Prevention: Skips existing entries to avoid clutter.
- Admin-Friendly: Auto-elevates to admin rights when needed.
-
Prerequisites:
- Windows 10/11 with PowerShell 5.1+.
- WSL with Kali Linux installed (default distro name:
kali-linux).
-
Clone the Repository:
git clone https://github.com/ymuuuu/AddToHosts.git cd AddToHosts
Run the script as Administrator:
.\AddTo-Hosts.ps1Example Workflow:
Enter IP (e.g., 10.10.11.55): 10.10.11.123
Enter hostname (e.g., titanic.htb): ctf-target.htb
[*] Windows backup already exists: C:\Windows\System32\drivers\etc\hosts.bak
[*] WSL backup already exists: /etc/hosts.bak
[+] Windows hosts: Added '10.10.11.123 ctf-target.htb'
[+] Added to WSL Kali hosts: 10.10.11.123 ctf-target.htb
Press any key to exit...
Copy-Item "$env:SystemRoot\System32\drivers\etc\hosts.bak" "$env:SystemRoot\System32\drivers\etc\hosts" -Forcesudo cp /etc/hosts.bak /etc/hostsYes! You can easily modify it to work with any WSL distribution (e.g., Ubuntu, Debian). Hereโs how:
- Check Your WSL Distribution Name Run this in PowerShell to list installed WSL distros:
wsl -l -qExample output:
Ubuntu
kali-linux
Debian
- Update the Script Replace kali-linux in the script with your distribution name. For example:
# For Ubuntu
wsl -d Ubuntu -u root bash -c ...
# For Debian
wsl -d Debian -u root bash -c ...- Requires admin rights to modify system files.
- Backups are created only once to prevent accidental overwrites.
- Script exits immediately on cancellation.
MIT License - See LICENSE.
Happy Hacking! ๐ฎ๐
Feel free to contribute or report issues!