Skip to content

Commit fcb176e

Browse files
authored
SSH Server on clab with key import (#5)
SSH Server on clab with key import
2 parents 7f38286 + 56041c4 commit fcb176e

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ RUN apt install -y --no-install-recommends \
2020
dnsutils \
2121
telnet \
2222
unzip \
23+
openssh-server \
2324
zsh && rm -rf /var/lib/apt/lists/*
2425

2526
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
3031

3132
COPY ./profile /etc/profile
3233

34+
RUN bash -c "echo 'port 2222' >> /etc/ssh/sshd_config"
35+
3336
# Create clab user and add to sudo group
3437
RUN useradd -m -s /bin/zsh clab && \
3538
echo "clab:clab" | chpasswd && \

oobe.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,61 @@ function install_fonts {
8181
fi
8282
}
8383

84+
function import_ssh_keys {
85+
KEY_CHECK=$(powershell.exe -NoProfile -Command '
86+
$key_types = @("rsa", "ecdsa", "ed25519")
87+
88+
foreach ( $type in $key_types )
89+
{
90+
if( Test-Path $env:userprofile\.ssh\id_$type.pub )
91+
{
92+
return $type
93+
}
94+
}
95+
Write-Output False
96+
')
97+
98+
mkdir -p /home/clab/.ssh
99+
100+
case $KEY_CHECK in
101+
102+
rsa*)
103+
echo -e "\033[32mRSA key found, Copying into Containerlab WSL...\033[0m"
104+
KEY=$(powershell.exe -NoProfile -Command 'Get-Content $env:userprofile\.ssh\id_rsa.pub')
105+
echo $KEY >> /home/clab/.ssh/authorized_keys
106+
;;
107+
ecdsa*)
108+
echo -e "\033[32mECDSA key found, Copying into Containerlab WSL...\033[0m"
109+
KEY=$(powershell.exe -NoProfile -Command 'Get-Content $env:userprofile\.ssh\id_ecdsa.pub')
110+
echo $KEY >> /home/clab/.ssh/authorized_keys
111+
;;
112+
ed25519*)
113+
echo -e "\033[32mED25519 key found, Copying into Containerlab WSL...\033[0m"
114+
KEY=$(powershell.exe -NoProfile -Command 'Get-Content $env:userprofile\.ssh\id_ed25519.pub')
115+
echo $KEY >> /home/clab/.ssh/authorized_keys
116+
;;
117+
False*)
118+
echo -e "\033[34mNo host keys found, Generating RSA key...\033[0m"
119+
powershell.exe -NoProfile -Command "ssh-keygen -t rsa -b 4096 -f \$env:userprofile\.ssh\id_rsa -N '\"\"'"
120+
KEY=$(powershell.exe -NoProfile -Command 'Get-Content $env:userprofile\.ssh\id_rsa.pub')
121+
echo $KEY >> /home/clab/.ssh/authorized_keys
122+
# powershell.exe -NoProfile -Command "Get-Content $env:userprofile\.ssh\id_rsa.pub | ssh clab@localhost -p 2222 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys'"
123+
;;
124+
*)
125+
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"
126+
esac
127+
128+
echo -e "\033[32mKeys successfully copied. You can SSH into Container WSL passwordless with: 'ssh clab@localhost -p 2222'\033[0m"
129+
}
130+
84131
# We know the user clab exists from Dockerfile with UID 1000
85132
if getent passwd "$DEFAULT_UID" > /dev/null ; then
86133

87134
echo -e "\033[32mWelcome to Containerlab's WSL distribution\033[0m"
88135

89136
echo "cd ~" >> /home/clab/.bashrc
137+
138+
echo "echo clab | sudo -S mkdir -p /run/docker/netns" >> /home/clab/.bashrc
90139

91140
PS3="
92141
Please select which shell you'd like to use: "
@@ -159,6 +208,12 @@ Select zsh configuration: "
159208
esac
160209
done
161210

211+
read -p "Copy Windows SSH keys for passwordless SSH access? (y/N) " -n 1 -r
212+
echo
213+
if [[ $REPLY =~ ^[Yy]$ ]]; then
214+
import_ssh_keys
215+
fi
216+
162217
exit 0
163218
fi
164219

zsh/.zshrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,6 @@ eval "$(pyenv init -)"
136136
# go path
137137
export PATH=$PATH:/usr/local/go/bin:~/go/bin
138138

139-
cd ~
139+
cd ~
140+
# create /run/docker/netns without password prompt
141+
echo clab | sudo -S mkdir -p /run/docker/netns

0 commit comments

Comments
 (0)