-
在一个文件夹中创建
dockerfile文件,文件内容如下powershell# 使用官方的 Ubuntu 镜像 FROM ubuntu:latest # 安装 SSH 服务 RUN apt-get update && apt-get install -y openssh-server # 创建 SSH 目录并设置权限 RUN mkdir -p /var/run/sshd && ssh-keygen -A RUN echo 'root:123456' | chpasswd # 显式开启 root + 密码登录,避免不同基础镜像默认值差异 # PermitRootLogin yes:允许 root 登录 # PasswordAuthentication yes:允许密码认证 # KbdInteractiveAuthentication no:关闭交互式认证(避免和密码认证混杂) # UsePAM no:关闭 PAM(容器场景常用于减少兼容问题) RUN sed -ri 's/^#?PermitRootLogin\s+.*/PermitRootLogin yes/' /etc/ssh/sshd_config \ && sed -ri 's/^#?PasswordAuthentication\s+.*/PasswordAuthentication yes/' /etc/ssh/sshd_config \ && sed -ri 's/^#?KbdInteractiveAuthentication\s+.*/KbdInteractiveAuthentication no/' /etc/ssh/sshd_config \ && sed -ri 's/^#?UsePAM\s+.*/UsePAM no/' /etc/ssh/sshd_config # 暴露 SSH 端口 EXPOSE 22 # 设置容器启动时自动运行 SSH 服务 CMD ["/usr/sbin/sshd", "-D"] -
在
dockerfile文件所在的文件夹中打开终端,输入如下命令构建一个镜像,镜像名字叫ubuntu-with-sshpowershelldocker build -f dockerfile -t ubuntu-with-ssh . -
在终端中输入如下命令,将根据镜像运行一个容器,容器名字叫
ubuntu-sshpowershelldocker run -d -p 2222:22 --name ubuntu-ssh ubuntu-with-ssh -
在终端中使用如下命令,根据提示输入密码,就可以连接ubuntu 了
- 注意:端口用的是主机的
2222端口
powershellssh root@127.0.0.1 -p 2222- 如果是使用软件连的,可以参考图片

- 注意:端口用的是主机的
在Docker 建立一个SSH连接的 Ubuntu 容器
草木红2026-05-15 8:37
相关推荐
EterNity_TiMe_44 分钟前
一个网页管理多种远程连接:Docker 部署 Next Terminal 与资产审计蒲锘1 小时前
DevOps 实验阶段四:Infra 节点 + MySQL + Spring Boot 容器化是枚小菜鸡儿吖5 小时前
把视频教程变成可复习的笔记:Docker 部署 BiliNote,自动转写与总结ICollection6 小时前
4060笔记本用Docker部署ComfyUI绘图淼澄研学13 小时前
Proliferate开源框架技术解析与Docker部署实操浪兎兎14 小时前
Docker 容器技术笔记Zhu75815 小时前
在Docker环境离线部署最新版Harbor敲代码还房贷16 小时前
VMware17 + Ubuntu22.04 共享 完整步骤qq_11360149351 天前
CopyEscape Docker容器逃逸(CVE-2026-17106)