前言

本地化启动一个 Linux 服务器,使用 Docker 容器来构建一个包含 SSH 服务的 Linux 镜像。

准备 centos 官方镜像

具体步骤:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 1、拉取centos7官方镜像
docker pull centos:7
# 2、启动镜像
docker run -itd --privileged centos:7 init
# 3、进入容器bash中
docker exec -it 镜像ID bash
# 4、修改root用户密码
[root@33c7ee24f43e /]# passwd
Changing password for user root.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
# 5、安装和配置ssh服务
yum install openssh-server -y
# 6、修改/etc/ssh/sshd_config配置并保存:PermitRootLogin yes UsePAM no

image-20220223145827608

image-20220223145949729

1
2
3
4
5
# 7、启动ssh服务
[root@33c7ee24f43e /]# systemctl start sshd
# 8、退出容器
[root@33c7ee24f43e /]# exit
exit

构建镜像

通过 docker commit 来构建包含 ssh 服务的镜像

1
2
3
4
# 1、通过commit构建镜像
docker commit -a="Irving" -m="本地linux镜像" 容器ID mycentos:1.0
# 2、启动镜像 -p 10000:22 映射本地10000端口
docker run -d -p 10000:22 --name 容器名 镜像ID /usr/sbin/sshd -D

使用 xshell 进行连接

1
ssh://root:****@127.0.0.1:10000