diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..203c22a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,30 @@ +FROM alpine:latest +# Your desired username and super secure password +ENV USERNAME="ed" +ENV PASSWORD="password" + +RUN apk update && apk upgrade && apk add openssh openrc bash + +# For faster ssh connection +RUN echo "UseDNS no" >> /etc/ssh/sshd_config.d/20-dont_use_dns.conf + +# Adding ssh user +RUN adduser -h /home/$USERNAME -s /bin/bash -u 1000 -D $USERNAME +RUN echo "PermitRootLogin no" >> /etc/ssh/sshd_config.d/20-deny_root.conf +RUN echo "AllowUsers $USERNAME" >> /etc/ssh/sshd_config.d/20-allow_users.conf +RUN echo "$USERNAME:$PASSWORD" | chpasswd + +# touch command is For running openrc in container so we can use our init system. First command returns error, in order to continue I added `|| true` to Dockerfile +# Why openrc instead of starting normally? Because if you check alpine's openssh openrc script they do a little bit hardening and I like that. +RUN rc-update add sshd && rc-status && rc-service sshd start || true + +# Optional: giving doas(sudo) perms to user +RUN apk add doas +RUN echo "permit persist :wheel" >> /etc/doas.d/doas.conf +RUN adduser $USERNAME wheel + +COPY ./start_ssh.sh /usr/sbin +RUN chmod +x /usr/sbin/start_ssh.sh + +# Script runs /bin/bash. If you dont want it remove it as you need +ENTRYPOINT ["/usr/sbin/start_ssh.sh"] diff --git a/README.MD b/README.MD new file mode 100644 index 0000000..9952261 --- /dev/null +++ b/README.MD @@ -0,0 +1,43 @@ +# Basic ssh server with docker/podman +Hi this is my setup for sshing container. + +# Why? + +Purpose of this is using with `ssh` and sshfs` and gaining `vscode/dev-containers` functionality with any ide you desire. + +# Warning +Keep in mind that this docker file built with no security with mind. Its assumed that your computer's or your modem's firewall already blocking this ports to the outside. If you want to use it in internal networks, please harden it. + +# How to use it. +First, keep in mind that every time you started a new container, sshd key will change and ssh will warn you about it. You should remove ~/.ssh everytime for a new container. + +you build it `Dockerfile` with: +``` +docker build -t alpine-ssh . +``` +then run it with: +``` +docker run -p 2222:22 -it alpine-ssh +``` +then connect to ssh with your terminal(default password is `password`): +``` +ssh -p 2222 ed@yourlocalipaddress +``` +then mount user ed's home folder with(default password is `password`): +``` +sshfs -oport=2222 ed@yourlocalipaddress:/home/ed /your/desired/mount/point +``` +change your local ip address with inet section of your network interface when running `ip addr` for example if it shows: +``` +inet 192.168.1.122/44 brd 192.168.1.255 scope global dynamic noprefixroute wlan0 +``` +yourlocalipaddress should be 192.168.1.122. + +After finished unmount with: +``` +fusermount -u /your/desired/mount/point +``` + +# I want to change default user +Sure default user's username defined in "USERNAME" variable and default password is in "PASSWORD", you can either change it in `Dockerfile` or define as variable when building container. + diff --git a/README.md b/README.md deleted file mode 100644 index 4f16d56..0000000 --- a/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# alpine-ssh-container - -Purpose of this is using with ssh and sshfs and gaining vscode/dev-containers functionality with any IDE you desire. \ No newline at end of file diff --git a/start_ssh.sh b/start_ssh.sh new file mode 100644 index 0000000..4613807 --- /dev/null +++ b/start_ssh.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +# If we dont do that ssh service crashes because of openrc run policy. +touch /run/openrc/softlevel +rc-service sshd restart +# Drop to the bash shell +/bin/bash