Sometimes you need a quick and disposable SSH server for testing. An Alpine-based Docker container is the perfect solution: lightweight, fast to build, and easy to tear down.
Building the Docker Image
The Docker image is constructed using Alpine Linux, a lightweight and security-oriented Linux distribution.
FROM alpine:latest
RUN apk update && apk add openssh
RUN adduser -D testuser
RUN echo "testuser:password" | chpasswd
RUN echo "root:rootpassword" | chpasswd
# Generate SSH host keys
RUN ssh-keygen -A
# Configure SSH to allow root login
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
EXPOSE 22
# Start the SSH service
CMD ["/usr/sbin/sshd", "-D"]
Build the image with:
docker build -t ssh-test .
Keep the image current by using:
docker build --pull -t ssh-test .
Running the Container
Two primary deployment methods are available:
Using the Bridge Network
This approach maps a specific port on the host machine to port 22 within the container, preventing conflicts with existing SSH services:
docker run -d -p 2222:22 --name test-ssh-connexion ssh-test
Using the Host Network
This method binds the container directly to the host’s network stack:
docker run -d --net host --name test-ssh-connexion ssh-test
Testing the Connection
For the Bridge Network:
ssh testuser@<host_machine_ip_address> -p 2222
For the Host Network:
ssh testuser@<host_machine_ip_address>
Root Access
The container permits root SSH connections by default. Test root access using:
ssh root@<host_machine_ip_address>
Default Credentials
The container includes predefined credentials for testing:
testuser/passwordroot/rootpassword
Modify these lines in the Dockerfile to change credentials:
RUN echo "testuser:password" | chpasswd
RUN echo "root:rootpassword" | chpasswd
Warning: Never use weak passwords in production environments.
Improve the Security
To enhance container security:
- Keep container images regularly updated
- Check for vulnerabilities frequently
- Use Podman in rootless mode
- Run containers with minimum privilege user accounts
This approach addresses the numerous attacks targeting the SSH protocol.