diff --git a/Dockerfile b/Dockerfile index 39966bf..ef39003 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,6 +20,7 @@ RUN apt install -y --no-install-recommends \ dnsutils \ telnet \ unzip \ + openssh-server \ zsh && rm -rf /var/lib/apt/lists/* COPY --chmod=644 --chown=root:root ./wsl-distribution.conf /etc/wsl-distribution.conf @@ -30,6 +31,8 @@ COPY ./terminal-profile.json /usr/lib/wsl/terminal-profile.json COPY ./profile /etc/profile +RUN bash -c "echo 'port 2222' >> /etc/ssh/sshd_config" + # Create clab user and add to sudo group RUN useradd -m -s /bin/zsh clab && \ echo "clab:clab" | chpasswd && \ diff --git a/oobe.sh b/oobe.sh index 9084313..7365509 100644 --- a/oobe.sh +++ b/oobe.sh @@ -81,12 +81,61 @@ function install_fonts { fi } +function import_ssh_keys { + KEY_CHECK=$(powershell.exe -NoProfile -Command ' + $key_types = @("rsa", "ecdsa", "ed25519") + + foreach ( $type in $key_types ) + { + if( Test-Path $env:userprofile\.ssh\id_$type.pub ) + { + return $type + } + } + Write-Output False + ') + + mkdir -p /home/clab/.ssh + + case $KEY_CHECK in + + rsa*) + echo -e "\033[32mRSA key found, Copying into Containerlab WSL...\033[0m" + KEY=$(powershell.exe -NoProfile -Command 'Get-Content $env:userprofile\.ssh\id_rsa.pub') + echo $KEY >> /home/clab/.ssh/authorized_keys + ;; + ecdsa*) + echo -e "\033[32mECDSA key found, Copying into Containerlab WSL...\033[0m" + KEY=$(powershell.exe -NoProfile -Command 'Get-Content $env:userprofile\.ssh\id_ecdsa.pub') + echo $KEY >> /home/clab/.ssh/authorized_keys + ;; + ed25519*) + echo -e "\033[32mED25519 key found, Copying into Containerlab WSL...\033[0m" + KEY=$(powershell.exe -NoProfile -Command 'Get-Content $env:userprofile\.ssh\id_ed25519.pub') + echo $KEY >> /home/clab/.ssh/authorized_keys + ;; + False*) + echo -e "\033[34mNo host keys found, Generating RSA key...\033[0m" + powershell.exe -NoProfile -Command "ssh-keygen -t rsa -b 4096 -f \$env:userprofile\.ssh\id_rsa -N '\"\"'" + KEY=$(powershell.exe -NoProfile -Command 'Get-Content $env:userprofile\.ssh\id_rsa.pub') + echo $KEY >> /home/clab/.ssh/authorized_keys + # powershell.exe -NoProfile -Command "Get-Content $env:userprofile\.ssh\id_rsa.pub | ssh clab@localhost -p 2222 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys'" + ;; + *) + echo "\033[34m\nCouldn't match key type, invoking Powershell may have failed. Create an issue at https://github.com/srl-labs/wsl-containerlab\033[0m" + esac + + echo -e "\033[32mKeys successfully copied. You can SSH into Container WSL passwordless with: 'ssh clab@localhost -p 2222'\033[0m" +} + # We know the user clab exists from Dockerfile with UID 1000 if getent passwd "$DEFAULT_UID" > /dev/null ; then echo -e "\033[32mWelcome to Containerlab's WSL distribution\033[0m" echo "cd ~" >> /home/clab/.bashrc + + echo "echo clab | sudo -S mkdir -p /run/docker/netns" >> /home/clab/.bashrc PS3=" Please select which shell you'd like to use: " @@ -159,6 +208,12 @@ Select zsh configuration: " esac done + read -p "Copy Windows SSH keys for passwordless SSH access? (y/N) " -n 1 -r + echo + if [[ $REPLY =~ ^[Yy]$ ]]; then + import_ssh_keys + fi + exit 0 fi diff --git a/zsh/.zshrc b/zsh/.zshrc index 8ce0ff5..f0d1d8d 100644 --- a/zsh/.zshrc +++ b/zsh/.zshrc @@ -136,4 +136,6 @@ eval "$(pyenv init -)" # go path export PATH=$PATH:/usr/local/go/bin:~/go/bin -cd ~ \ No newline at end of file +cd ~ +# create /run/docker/netns without password prompt +echo clab | sudo -S mkdir -p /run/docker/netns \ No newline at end of file