NfreerCom / servermaster

시스템관리자
MIT License
0 stars 0 forks source link

WSL #5

Open NfreerCom opened 1 year ago

NfreerCom commented 1 year ago

centos systemctl D-bus connection 에러시

mv /usr/bin/systemctl /usr/bin/systemctl.old
curl https://raw.githubusercontent.com/gdraheim/docker-systemctl-replacement/master/files/docker/systemctl.py > /usr/bin/systemctl
chmod +x /usr/bin/systemctl

wsl unregister

wsl --unregister

wsl 기본 VM 설정

wsl --set-default centos7

wsl export

wsl --export centos7 /k/111/wslvm/centos7-systemd-yum-kakao-nfreercom-sudoers-clean-wget-docker-engine-kind-kubectl.tar

wsl import

wsl --import centos-c /f/111/centos/centos-c /k/111/wslvm/centos7-systemd-yum-kakao-nfreercom-sudoers-clean-wget-docker-engine-k3d-kubectl-nginx-stopgo.tar

wsl --import centos-c /c/centos/centos-c /k/111/wslvm/centos7-systemd-yum-kakao-nfreercom-sudoers-clean-wget-docker-engine.tar

wsl 인스턴스 다른 ip 설정 (중요:실행은 /usr/local/sbin/wsl-netns.sh 숫자)

vim /usr/local/sbin/wsl-netns.sh
#!/usr/bin/env bash
instance_num=$1
#if [ -e /run/netns/]

# Create the bridge that will be common to all instances.
# Only a `wsl --shutdown` will terminate the bridge, unless
# otherwise manually removed.
if [ ! -e /sys/devices/virtual/net/br1 ]
then
    ip link add name br1 type bridge
    ip addr add 10.0.0.253/24 brd + dev br1
    ip link set br1 up
fi

# Add namespace for this instance
if [ ! -e /run/netns/vnet${instance_num} ]
then
    ip netns add vnet${instance_num}
fi

# Adds a veth pair.  The vethX
# side will reside # inside the namespace 
# and be the primary NIC inside that namespace.
# The br-vethX  end will reside in the primary
# namespace.
ip link add veth${instance_num} type veth peer name br-veth${instance_num}
ip link set veth${instance_num} netns vnet${instance_num}
# Give it a unique IP based on the instance number
ip netns exec vnet${instance_num} \
    ip addr add 10.0.0.${instance_num}/24 dev veth${instance_num}
ip link set br-veth${instance_num} up
# Add the bridged end of the veth pair
# to br1
ip link set br-veth${instance_num} master br1
ip netns exec vnet${instance_num} \
    ip link set veth${instance_num} up

# Set the default route in the namespace
ip netns exec vnet${instance_num} \
    ip route add default via 10.0.0.253
# Enable loopback fort he namespace
ip netns exec vnet${instance_num} \
    ip link set up dev lo
# Set up NAT for return traffic
iptables \
    -t nat \
    -A POSTROUTING \
    -s 10.0.0.0/24 \
    -j MASQUERADE
# Enable forwarding
sysctl -w net.ipv4.ip_forward=1

# Optional - Start a namespace for the 
# default WSL user (UID 1000).
# You can exit this namespace normally
# via the `exit` comamnd or Ctrl+D.
default_username=$(getent passwd 1000 | cut -d: -f1)
nsenter -n/var/run/netns/vnet${instance_num} su - $default_username
chmod +x /usr/local/sbin/wsl-netns.sh && cd /usr/local/sbin
vim /etc/rc.d/rc.local
/usr/local/sbin/wsl-netns.sh
NfreerCom commented 1 year ago

58.124.221.191

192.168.35.100

1 TCP 62121:62121 192.168.35.100 80:80 2 TCP 52121:52121 192.168.35.100 443:443 3 TCP 32000:32000 192.168.35.100 22:22

systemctl start docker && k3d cluster start master-worker

k3d cluster stop master-worker && systemctl stop docker

sudo systemctl restart sshd.service

$remoteport = bash.exe -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,22,443,1000,2000,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"; }