Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build SSH Server with Dockerfile

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces how to use Dockerfile to build SSH Server, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

one。 Related documents 1. Create a new directory and a Dockerfilemkdir y109-sshdvim Dockerfile2. The content of Dockerfile is as follows: # docker sshdFROM ubuntu:14.04MAINTAINER y10 sets the root password RUN echo 'root:bMg5kesfdsfesx9gD' using 163.com 's source COPY sources.list.163.txt / etc/apt/sources.listRUN apt-get-y update# | chpasswd# installs openssh-serverRUN apt-get-y install openssh-serverRUN mkdir / var/run/sshd# SSH login fix. Otherwise user is kicked off after loginRUN sed 's@session\ s*required\ s*pam_loginuid.so@session optional pam_loginuid.so@g'-I / etc/pam.d/sshd## ENV NOTVISIBLE "in users profile" RUN echo "export VISIBLE=now" > > / etc/profile# add a public key (omitted if there is no public key) RUN mkdir / root/.sshRUN echo' ssh-rsa YOU_PUB_KEY' > / the program CMD that runs after the root/authorized_keys# container starts ["/ usr/sbin/sshd" "- D"] # Open port 22 EXPOSE 22

The content of sources.list.163.txt is as follows

Deb http://mirrors.163.com/ubuntu/ precise main restricteddeb-src http://mirrors.163.com/ubuntu/ precise main restricteddeb http://mirrors.163.com/ubuntu/ precise-updates main restricteddeb-src http://mirrors.163.com/ubuntu/ precise-updates main restricteddeb http://mirrors.163.com/ubuntu/ precise universedeb-src http://mirrors.163.com/ubuntu/ precise universedeb http://mirrors.163.com/ubuntu/ precise-updates universedeb-src http:/ / mirrors.163.com/ubuntu/ precise-updates universedeb http://mirrors.163.com/ubuntu/ precise-security main restricteddeb-src http://mirrors.163.com/ubuntu/ precise-security main restricteddeb http://mirrors.163.com/ubuntu/ precise-security universedeb-src http://mirrors.163.com/ubuntu/ precise-security universe II. Build Image

Use docker build to generate a mirror

The-t parameter is the TAG for this image.

Sudo docker build-t 'y109 kBSending build context to Docker daemonStep. / Sending build context to Docker daemon 4.608 kBSending build context to Docker daemonStep 0: FROM ubuntu:14.04-> 9cbaf023786cStep 1: MAINTAINER y109-> Using cache-> 2256ab1cc931Step 2: COPY sources.list.163.txt / etc/apt/sources.list-> Using cache-> 65536ca26964Step 3: RUN apt-get-y update-> Using cache-> 60639e42f098Step 4: RUN echo 'root:pass123456' | chpasswd-- > Using cache-> 8644dd20854fStep 5: RUN apt-get-y install openssh-server-- > Using cache-> 98039327bca7Step 6: RUN mkdir / var/run/sshd-- > Using cache-> 9bd3b3fc7828Step 7: RUN sed 's@session\ s*required\ s*pam_loginuid.so@session optional pam_loginuid.so@g'-I / etc/pam.d/sshd-> Using cache-> d748cb9428a0Step 8: RUN echo "export VISIBLE=now" > > / etc/profile-- > Using cache-> e975cd819243Step 9: RUN mkdir / root/.ssh-> Using cache-> e561acc07675Step 10: RUN echo 'ssh-rsa YOU_PUBLIC_KEY'-> Using cache-> 8f6882a72037Step 11: CMD ["/ usr/sbin/sshd" "- D"]-- > Using cache-- > 48cbd2c4aa70Step 12: EXPOSE 22-> Using cache-- > 3101a36f0084Successfully built 3101a36f0084

Use the docker images command to view the image and confirm that the image has been built successfully

Sudo docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEy109/sshd latest 3101a36f0084 22 minutes ago 226.1 MB 23f604e547b8 28 minutes ago 226.1 MB 50647a1fb746 36 minutes ago 226.1 MBy...

Y109/sshd is the mirror image we just built.

three。 Create Container

Use docker run to create a Container with a mirror

-d: Detached mode to make Container run in background mode-p: map port 22 to the network card of the host. Format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort-name: specify a name for Container. Once the name is specified, it is bound to the Container. It can be listed with docker ps-a.

Sudo docker run-d-p 10922 22-- name y109-sshd y109/sshd

The public network port I use is 10922, which can be modified as needed. The next step is to make sure that Container is running normally.

Sudo docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESfc37b83d343e y109/sshd:latest "/ usr/sbin/sshd-D" 9 seconds ago Up 9 seconds 0.0.0.0 seconds ago Up 10922-> 22/tcp y109-sshd

It seems that the execution is successful. Try to connect.

Ssh root@localhost-p10922The authenticity of host'[localhost]: 10922 ([127.0.0.1]: 10922) 'can't be established.ECDSA key fingerprint is 4d:48:5c:61:54:d6:8f:62:70:a2:0e:ab:b7:1a:cb:f7.Are you sure you want to continue connecting (yes/no)? YesWarning: Permanently added'[localhost]: 10922'(ECDSA) to the list of known hosts.The programs included with the Ubuntu system are free software;the exact distribution terms for each program are described in theindividual files in / usr/share/doc/*/copyright.Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted byapplicable law.root@80f07ad418fe:~#

Has successfully connected to Container.

four。 Close Container

Sudo docker stop fc3 fc3 is the abbreviation of Container Id fc37b83d343e, as long as you can uniquely identify the Container. Or sudo docker stop y109-sshd.

five。 Run Container

Sudo docker start y109-sshd

Thank you for reading this article carefully. I hope the article "how to build SSH Server with Dockerfile" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report