本文主要是介绍Dockerfile制作能够ssh的ubuntu和centos7系统,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
一.制作ubuntu系统
1.Dockerfile内容如下
2.运行容器并验证
二.制作centos7系统
1.Dockerfile内容如下
2.验证并测试
一.制作ubuntu系统
要求:制作一个能够远程ssh的ubuntu操作系统镜像,要求通过该镜像运容器后,内部拥有一些自动补全、vim、网络插件等常用的软件包。
1.Dockerfile内容如下
[root@localhost docker]# cat Dockerfile
FROM ubuntu
RUN apt-get update && apt-get install -y openssh-server vim net-tools iproute2 bash-completion
RUN mkdir -p /var/run/sshd && echo 'root:nihao' | chpasswd
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config && sed -i 's@session required pam_loginuid.so@session optional pam_loginuid.so@g' /etc/pam.d/sshd
ENTRYPOINT ["/usr/sbin/sshd", "-D"]
EXPOSE 22
[root@localhost docker]# docker build -t myubuntu ./
[root@localhost docker]# docker images | grep myubuntu
myubuntu latest dbcc4cc0f303 About a minute ago 300MB
2.运行容器并验证
[root@localhost docker]# docker run --name myubuntu -p 2222:22 -d myubuntu
[root@localhost docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b055d6ecb26c myubuntu "/usr/sbin/sshd -D" About a minute ago Up About a minute 0.0.0.0:2222->22/tcp, :::2222->22/tcp myubuntu
[C:\~]$ ssh 192.168.2.160:2222
Connecting to 192.168.2.160:2222...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Welcome to Ubuntu 24.04 LTS (GNU/Linux 5.14.0-162.6.1.el9_1.x86_64 x86_64)
* Documentation: https://help.ubuntu.com* Management: https://landscape.canonical.com* Support: https://ubuntu.com/pro
This system has been minimized by removing packages and content that are
not required on a system that users do not log into.
To restore this content, you can run the 'unminimize' command.
Last login: Wed Jun 5 06:01:37 2024 from 192.168.2.2
root@b055d6ecb26c:~#
root@b055d6ecb26c:~# ss -lntup | grep 22
tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=1,fd=3))
tcp LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=1,fd=4))
root@b055d6ecb26c:~# netstat -anp | grep 22
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1/sshd: /usr/sbin/s
tcp 0 52 172.17.0.2:22 192.168.2.2:57982 ESTABLISHED 49/sshd: root@pts/0
tcp6 0 0 :::22 :::* LISTEN 1/sshd: /usr/sbin/s
root@b055d6ecb26c:~# service ssh status* sshd is running
二.制作centos7系统
要求同制作ubuntu系统一样
只是在centos系统内下载好openssh后启动ssh会报错没有主机密钥,所以我们需要在Dockerfile中使用ssh-kengen -A生成主机密钥
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
sshd: no hostkeys available -- exiting.
1.Dockerfile内容如下
[root@localhost centos]# cat Dockerfile
FROM centos:7
RUN yum install -y openssh-server vim net-tools bash-completion
RUN mkdir -p /var/run/sshd && echo 'nihao' | passwd --stdin root && ssh-keygen -A
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config && sed -i 's@session required pam_loginuid.so@session optional pam_loginuid.so@g' /etc/pam.d/sshd
ENTRYPOINT ["/usr/sbin/sshd", "-D"]
EXPOSE 22
[root@localhost centos]# docker build -t mycentos:7 ./
[root@localhost centos]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 7 84f3511bdfb5 7 minutes ago 519MB
mytomcat latest c4d1c41c131b 18 hours ago 745MB
nginx latest e784f4560448 5 weeks ago 188MB
busybox latest 65ad0d468eb1 13 months ago 4.26MB
tomcat latest fb5657adc892 2 years ago 680MB
centos 7 eeb6ee3f44bd 2 years ago 204MB
centos latest 5d0da3dc9764 2 years ago 231MB
nginx 1.17.2 4733136e5c3c 4 years ago 126MB
[root@localhost centos]# docker run -it --name mycentos7 -p 2222:22 -d mycentos:7
3bb789c6e2c973f2727dc8925de5f8847cf266eddb9a6510478d3015c2f01e6f
[root@localhost centos]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3bb789c6e2c9 mycentos:7 "/usr/sbin/sshd -D" 2 seconds ago Up 2 seconds 0.0.0.0:2222->22/tcp, :::2222->22/tcp mycentos7
2.验证并测试
[C:\~]$ ssh root@192.168.2.160:2222Connecting to 192.168.2.160:2222...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.Last failed login: Thu Jun 13 07:42:36 UTC 2024 from 192.168.2.2 on ssh:notty
There was 1 failed login attempt since the last successful login.
这篇关于Dockerfile制作能够ssh的ubuntu和centos7系统的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!