Win11下为ubuntu镜像添加SSH服务

很多时候,系统管理员都习惯通过SSH服务来远程登陆管理Linux服务器,但是Docker的很多镜像是不带SSH的,当需要远程登录到容器进行一些操作的时候,就需要SSH的支持了。这里介绍如何自行创建一个带有SSH服务的镜像。以下以Ubuntu docker镜像为例:

整体思路为:

1.从镜像仓库获取标准的Ubuntu镜像。

2.启动Ubuntu镜像,并在Ubuntu容器中安装SSH服务。

3.将安装好SSH服务的Ubuntu镜像commit为新的docker镜像。

4.启动镜像。

5.在其他主机通过SSH方式连接启动的容器。

一、使用docker安装Ubuntu

Ubuntu是以桌面应用为主的GNU/Linux开源操作系统,官方译名“友邦拓”,另有“乌班图”等译名。Ubuntu每6个月会发布一个新版本,每两年会推出一个长期支持(Long Term Support,LTS)版本,一般支持3年时间。

1.下载镜像,这里下载的是最新版本的镜像:

docker pull ubuntu

2.启动容器:

docker run -it ubuntu bash

3.查看ubuntu镜像的发行版本号:

root@880d2c983b81:/# cat /etc/lsb-release

DISTRIB_ID=Ubuntu

DISTRIB_RELEASE=22.04

DISTRIB_CODENAME=jammy

DISTRIB_DESCRIPTION="Ubuntu 22.04.1 LTS"

二、安装和配置SSH服务 ##

首先进入容器内部

1.选择openshh-server作为服务端,安装openssh-server的命令如下:###

root@880d2c983b81:/# apt-get install openssh-server

2.如果要正常启动SSH服务,那么目录/var/run/sshd必须存在,下面手动创建:

root@880d2c983b81:/# mkdir -p /var/run/sshd

3.启动SSH服务:###

root@880d2c983b81:/# /usr/sbin/sshd -D &

4.此时查看容器的22端口,可见此端口已经处于监听状态:###

root@880d2c983b81:/# netstat -lntp

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address          Foreign Address        State      PID/Program name

tcp        0      0 0.0.0.0:22              0.0.0.0:*              LISTEN      7/sshd: /usr/sbin/s

tcp6      0      0 :::22                  :::*                    LISTEN      7/sshd: /usr/sbin/s

5.修改SSH服务的安全配置,取消pam登录限制:###

root@880d2c983b81:/# sed -ri 's/session required pam_loginuid.so/#session required pam_loginuid.so/g' /etc/pam.d/sshd

6.在root用户目录下创建.ssh目录,并复制需要登录的公钥信息到authorized_keys文件中:###

root@880d2c983b81:/# mkdir root/.ssh

root@880d2c983b81:/# ssh-keygen -t rsa

root@880d2c983b81:/# cp root/.ssh/id_rsa.pub root/.ssh/authorized_keys

7.创建自动启动SSH服务的可执行文件run.sh,并添加可执行权限:

root@880d2c983b81:/# vi run.sh

root@880d2c983b81:/# chmod +x run.sh

run.sh脚本内容如下:

#!/bin/bash

/usr/sbin/sshd -D

8.退出容器:

root@880d2c983b81:/# exit

exit

三、保存镜像

将容器用docker commit命令保存为一个新的sshd:ubuntu镜像:

docker commit 880d2c983b81 ubuntu:sshd

使用docker images查看本地目前的镜像列表:

PS C:\Users\hb> docker images

REPOSITORY  TAG      IMAGE ID      CREATED      SIZE

ubuntu      sshd      d1f8d099582a  2 days ago  281MB

ubuntu      latest    216c552ea5ba  9 days ago  77.8MB

四、启动镜像

启动容器,并添加端口映射10022~22,其中10022是宿主主机的端口,22是容器SSH服务监听的端口:

docker run -p 10022:22 -itd --name usshd ubuntu:sshd /run.s

启动成功后,可以在宿主机上看到容器运行的详细信息:

PS C:\Users\hb> docker ps

CONTAINER ID  IMAGE          COMMAND    CREATED      STATUS      PORTS                  NAMES

880d2c983b81  f7239f5c3bed  "/run.sh"  3 days ago  Up 3 hours  0.0.0.0:10022->22/tcp  usshd

在宿主主机或其他主机上,可以通过SSH访问10022端口来登录容器: 如下使用secureCRT客户端连接:


Welcome to Ubuntu 22.04.1 LTS (GNU/Linux 5.10.16.3-microsoft-standard-WSL2 x86_64)

* Documentation:  https://help.ubuntu.com

* Management:    https://landscape.canonical.com

* Support:        https://ubuntu.com/advantage

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 Oct 12 02:22:36 2022 from 172.17.0.1

root@880d2c983b81:~#

你可能感兴趣的:(Win11下为ubuntu镜像添加SSH服务)