이 포스트는 kafka를 기준으로 설명하고 있다. 다른 서비스를 설정한다면 환경변수 및 여러 경로에 해당 서비스를 입력하면 된다.
(2021/07/19 추가) 심볼릭 링크 파일을 생성하여 기존 환경변수 폴더에 추가하는 방식도 있다.
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:$HOME/kafka_2.13-2.8.0/bin"
CONF="$HOME/kafka_2.13-2.8.0/config"
@echo off
wsl /etc/init.d/auto_start.sh
powershell -ExecutionPolicy Bypass -File C:\wsl2_settings\wsl2_network.ps1
kafkaLog=" * Establishing kafka server"
zookeeperLog=" * Establishing zookeeper server"
source /etc/environment
sudo /etc/init.d/ssh restart
echo -n "$zookeeperLog"
sudo screen -dmS zookeeper zookeeper-server-start.sh $CONF/zookeeper.properties
while true
do
if [ "$(netstat -an|grep 2181|awk '{print $6}'|head -1)" != "LISTEN" ]
then
break
fi
done
for (( i = 0; i < $COLUMNS - ${#zookeeperLog} - 7; ++i)) do echo -n " "; done; echo -n "[ OK ] ";
echo -n "$kafkaLog"
sudo screen -dmS kafka kafka-server-start.sh $CONF/server.properties
for (( i = 0; i < $COLUMNS - ${#kafkaLog} - 7; ++i)) do echo -n " "; done; echo -n "[ OK ] ";
$remoteport = bash -c "ifconfig eth0 | grep 'inet '"
$found = $remoteport -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
if( $found ){
$remoteport = $matches[0];
} else{
echo "The Script Exited, the ip address of WSL 2 cannot be found";
exit;
}
#[Ports]
#All the ports you want to forward separated by coma
$ports=@(80,443,10000,3000,5000);
#[Static ip]
#You can change the addr to your ip config to listen to a specific address
$addr='0.0.0.0';
$ports_a = $ports -join ",";
#Remove Firewall Exception Rules
iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' ";
#adding Exception Rules for inbound and outbound Rules
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP";
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP";
for( $i = 0; $i -lt $ports.length; $i++ ){
$port = $ports[$i];
iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr";
iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport";
}
schtasks /create /ru username /rp /sc onstart /tn wsl2_startup /tr c:\wsl2_settings\wsl2_startup.bat /rl highest
Defaults secure_path="/home/username/kafka_2.13-2.8.0/bin"
%sudo ALL=NOPASSWD: /etc/init.d/*
wsl --shutdown
wsl sudo /etc/init.d/auto_start.sh