在docker容器中开启ssh服务 (未成功有时间再验证)
一、
利用ubuntu:14.04镜像创建一个容器:
[root@docker ~]# docker run -it ubuntu:14.04 /bin/bash root@161f67ccad50:/#
更新apt缓存:
root@161f67ccad50:/# apt-get update
2.安装和配置SSH服务
选择主流的openssh-server作为服务端:
root@161f67ccad50:/# apt-get install openssh-server net-tools passwd -y Reading package lists... Done Building dependency tree Reading state information... Done openssh-server is already the newest version. 0 upgraded, 0 newly installed, 0 to remove and 6 not upgraded. root@161f67ccad50:/#
#启动服务
root@1b7d84e1e8ea:/# /usr/sbin/sshd -D &
[1] 9489
root@1b7d84e1e8ea:/# netstat -lntup|grep 22
tcp 0 0 127.0.0.1:9229 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 29/sshd
tcp6 0 0 :::22 :::* LISTEN 29/sshd
[1]+ Exit 255 /usr/sbin/sshd -D
#查看ip地址
root@1b7d84e1e8ea:/# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:ac:11:00:02
inet addr:172.17.0.2 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::42:acff:fe11:2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:18536 errors:0 dropped:0 overruns:0 frame:0
TX packets:17649 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:26895955 (26.8 MB) TX bytes:3361191 (3.3 MB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:36302 errors:0 dropped:0 overruns:0 frame:0
TX packets:36302 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:36705585 (36.7 MB) TX bytes:36705585 (36.7 MB)
#
root@1b7d84e1e8ea:/# passwd root Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully root@1b7d84e1e8ea:/# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:4BAdfRsQ6TtubD4cceb6T4kN7ZZnDtyey4pH/OWvx7A root@1b7d84e1e8ea The key's randomart image is: +---[RSA 2048]----+ | ...oo+ | | .. o o | | . .. . o | | o .o +. | | . S*... | | + .*o+. .| | + +..X.+* | | O +.BE.+| | +.oooo.B+o| +----[SHA256]-----+ root@1b7d84e1e8ea:/# root@1b7d84e1e8ea:/# root@1b7d84e1e8ea:/# root@1b7d84e1e8ea:/# cd ~/.ssh/ root@1b7d84e1e8ea:~/.ssh# ls id_rsa id_rsa.pub root@1b7d84e1e8ea:~/.ssh# cp id_rsa.pub authorized_keys
#参考:
https://www.cnblogs.com/jie-fang/p/7928406.html
https://www.cnblogs.com/kevingrace/p/6526990.html