17.6 C
Jaipur
Thursday, December 2, 2021

How to install OpenSSH server on Alpine Linux (including Docker)

Must read

This quick tutorial explains how to install and set up OpenSSH (SSHD) server and client on the Alpine Linux system. Further, you will learn how to build a Docker Linux container running sshd server based upon Alpine Linux image too.

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Alpine Linux
Est. reading time 4 minutes

Installing OpenSSH server on Alpine Linux

The procedure for setting up an ssh-server is as follows:

  1. Search for ssh package, run:
    apk search openssh
    Setting up an ssh-server on Alpine Linux by searching it
  2. Install OpenSSH server and client using the apk command:
    apk add openssh
  3. Enable the sshd service at boot time:
    rc-update add sshd
    Outputs:
     * service sshd added to runlevel default
  4. Start the SSHD service on Alpine Linux immediately using the service command:
    service sshd start
    Outputs:
    
     * Caching service dependencies ...          [ ok ]
    ssh-keygen: generating new host keys: RSA DSA ECDSA ED25519 
     * Starting sshd ...                         [ ok ]
    
  5. Edit the /etc/ssh/sshd_config for customization purpose.
  6. By default, sshd on Alpine Linux will use TCP port 22. So you need to configure the Awall firewall on Alpine Linux to open the TCP port # 22.
  7. Users can now log in using their passwords and ssh keys. For example:
    ssh [email protected]

Installing OpenSSH on the Alpine Linux Docker container

You only need to add the following to your Dockerfile:
RUN apk add --no-cache openssh
But, here is how to setup an ssh server within a docker container using Alpine Linux. Create a new Dockerfile:

FROM alpine:latest
LABEL maintainer="Vivek Gite [email protected]"
RUN apk add --update --no-cache openssh 
RUN echo 'PasswordAuthentication yes' >> /etc/ssh/sshd_config
RUN adduser -h /home/vivek -s /bin/sh -D vivek
RUN echo -n 'vivek:some_password_here' | chpasswd
ENTRYPOINT ["/entrypoint.sh"]
EXPOSE 22
COPY entrypoint.sh /

Create a new shell script named entrypoint.sh as follows:

#!/bin/sh
ssh-keygen -A
exec /usr/sbin/sshd -D -e "[email protected]"

Set up executable permissions using the chmod command:
chmod +x -v entrypoint.sh
Then build it as follows:
docker build -t alpine-sshd .

Sending build context to Docker daemon  30.21kB
Step 1/9 : FROM alpine:latest
latest: Pulling from library/alpine
a0d0a0d46f8b: Pull complete 
Digest: sha256:e1c082e3d3c45cccac829840a25941e679c25d438cc8412c2fa221cf1a824e6a
Status: Downloaded newer image for alpine:latest
 ---> 14119a10abf4
Step 2/9 : LABEL maintainer="Vivek Gite [email protected]"
 ---> Running in 3bd5df80a039
Removing intermediate container 3bd5df80a039
 ---> ad5cae21b2b8
Step 3/9 : RUN apk add --update --no-cache openssh
 ---> Running in 2af9aebbe183
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/community/x86_64/APKINDEX.tar.gz
(1/10) Installing openssh-keygen (8.6_p1-r3)
(2/10) Installing ncurses-terminfo-base (6.2_p20210612-r0)
(3/10) Installing ncurses-libs (6.2_p20210612-r0)
(4/10) Installing libedit (20210216.3.1-r0)
(5/10) Installing openssh-client-common (8.6_p1-r3)
(6/10) Installing openssh-client-default (8.6_p1-r3)
(7/10) Installing openssh-sftp-server (8.6_p1-r3)
(8/10) Installing openssh-server-common (8.6_p1-r3)
(9/10) Installing openssh-server (8.6_p1-r3)
(10/10) Installing openssh (8.6_p1-r3)
Executing busybox-1.33.1-r3.trigger
OK: 12 MiB in 24 packages
Removing intermediate container 2af9aebbe183
 ---> 810ed83e5a93
Step 4/9 : RUN echo 'PasswordAuthentication yes' >> /etc/ssh/sshd_config
 ---> Running in c5288a3af1a0
Removing intermediate container c5288a3af1a0
 ---> e2f05f1da7cd
Step 5/9 : RUN adduser -h /home/vivek -s /bin/sh -D vivek
 ---> Running in 8ae29c917926
Removing intermediate container 8ae29c917926
 ---> e258460dfa67
Step 6/9 : RUN echo -n 'vivek:vivek' | chpasswd
 ---> Running in 1df5fef24dcf
chpasswd: password for 'vivek' changed
Removing intermediate container 1df5fef24dcf
 ---> 1c903eaa551f
Step 7/9 : ENTRYPOINT ["/entrypoint.sh"]
 ---> Running in 86af67a76db7
Removing intermediate container 86af67a76db7
 ---> 185a32d0bc09
Step 8/9 : EXPOSE 22
 ---> Running in 203f6f8bcaa2
Removing intermediate container 203f6f8bcaa2
 ---> 9f3f5a20d44c
Step 9/9 : COPY entrypoint.sh /
 ---> c1a0fe4e6375
Successfully built c1a0fe4e6375
Successfully tagged alpine-sshd:latest

Run it:
docker run --name sshd_app -d -p 22:22 alpine-sshd:latest

03a3661d04d6aa266690c3c44ab3aaa23ea2258ebe18d5efd07f8553710c9da2

Patreon supporters only guides 🤓

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare
  • Join my Patreon to support independent content creators and start reading latest guides:

Join Patreon

Verify it:
docker ps

CONTAINER ID   IMAGE                COMMAND            CREATED          STATUS          PORTS                               NAMES
03a3661d04d6   alpine-sshd:latest   "/entrypoint.sh"   16 seconds ago   Up 15 seconds   0.0.0.0:22->22/tcp, :::22->22/tcp   sshd_app

Now you can ssh into it:
ssh [email protected]
Please note that the above is a primary container for demo purposes, and I have not set up a volume for persistence data. See docker volumes for more info.

Summing up

You learned about installing OpenSSH on the Alpine Linux server or building Docker Container with OpenSSH server using Alpine Linux as base image. See the following man pages using the man command:
man docker
man docker-run
man docker-build
man docker-volume


ADVERTISEMENT

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest article