1、用户密码认证方式登陆
hsl@ubuntu:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hsl/ubuntu 14.04_add_sourcealiyun_git_vim_ssh 7e81fb2f82c5 44 minutes ago 634.2 MB
hsl/ubuntu 14.04_JenkinsWithDocker_key 6fb1d3cb7983 46 hours ago 760 MB
hsl/ubuntu 14.04_JenkinsWithDocker_password 659fcb00b0dc 3 days ago 760.1 MB
ubuntu 14.04 4a725d3b3b1c 8 days ago 188 MB
training/webapp latest 6fae60ef3446 15 months ago 348.8 MB
hsl@ubuntu:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
hsl@ubuntu:~$ sudo docker run -tid -p 22 -P hsl/ubuntu:14.04_add_sourcealiyun_git_vim_ssh
ea153153c8837a4b0e1a8d0e6945200c7ac820c358d546202f1a95c72f12ca77
hsl@ubuntu:~$ sudo docker exec -ti ea /bin/bash
root@ea153153c883:/# cd /etc/ssh
root@ea153153c883:/etc/ssh# vim sshd_config
sshd_config 需要关注三个地方,未修改之前是这样:
PermitRootLogin without-password
#AuthorizedKeysFile %h/.ssh/authorized_keys
#PasswordAuthentication yes
说明:
#PermitRootLogin yes #允许root用户以任何认证方式登录(用户名密码认证和公钥认证)
#PermitRootLogin without-password #只允许root用公钥认证方式登录
#PermitRootLogin no #不允许root用户以任何认证方式登录
这里先修改两处:
PermitRootLogin without-password 改为 PermitRootLogin yes
#PasswordAuthentication yes 改为 PasswordAuthentication yes
root@ea153153c883:/etc/ssh# cd
root@ea153153c883:~# service ssh start
* Starting OpenBSD Secure Shell server sshd [ OK ]
root@ea153153c883:/# passwd root
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
root@ea153153c883:/# ifconfig
#获得docker的内网地址(inet addr):172.17.0.2
root@ea153153c883:/# exit
exit
hsl@ubuntu:~$ ssh root@172.17.0.2
root@172.17.0.2's password:
Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.13.0-93-generic x86_64)
* Documentation: https://help.ubuntu.com/
The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
root@ea153153c883:~# exit
logout
Connection to 172.17.0.2 closed.
hsl@ubuntu:~$ ifconfig
#获得宿主机的内网地址(inet地址):192.168.127.159,这里试一下端口映射方式登陆docker
#因为做了端口映射,所以可以直接从映射的端口登陆,只需要知道和docker的22端口映射的宿主机端口和宿主机的ip(如果和docker的22做端口映射时候采用默认IP方式,则默认宿主机的所有IP都和docker的22端口映射,这样localhost和子网IP均可等登陆)
#即ssh root@172.17.0.2和ssh -p 32770 root@192.168.127.159都可以登陆docker
hsl@ubuntu:~$ ssh -p 32771 root@192.168.127.159
#第一次登陆有警告
The authenticity of host '[192.168.127.159]:32771 ([192.168.127.159]:32771)' can't be established.
ECDSA key fingerprint is SHA256:icDOU4lcWTiFb4eIKUtosFNrqzGMo5ufzqXQfPdtSZg.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.127.159]:32771' (ECDSA) to the list of known hosts.
root@192.168.127.159's password:
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 4.4.0-36-generic x86_64)
* Documentation: https://help.ubuntu.com/
Last login: Sun Sep 4 06:00:37 2016 from 172.17.0.1
root@ea153153c883:~# exit
logout
Connection to 192.168.127.159 closed.
hsl@ubuntu:~$ ssh -p 32771 root@192.168.127.159
#再登录一遍就没有警告了
root@192.168.127.159's password:
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 4.4.0-36-generic x86_64)
* Documentation: https://help.ubuntu.com/
Last login: Sun Sep 4 06:01:33 2016 from 192.168.127.159
root@ea153153c883:~# exit
logout
Connection to 192.168.127.159 closed.
2、公钥认证方式登陆
hsl@ubuntu:~$ sudo docker run -tid -p 22 -P hsl/ubuntu:14.04_add_sourcealiyun_git_vim_ssh
f2e54200c5c7f3310a27e274d7d8c9585ed3c79f921cb63edb8f3a00d1165e24
hsl@ubuntu:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f2e54200c5c7 hsl/ubuntu:14.04_add_sourcealiyun_git_vim_ssh "/bin/bash" 15 seconds
ago Up 10 seconds 0.0.0.0:32768->22/tcp big_brown
hsl@ubuntu:~$ sudo docker exec -it f2 /bin/bash
root@f2e54200c5c7:/# vim /etc/ssh/sshd_config
把第一步中提到的需要注意的三个地方做以下修改:
PermitRootLogin without-password
#AuthorizedKeysFile %h/.ssh/authorized_keys改为AuthorizedKeysFile %h/.ssh/authorized_keys
#PasswordAuthentication yes改为PasswordAuthentication yes
(如果服务器不在本地,千万不能PasswordAuthentication yes->no,万一当前的ssh链接中断,万一RAS认证没弄好,密码验证又禁止了。可以理解为公钥认证优先于用户密码认证,但是万一公钥认证失败,用用户密码认证以防万一)
root@f2e54200c5c7:/# exit
exit
hsl@ubuntu:~$ ssh-keygen -t rsa
#一直回车,生成宿主机的密钥
hsl@ubuntu:~$ cd .ssh
hsl@ubuntu:~/.ssh$ ls
id_rsa id_rsa.pub
hsl@ubuntu:~/.ssh$ scp id_rsa.pub root@172.17.0.2:~/.ssh/
root@172.17.0.2's password:
id_rsa.pub 100% 392 0.4KB/s 00:00
hsl@ubuntu:~/.ssh$ sudo docker exec -it f2 /bin/bash
root@ea153153c883:/# cd
root@ea153153c883:~# cd .ssh
root@ea153153c883:~/.ssh# ls
id_rsa.pub
root@ea153153c883:~/.ssh# mv id_rsa.pub authorized_keys
root@ea153153c883:~/.ssh# ls
authorized_keys
********可以替换上面的通过scp方法把公钥传送到docker*********
#或者直接把宿主机的id_rsa.pub内容复制到docker的/root/.ssh/authorized_keys
hsl@ubuntu:~$ cd .ssh
hsl@ubuntu:~/.ssh$ ls
id_rsa id_rsa.pub known_hosts
hsl@ubuntu:~/.ssh$ cat id_rsa.pub
#宿主机的公钥
hsl@ubuntu:~/.ssh$ sudo docker exec -it f2 /bin/bash
root@f2e54200c5c7:/# ssh-keygen -t rsa
#这一步只是为了方便产生docker的.ssh目录
root@f2e54200c5c7:/# cd
root@f2e54200c5c7:~# cd .ssh
root@f2e54200c5c7:~/.ssh# ls
id_rsa id_rsa.pub
root@f2e54200c5c7:~/.ssh# touch authorized_keys
root@f2e54200c5c7:~/.ssh# ls
authorized_keys id_rsa id_rsa.pub
root@f2e54200c5c7:~/.ssh# vim authorized_keys
#把宿主机的id_rsa.pub内容复制到docker的/root/.ssh/authorized_keys
********可以替换上面的通过scp方法把公钥传送到docker*********
root@f2e54200c5c7:/# service ssh start
* Starting OpenBSD Secure Shell server sshd [ OK ]
root@f2e54200c5c7:/# exit
exit
hsl@ubuntu:~/.ssh$ ssh root@172.17.0.2
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 4.4.0-36-generic x86_64)
* Documentation: https://help.ubuntu.com/
Last login: Sun Sep 4 08:18:21 2016 from 172.17.0.1
root@f2e54200c5c7:~# exit
logout
Connection to 172.17.0.2 closed.
分享到:
相关推荐
远程端口转发工具,用于访问Docker容器内的服务
Docker容器的SSH服务器〜因为每个容器都应可访问。 是否想立即通过SSH进入您的容器? 干得好: $ docker run -d -p 2222:22 \ -v /var/run/docker.sock:/var/run/docker.sock \ -e FILTERS={\"name\":[\"^/my-...
Shipspotter一个远程端口转发工具,用于访问Docker容器内的服务。 那些私有管理界面,JMX端口或Erlang / Elixir分发端口不对外开放? 没问题。 Shipspotter aut Shipspotter一个远程端口转发工具,用于访问Docker...
Freighter的目标是简化在ssh上部署docker容器的过程。 Freighter使用一个YAML文件来描述您环境中的环境,服务器,图像和容器。 货机目标: 简单的Docker容器部署 直截了当的配置 刚接触货机的用户应能在几分钟内...
通过提供的多种平台通知您与Docker swarm一起使用以更新最新可用版本的服务限制您的服务器SSH访问有助于保持第三方容器的最新状态入门可以找到更详细的用法和配置。Docker容器通过docker映像将Dockupdater部署在...
通过maven编译、打包,然后通过执行shell脚本使docker构建镜像并push到私服(或者阿里云)仓库,此操作完成后jenkins服务器上再执行SSH命令登录到部署服务器,docker从仓库(私服)拉取镜像,启动容器。整个操作流程...
Gollum Docker 容器从Docker容器在机架网络服务器中运行 gollum/gollum。安装 git clone https://github.com/hrzg/docker-gollum.gitcd docker-gollum在开始使用此容器之前,您必须准备一些包含 SSH 密钥和所需配置...
Autossh 高度可定制的AutoSSH docker容器。概述jnovack / autossh是一个小型的轻量级映像(约15MB),尝试提供一种安全的方式来建立SSH隧道,而无需在映像本身中包含密钥或链接到主机。 有数千个autossh docker容器...
如何构建如果您的开发环境中没有 Docker,您可以使用提供的 VM: vagrant upvagrant ssh然后构建Docker容器: sudo docker build -t="uncharted/mitie-server" .怎么跑使用 Docker 运行服务器: sudo docker run -it...
在一个容器中运行SSH服务器,这真的是一个错误(大写字母W)吗?老实说,没那么严重。当你不去访问Docker主机的时候,这样做甚至是极其方便的,但是这仍然需要在容器中取得一个shell
将您的公钥添加到容器的 SSH 服务器以便于访问 向主机公开端口22 (SSH) 和 5432 (PostgreSQL) 与主机和其他 Docker 容器共享 PostgreSQL数据目录和日志文件 链接: 我网站中的 建造 $ docker build -t nimiq/...
38-通过网络访问容器 39-容器间通信的实现 40-网络配置的查看 41-容器命名 42-容器连接1 43-容器连接2 44-Dockerfile的使用 45-实训项目1--快速创建SSH服务镜像 46-实训项目1--快速创建SSH服务镜像 47-实训项目1--...
38-通过网络访问容器 39-容器间通信的实现 40-网络配置的查看 41-容器命名 42-容器连接1 43-容器连接2 44-Dockerfile的使用 45-实训项目1--快速创建SSH服务镜像 46-实训项目1--快速创建SSH服务镜像 47-实训项目1--...
问题描述: 在执行docker run -p 19918:19918/tcp -v /etc/localtime:/etc/localtime时候后 ...补充知识:docker中开启时运行多个不同进程,安装ssh,并在启动docker时与jenkins同时启动运行 最近在做
测试项目以部署带有无业游民的Docker容器 Windows的要求 虚拟盒子 Boot2Docker(可选,如果要在无业游民的情况下测试Docker容器) 流浪汉 使用cygwin安装rsync和ssh软件包 对Linux的要求 虚拟盒子 流浪汉 同步 ssh ...
38-通过网络访问容器 39-容器间通信的实现 40-网络配置的查看 41-容器命名 42-容器连接1 43-容器连接2 44-Dockerfile的使用 45-实训项目1--快速创建SSH服务镜像 46-实训项目1--快速创建SSH服务镜像 47-实训项目1--...
38-通过网络访问容器 39-容器间通信的实现 40-网络配置的查看 41-容器命名 42-容器连接1 43-容器连接2 44-Dockerfile的使用 45-实训项目1--快速创建SSH服务镜像 46-实训项目1--快速创建SSH服务镜像 47-实训项目1--...
38-通过网络访问容器 39-容器间通信的实现 40-网络配置的查看 41-容器命名 42-容器连接1 43-容器连接2 44-Dockerfile的使用 45-实训项目1--快速创建SSH服务镜像 46-实训项目1--快速创建SSH服务镜像 47-实训项目1--...
38-通过网络访问容器 39-容器间通信的实现 40-网络配置的查看 41-容器命名 42-容器连接1 43-容器连接2 44-Dockerfile的使用 45-实训项目1--快速创建SSH服务镜像 46-实训项目1--快速创建SSH服务镜像 47-实训项目1--...