ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

Docker 基于Ubuntu镜像添加SSH服务

2020-07-24 14:32:16  阅读:589  来源: 互联网

标签:sshd run 09c5e8c04b11 SSH Ubuntu Docker root ssh


这里我们用Ubuntu18.04作为基础镜像。

基于Commit命令创建:

1、准备工作

首先,获取Ubuntu:18.04镜像并创建一个容器:

docker pull ubuntu:18.04

docker run -it ubuntu:18.04 bash

root@09c5e8c04b11:~# 

2、配置软件源 

root@09c5e8c04b11:~#  apt-get update

3、安装和配置SSH服务

root@09c5e8c04b11:~#  apt-get install openssh-server

如果需要正常启动 SSH 服务, 则目录/var/run/sshd 必须存在 。 下面手动创建它,并启动SSH 服务:

root@09c5e8c04b11:~#  mkdir -p /var/run/sshd

root@09c5e8c04b11:~#  /usr/sbin/sshd -D &

安装net-tools查看容器22端口(SSH服务默认监听端口),可见端口已经处于监听状态。

root@09c5e8c04b11:~#  apt-get install net-tools

root@09c5e8c04b11:~#  netstat -tunlp

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.0:22          0.0.0.0:*       LISTEN           1/sshd
tcp6      0           0       :::22             :::*       LISTEN      1/sshd

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

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

在root目录下创建.ssh目录,并使用ssh-keygen命令生成秘钥。

给容器root设置密码

root@09c5e8c04b11:~#passwd root

Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

修改配置让root能登录

root@09c5e8c04b11:~#vim /etc/ssh/sshd_config

注释

# PermitRootLogin prohibit-password.

添加

PermitRootLogin yes

保存,退出,重启SSH服务

root@09c5e8c04b11:~#/etc/init.d/ssh restart

到目前可以通过密码ssh到容器

root@09c5e8c04b11:~# ifconfig

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.5 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:05 txqueuelen 0 (Ethernet)
RX packets 3184 bytes 349005 (349.0 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2908 bytes 341429 (341.4 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

root@09c5e8c04b11:~# exit

[root@5gd51 ~]#  ssh 172.17.0.5

root@172.17.0.5's password:输入密码可以进入

root@09c5e8c04b11:~# exit 

退出,复制需要登录的公钥信息到 authorized_keys文件中

[root@5gd51 ~]#ssh-copy-id -i /root/.ssh/id_rsa.pub 172.17.0.5

这种方式是为了保证文件权限准确

[root@5gd51 ~]#ssh 172.17.0.5

root@09c5e8c04b11:~#

OK,到此SSH服务成功配置。

然后,在容器中新建脚本/run.sh

root@09c5e8c04b11:~# apt-get install vim 

root@09c5e8c04b11:~#  vim /run.sh

run.sh脚本如下:

#!/bin/bash

/usr/sbin/sshd -D

root@09c5e8c04b11:~#chmod +x /run.sh

最后,退出容器

root@09c5e8c04b11:~# exit

4、保存镜像

[root@5gd51 ~]#docker commit  09c5e8c04b11 sshd:ubuntu

sha256:5998511380c52caa60a54fa75a28dc258f387b72aea41ffaa9c8c2ad209c8399

[root@5gd51 ~]#docker images

REPOSITORY   TAG   IMAGE ID         CREATED     SIZE
sshd         ubuntu  5998511380c5       20 hours ago    245MB

5、使用镜像

[root@5gd51 ~]#docker run -p 10022:22 -d  sshd:ubuntu  /run.sh

3bfc78853f924097ab494c11683cfc444402df1c370df02d1d791bb3dca736a8

[root@5gd51 ~]# ssh 101.33.22.44(公网IP) -p 10022

Welcome to Ubuntu 18.04.4 LTS (GNU/Linux 3.10.0-693.el7.x86_64 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: Fri Jul 24 05:59:55 2020 from 172.17.0.1

root@3bfc78853f92:~#

成功用SSH连接

参考:《Docker技术入门与实战第三版》

博客:https://blog.csdn.net/qq_27068845/article/details/77015432

标签:sshd,run,09c5e8c04b11,SSH,Ubuntu,Docker,root,ssh
来源: https://www.cnblogs.com/autumn007/p/13371740.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有