From 00a5006518493b77c7234f49c4ac593a7b30057c Mon Sep 17 00:00:00 2001 From: Kaelem Chandra Date: Sat, 7 Dec 2024 14:44:35 +1300 Subject: [PATCH 1/3] Add SSH installation in Dockerfile --- Dockerfile | 3 +++ 1 file changed, 3 insertions(+) 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 && \ From a5d8f2bc583830b458a9252d8b7ca9ffd6cdb733 Mon Sep 17 00:00:00 2001 From: Kaelem Chandra Date: Sat, 7 Dec 2024 14:49:34 +1300 Subject: [PATCH 2/3] Add SSH key import/generation prompt to oobe.sh --- oobe.sh | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/oobe.sh b/oobe.sh index 9084313..31b089d 100644 --- a/oobe.sh +++ b/oobe.sh @@ -81,6 +81,53 @@ 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 @@ -159,6 +206,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 From 56041c4f47c9024b94723ee6be444208c14e74da Mon Sep 17 00:00:00 2001 From: Kaelem Chandra Date: Sat, 7 Dec 2024 15:04:00 +1300 Subject: [PATCH 3/3] Add `/run/docker/netns` dir creation to shell profiles --- oobe.sh | 2 ++ zsh/.zshrc | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/oobe.sh b/oobe.sh index 31b089d..7365509 100644 --- a/oobe.sh +++ b/oobe.sh @@ -134,6 +134,8 @@ 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: " 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