My Complete Guide to Managing Multiple Machines with WSL and SSH

Digi
6 min readOct 21, 2024

--

This post provides a step-by-step guide on using WSL (Windows Subsystem for Linux) to streamline SSH connections across multiple machines. It covers setting up port forwarding, adding custom hostnames, and creating a simple script to easily send files between devices, boosting efficiency and productivity.

If you are like me, you have more than one machine to work from. When it comes to CTF (Capture the Flag) Cybersecurity challenges like NCL (National Cyber League) it’s crucial to take advantage of all your resources.

I am a big fan of using WSL (Windows Subsystem for Linux). Here is the scenario, I am doing a password cracking session and if you have ever done this before, you know it’s very time consuming. You can only run 1 session of Hashcat on a machine at a time. My main pc that I will refer to as mainpc has a NVIDIA RTX 4080 Super GPU, my second pc I will refer to as gamingpc has a NVIDIA RTX 3090 GPU, and lastly is my laptop that has a NVIDIA RTX 4070. The problem I am faced with using multiple machines is sharing a keyboard with KVM on mainpc and gamingpc and then swinging my chair around to use my laptop. This is not efficient. I want to run Hashcat on all these machines and monitor them with ease and manage them all on my mainpc.

The following is my guide on how I was able to connect multiple ssh sessions with WSL, add host names for each machine (so I don’t have to remember their ip addresses) and finally making a script that makes it easy to send files.

After this is all setup , I was able to send a file to another machine from mainpc with ease:

send.sh <file name you want to send> <machine name>
send.sh picture.jpg LAPTOP

The first part is setting up your WSL properly with some trickery of port forwarding. First we need to find our ip address of our host machine (this will be Windows) and then our WSL ip address. These are two entirely different ip addresses.

So, first I would get my ip address of mainpc using command prompt and type: ipconfig. At the top you will see IPv4 Address, jot that down in notepad for reference. i.e. mainpc windows 192.168.1.5.

Next, I would open WSL on mainpc and type: $ ip a | grep eth0

This ip address will look like 172.17.0.1. Next jot that down in notepad mainpc wsl 172.17.0.1.

Now, we need to setup port forwarding on windows that will redirect a random port number that we choose to hit our WSL ip address. The reason we are doing this is because we can’t reach our WSL by doing ssh user@<wsl_ip_address>. The random port number I chose is 2012.

I’m going to setup mainpc. Open PowerShell as administrator:

Now run the command

netsh interface portproxy add v4tov4 listenport=<random_port_number> listenaddress=<windows_host_ip_address> connectport=22 connectaddress=<wsl_ip_address>

For my mainpc I would be entering the following:

netsh interface portproxy add v4tov4 listenport=2012 listenaddress=192.168.1.5 connectport=22 connectaddress=172.17.0.1

Verify this is setup by running this in PowerShell:
netsh interface portproxy show all

In case you accidentally type in the wrong information and need to reset it (which happened to me), you can reset it here:
netsh interface portproxy reset

Next, we need to add a rule to Windows Firewall.

Click Inbound Rules on the left pane and then “New Rule” on the right pane.

We have added port forwarding to mainpc and a firewall to our new port 2012 for windows to accept that connection.

Now we need to make sure SSH is installed and working on mainpc.

sudo systemctl status ssh

sudo systemctl start ssh
sudo systemctl enable ssh

sudo ufw allow ssh
sudo ufw enable

sudo systemctl status ssh

You should see your ssh service Active: active

We need to make sure our sudo nano /etc/ssh/sshd_config file is setup with the following variables uncommented with the value yes.


AllowUsers kali
Port 22

PermitRootLogin
PasswordAuthentication yes
PubkeyAuthentication yes

If you are going to use your main username, in this case kali, then you don’t need to do anything for AllowUsers. However, if you make a new username for your wsl connections, you will need to enter that after kali i.e AllowUsers kali newnamehere. Make sure Port 22 is uncommented because this is your WSL ssh, remember the port is forwarding from our windows host ip address and port 2012 to this ip address and our ssh port 22.

You should be able to connect to mainpc with ssh:

$ kali@192.168.1.5 -p 2012

You would then repeat this process for the PowerShell for each and every machine. In my case, I would do this for mainpc, gamingpc and laptop.

Once these are all setup, I found it useful to create a host name for each machine instead of remembering their ip address. I used the same random port number on each machine. This is what I added on mainpc.

nano ~/.ssh/config

Host GAMINGPC
HostName 192.168.1.5
User kali
Port 2012


Host LAPTOP
HostName 192.168.1.6
User kali
Port 2012

You will want to change the HostName ip address to your windows host ip addresses for each machine.

Now I can ssh to my gamingpc by typing $ ssh GAMINGPC

One thing I will be doing a lot is sending files between machines. I can use scp (Secure Copy Protocol). Here is an example of sending an image file to my gamingpc.

$ scp /home/kali/cybersecurity.jpg GAMINGPC:/home/kali/shared

This will send cybersecurity.jpg to my gamingpc and store it at /home/kali/shared. Change this accordingly.

After using the scp method a lot, I wanted to simplify sending files with a python script that I will be storing in /usr/local/bin so I can use it globally in any directory.

#!/bin/bash

# Check if the required arguments (file and target) are provided
if [ $# -ne 2 ]; then
echo "Usage: ./send.sh <file> <target>"
echo "Example: ./send.sh hide.enc LAPTOP"
exit 1
fi

# Assign the arguments to variables
FILE=$1
TARGET=$2

# Define the paths and ports for each target
case $TARGET in
LAPTOP)
USER="kali"
HOST="192.168.1.7" # Or use the hostname if configured in ~/.ssh/config
PORT="2012"
DEST="/home/kali/"
;;
GAMETIME)
USER="kali"
HOST="192.168.1.6" # Or use the hostname if configured in ~/.ssh/config
PORT="2012"
DEST="/home/kali/"
;;
*)
echo "Unknown target: $TARGET. Use LAPTOP or GAMETIME."
exit 1
;;
esac

# Execute the SCP command
scp -P $PORT "$FILE" $USER@$HOST:$DEST

# Check if the file transfer was successful
if [ $? -eq 0 ]; then
echo "File $FILE successfully transferred to $TARGET ($HOST)"
else
echo "Failed to transfer $FILE to $TARGET ($HOST)"
fi

With this script I can send a file faster and easier. Let’s say I want to send the file hide.enc to my LAPTOP.

send.sh <file_name> <machine>

send.sh hide.enc LAPTOP

The last thing I did was making this script available globally no matter what directory I am in. That way I can easily send files using send.sh anywhere instead of always doing /home/kali/send.sh file.jpg LAPTOP.

sudo mv /home/hack/send.sh /usr/local/bin/

sudo chmod +x /usr/local/bin/send.sh

SUMMARY

In conclusion, by setting up SSH connections with port forwarding in WSL, adding custom hostnames for each machine, and creating a simple script to transfer files, you can efficiently manage multiple devices during tasks like password cracking or CTF challenges. This approach saves time and eliminates the need to constantly switch between machines, streamlining your workflow. With the ability to send files quickly using a single command, you can take full advantage of all your resources and focus on solving challenges rather than dealing with technical inefficiencies.

--

--

Digi

Cybersecurity professional with 20+ years of experience as a PHP web developer and Linux server administrator. Enjoys CTF events to expand knowledge.