Open In App

How To Install OpenSSH On Alpine?

Last Updated : 15 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

OpenSSH, an SSH protocol implementation, is useful for securely transferring any file over the internet that can be over any trusted or untrusted network. It does strong encryption of any data that is transferred over the SSH connection, so no third party can see the data being transferred over the SSH connection. If the remote system has openssh installed on it and if you know the hostname and IP address of that remote system, then you can log in to any remote system using OpenSSH. Be it any

Alpine Linux is a lightweight Linux distro that is small, simple, fast, secure, and efficient. It is especially used in containerized environments like Docker to create small and lightweight containers and scale multiple Kubernetes clusters to do basic to medium-level operations easily.

Alpine Linux distro is becoming popular for the deployment of React and Angular Apps on a web server that uses Alpine Linux OS. Now, to do web server monitoring, install SSL certificates, or even perform troubleshooting, you need to SSH into this remote system. Adding OpenSSH to Alpine Linux is simple.

In this article, we will look at how to install and set up OpenSSH (sshd) on the Alpine Linux system.

Steps to install OpenSSH on Alpine

Step 1: Search the “OpenSSH” package

Search the “OpenSSH” package to check whether it exists for downloading by running the below command:

apk search openssh
apk-search-openssh

Search openssh package

Step 2: Install the OpenSSH package

From step 1, you can see that the openssh package is available for installation. This will install the OpenSSH server and client for us. Installing it using `apk` package manager provided in Alpine Linux as below:

apk add openssh
Install OpenSSH using apk command

Install OpenSSH using apk command

Step 3: Enable the sshd service to get started at boot time

Enable sshd service to start at boot time to avoid enabling the service again and again across restarts of the system. To enable it we can use rc-update command as follows:

rc-update add sshd
service

Enable sshd service for boot time

Step 4: Start the sshd service on Alpine Linux

To immediately start the sshd service on Alpine Linux, we will use the `service` command as follows:

service sshd start
rc-update

Start sshd service immediately

Step 5: Check the IP address of for your Alpine Linux system

Get the IP address of your Alpine Linux system to be able to login into this system from any remote system.

ip addr show

show-ip-addr

Step 6: Login to this Alpine Linux system from remote system

Now you can SSH into this Alpine Linux system from any other system.

ssh <username>@<ip_address>

Install OpenSSH on the Alpine Linux Docker container

Step 1: Create a new Dockerfile as follows:

FROM alpine:latest
LABEL maintainer="GeeksForGeeks support@geeksforgeeks.org"
RUN apk add --no-cache openssh
RUN echo 'PasswordAuthentication yes' >> /etc/ssh/sshd_config
RUN adduser -h /home/gfg -s /bin/sh -D gfg
RUN echo -n 'gfg:some_password_here' | chpasswd
ENTRYPOINT ["/start.sh"]
EXPOSE 22
COPY start.sh /

In the above Dockerfile, we are first pulling the alpine’s latest docker image from the docker repository. Then we install the openssh package and allow the Password Authentication. Then add a new user `gfg` with his root directory as ‘/home/gfg’. Then set the password for this gfg user using chpasswd command. Set the entrypoint to run the `start.sh` once the container starts. Expose port 22 to allow other systems to be able to SSH into our docker container.

Step 2: Create a new shell script file named`start.sh` as follows:

#!/bin/sh
ssh-keygen -A
exec /usr/sbin/sshd -D -e "$@"

Step 3: Give the shell script executable permissions using the chmod command:

chmod +x -v start.sh

Step 4: Build the docker container using the docker build command:

docker build -t alpine-openssh .
Build the docker image using the Dockerfile

Build the docker image using the Dockerfile

Step 5: Run the docker container as follows:

docker run --name gfg_alpine_ssh -d -p 22:22 alpine-openssh:latest

Here, we are running the docker container by naming it as ‘gfg_alpine_ssh’ and exposing the local port 22 and docker container’s port 22 and putting it as background/daemon process.

2)

Run the docker container

Step 6: Verify that docker container is now running using ‘docker ps’ command.

docker ps
Get a list of running docker containers

Get a list of running docker containers

Step 7: Get the IP address of the container

docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' [container_name | container_id]

To be able to SSH into this container from outside, you need to know the IP address of the container. You can use docker inspect command and then pass either the container name or container id as an argument to get the IP address of it.

Obtain the IP address of  specific container

Obtain the IP address of specific container

Step 8: Simply SSH into the container using the username and docker container’s IP address

ssh <username>@<docker-container-ip-address>
SSH into docker container

SSH into docker container

Conclusion

In this article we looked at how we can install OpenSSH on Alpine Linux system. OpenSSH provides developers, network administrators an easy and a secure way to log in to remote systems and perform their tasks there. It is easy to install and use. On Alpine Linux, we can simply install OpenSSH using the apk package manager and using the add command. You just need the remote system’s hostname and IP address to log in to that system.

Install OpenSSH on Alpine – FAQ’s

How to install OpenSSH in Alpine Linux?

To install OpenSSH in Alpine Linux, run apk add openssh in the terminal. Then, start the SSH service using rc-service sshd start.

Does Alpine include SSH?

Yes, Alpine Linux includes OpenSSH in its package repositories, allowing users to install and use SSH for remote access and secure communication.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads