docker安裝ssh
通過命令行安裝
1.pull ubuntu鏡像
docker pull ubuntu:latest
2.啓動並進入bash
docker run -it -d ubuntu:lastest /bin/bash
# 查看剛剛運行容器的id
docker ps
# 在容器中執行bash命令
docker exec -it id /bin/bash
3.安裝openssh-server並啓動
apt-get update
apt-get install openssh-server
# 啓動之前需手動創建/var/run/sshd,不然啓動sshd的時候會報錯
mkdir -p /var/run/sshd
# sshd以守護進程運行
/usr/sbin/sshd -D &
# 安裝netstat,查看sshd是否監聽22端口
apt-get install net-tools
netstat -apn | grep ssh
如果已經監聽22端口,說明sshd服務啓動成功
4.ssh登陸
# 生成ssh key
ssh-keygen -t rsa
# 修改sshd-config允許root登陸
sed -i 's+PermitRootLogin prohibit-password+PermitRootLogin yes' /etc/ssh/sshd-config
修改完sshd-config之後需要重啓sshd服務
// 找到pid
ps -aux | grep ssh
kill -9 pid
/usr/sbin/sshd -D &
查看容器ip
ifconfig
在主機上進行登陸
ssh root@ip
就可以登錄成功了,但是注意這裏是docker容器的宿主機才能登陸成功,如果需要其他機器登陸,可以在啓動docker的時候進行端口映射
// 11122宿主機端口,22爲容器端口
docker run -it -p 11122:22
// 在其他機器上可以使用以下命令登陸,假設宿主機ip爲192.168.1.101
ssh -p 11122 [email protected]
5.保存容器
// 把id爲id的容器保存
docker commit id sshd:ubuntu
// 停止容器
docker stop id
通過dockerfile
# 以最新的Ubuntu鏡像爲模板
FROM ubuntu:latest
// 將本目錄下的sources.list作爲容器的一個文件
ADD sources.list /root/sources.list
// 使用阿里Ubuntu源,更新快
RUN cp /root/sources.list /etc/apt/sources.list.d/aliyun.list
RUN apt-get update
RUN apt-get install -y openssh-server
RUN apt-get install -y net-tools
RUN apt-get install -y vim
RUN mkdir -p /var/run/sshd
RUN mkdir -p mkdir/root/.ssh/
// 修改root密碼,便於遠程登錄
RUN echo root:123456 | chpasswd
// 將key生成在指定文件內
RUN ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -P '' -N ''
// 配置ssh可以使用root登陸
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
// 開放22端口
EXPOSE 22
CMD /usr/sbin/sshd -D &
根據Dockerfile build鏡像
docker build -t nginx:ubuntu .
[root@localhost ~]# docker p_w_picpaths
REPOSITORY TAG IMAGE ID CREATED SIZE
centos7 7.3.1611 d5ebea14da54 2 weeks ago 311 MB
<none> <none> d5c154b612c8 2 weeks ago 311 MB
test latest ecefde07358f 11 weeks ago 599.6 MB
learn/ping latest fea07d84b0df 4 months ago 196.7 MB
docker.io/tomcat latest ebb17717bed4 4 months ago 355.4 MB
docker.io/centos latest 980e0e4c79ec 6 months ago 196.7 MB
nginx 1.9 c8c29d842c09 9 months ago 182.7 MB
docker.io/redis 2.8.19 dd9fe7db5236 22 months ago 110.7 MB
[root@localhost ~]# docker run -i -t centos7:7.3.1611 /bin/bash
[root@a3c8baf6961e /]# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
[root@a3c8baf6961e /]# yum install wget vim
[root@a3c8baf6961e /]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
安裝ssh服務端
[root@a3c8baf6961e /]# yum cleal all
[root@a3c8baf6961e /]# yum install passwd
[root@a3c8baf6961e /]# yum install openssh-server
修改容器密碼(提前yum -y reinstall cracklib-dicts)
[root@a3c8baf6961e /]# echo "123456" |passwd --stdin root
產生公私鑰
[root@a3c8baf6961e /]# ssh-keygen -t rsa //一路回車
[root@a3c8baf6961e /]# cd ~/.ssh/
[root@a3c8baf6961e .ssh]# ls
id_rsa id_rsa.pub
[root@a3c8baf6961e .ssh]# cp id_rsa.pub authorized_keys
[root@a3c8baf6961e .ssh]# ls
authorized_keys id_rsa id_rsa.pub
執行sshd命令,有報錯:
[root@a3c8baf6961e .ssh]# /usr/sbin/sshd
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_dsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
解決辦法:
[root@a3c8baf6961e .ssh]# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key //均是一路回車
[root@a3c8baf6961e .ssh]# ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
[root@a3c8baf6961e .ssh]# ssh-keygen -t rsa -f /etc/ssh/ssh_host_ecdsa_key
[root@a3c8baf6961e .ssh]# ssh-keygen -t rsa -f /etc/ssh/ssh_host_ed25519_key
再次執行sshd命令,如果沒有報錯,說明可以啓動了
[root@a3c8baf6961e .ssh]# /usr/sbin/sshd
[root@a3c8baf6961e .ssh]#
-----------------------啓動ssh,如果報錯如下(這是centos7下的一個bug)-------------------------
[root@a3c8baf6961e .ssh]# systemctl restart sshd.service
Failed to get D-Bus connection: Operation not permitted
這個報錯在之前的文檔裏就已經提到過了
解決辦法如下:
先把上面的容器關閉(docker stop container-id),然後重新啓動容器,啓動時加上參數--privileged(特權參數,也可以是--privileged=true,如果啓動容器中掛載目錄沒有權限也可以添加此參數)和/sbin/init(代替/bin/bash),如下:
[root@localhost ~]# docker run --privileged -i -t centos7:7.3.1611 /sbin/init
上面的容器啓動後,會一直在卡着的狀態中,先不用管,打開另一個終端窗口,查看容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
af40bd07fa0f centos7:7.3.1611 "/sbin/init" 28 seconds ago Up 28 seconds nauseous_shirley
然後按照容器的ID進去,這個時候再根據/bin/bash進入容器(前面加exec -it參數),接着重啓ssh服務就ok了
[root@localhost ~]# docker exec -it af40bd07fa0f /bin/bash
[root@af40bd07fa0f /]# systemctl restart sshd.service
[root@af40bd07fa0f /]# echo "123456" |passwd --stdin root
//注意這裏由於上述特殊情況重新啓動了容器,之前創建的root密碼無效了(這就相當於重新另起了一個容器),需要重新修改下root密碼!!可以隨便創建個用戶,然後切換到root,測試下之前創建的root密碼是否還有效!
--------------------------------------------------------------------------------------------------
查看ssh端口,發現22端口已經開啓
[root@af40bd07fa0f /]# ss -a|grep ssh
tcp LISTEN 0 128 *:ssh *:*
tcp LISTEN 0 128 :::ssh :::*
[root@af40bd07fa0f /]# ss -ln|grep 22
u_dgr UNCONN 0 0 * 26884224 * 26885412
tcp LISTEN 0 128 *:22 *:*
tcp LISTEN 0 128 :::22 :::*
然後docker ps查看下容器,提交更改爲新鏡像,運行新的鏡像
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
af40bd07fa0f centos7:7.3.1611 "/sbin/init" 21 minutes ago Up 21 minutes nauseous_shirley
記住這個容器ID,然後關閉
[root@localhost ~]# docker stop af40bd07fa0f
af40bd07fa0f
接着提交改爲新的鏡像,使用上一步的容器ID,提交名爲wangssh的鏡像(提交成功後,之前創建的容器可以選擇刪除(docker ps -a 查看);當然不刪除也不影響。
建議不要刪除,可以再次啓用提交新的鏡像以便他用。)
[root@localhost ~]# docker commit af40bd07fa0f wangssh
sha256:ca5e393b7605949e58c1067c1bc73d99d52f47107756f0ade1725ca04886fd71
[root@localhost ~]#
提交成功後,使用docker p_w_picpaths可以查看到
[root@localhost ~]# docker p_w_picpaths
REPOSITORY TAG IMAGE ID CREATED SIZE
wangssh latest ca5e393b7605 57 seconds ago 327.1 MB
centos7 7.3.1611 d5ebea14da54 2 weeks ago 311 MB
然後運行新的鏡像
[root@localhost ~]# docker run -d -p 220:22 wangssh /usr/sbin/sshd -D
b0a845a3dedeac7b46002d1c8514077309d88dcc0667b7080bc1ab67d70eb167
docker: Error response from daemon: Cannot start container b0a845a3dedeac7b46002d1c8514077309d88dcc0667b7080bc1ab67d70eb167: [9] System error: SELinux policy denies access..
如上出現上面的報錯,這是由於selinux造成的!需要關閉selinux,如下:
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive
然後再次運行新的鏡像,就成功了!
[root@localhost ~]# docker run -d -p 220:22 wangssh /usr/sbin/sshd -D
0a7c1406361ef52dcc5c32801e4c7c231078594cd7010375ea33fe3024cc9126
[root@localhost ~]#
上面運行命令中的參數解釋:
-d 後臺運行容器
-p 容器端口映射到主機[可選]
使用docker ps查看運行的容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0a7c1406361e wangssh "/usr/sbin/sshd -D" About a minute ago Up About a minute 0.0.0.0:220->22/tcp focused_hawking
此時你可以直接連接容器,也可以通過端口映射連接容器(使用之前創建的容器密碼123456登陸)
[root@localhost ~]# ssh -p220 root@localhost
root@localhost's password:
[root@0a7c1406361e ~]#
------------------------------------------------------------------------------------------------------------------
如果要想做ssh無密碼登陸的信任關係,只需要將物理機本地的~/.ssh/id_rsa.pub拷貝到容器裏的~/.ssh/authorized_keys即可
接着上面ID爲aea267757cc9的容器登陸後的操作:
[root@localhost ~]# docker exec -it aea267757cc9 /bin/bash
[root@aea267757cc9 /]# ssh-keygen -t rsa //一路回車
將物理機本地的~/.ssh/id_rsa.pub拷貝到容器裏
[root@localhost ~]# docker cp ~/.ssh/id_rsa.pub aea267757cc9:/root/.ssh/
然後到容器裏將id_rsa.pub拷貝爲authorized_keys
[root@aea267757cc9 /]# cd ~
[root@aea267757cc9 ~]# cd .ssh/
[root@aea267757cc9 .ssh]# cp id_rsa.pub authorized_keys
接着提交爲新鏡像
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aea267757cc9 centos7:7.3.1611 "/sbin/init" About an hour ago Up 38 minutes admiring_hodgkin
fc726a6a27d2 centos "/bin/bash" 3 months ago Up 3 months 0.0.0.0:32772->80/tcp web1
9d99c7b9451b centos "/bin/bash" 3 months ago Up 3 months 0.0.0.0:32769->8080/tcp web3
[root@localhost ~]# docker stop aea267757cc9
aea267757cc9
[root@localhost ~]# docker commit aea267757cc9 hahassh
sha256:906bf1bd2a156b1222def7d3d21fbc2cd7e963fc923f5a6da92e6b45954688d9
[root@localhost ~]# setenforce 0
[root@localhost ~]# docker run -d -p 220:22 hahassh /usr/sbin/sshd -D
8b9c153463c73122cfd787a27190a8665f54fe77fa51601d521baab5a9234f2e
最後嘗試ssh方式連接容器,發現可以無密碼登陸了~
[root@localhost ~]# ssh -p220 root@localhost
Last login: Mon Mar 13 10:03:54 2017
---------------------------------------------------------------------------------------------------------------------
當登陸到容器後,可以查看下容器ip
第一種方式:
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b220fabf815a wangssh "/usr/sbin/sshd -D" 6 hours ago Up About an hour 0.0.0.0:20020->22/tcp gigantic_goldwasser
fc726a6a27d2 980e0e4c79ec "/bin/bash" 3 months ago Up About an hour 0.0.0.0:32768->80/tcp web1
9d99c7b9451b 980e0e4c79ec "/bin/bash" 3 months ago Up About an hour 0.0.0.0:32769->8080/tcp web3
[root@localhost ~]# docker inspect b220fabf815a |grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
第二種方式:
[root@localhost ~]# docker inspect --format='``.`NetworkSettings`.`IPAddress`' b220fabf815a
172.17.0.2
第三種方式:
登陸到容器裏使用“yum install net-tools”,安裝後就可以使用ifconfig命令查看ip了
當知道了容器的ip後,就可以使用ssh直接連接容器的22端口即可!
[root@localhost ~]# ssh 172.17.0.2
[email protected]'s password:
Last login: Tue Mar 14 09:11:27 2017 from 172.17.0.1
[root@b220fabf815a ~]#
一. 從docker hub 下載centos 官方鏡像
hr:centos7 hr$ docker pull centos:7
下載完後,查看本地資源庫:
hr:centos7 hr$ docker p_w_picpaths
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos 7 ce20c473cd8a 7 weeks ago 172.3 MB
運行容器
hr:centos7 hr$ docker run -i -t centos:7 /bin/bash
二. 安裝passwd,openssl,openssh-server
[root@b5926410fe60 /]# yum install passwd openssl openssh-server -y
啓動sshd:
# /usr/sbin/sshd -D
這時報以下錯誤:
[root@ b5926410fe60 /]# /usr/sbin/sshd
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
執行以下命令解決:
[root@b5926410fe60 /]# ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ''
[root@b5926410fe60 /]# ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
[root@b5926410fe60 /]# ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key -N ''
然後,修改 /etc/ssh/sshd_config 配置信息:
UsePAM yes 改爲 UsePAM no
UsePrivilegeSeparation sandbox 改爲 UsePrivilegeSeparation no
[root@b5926410fe60 /]# sed -i "s/#UsePrivilegeSeparation.*/UsePrivilegeSeparation no/g" /etc/ssh/sshd_config
[root@b5926410fe60 /]# sed -i "s/UsePAM.*/UsePAM no/g" /etc/ssh/sshd_config
修改完後,重新啓動sshd
[root@b5926410fe60 /]# /usr/sbin/sshd -D
三. 修改root 密碼
[root@b5926410fe60 /]# passwd root
四. 查看容器ip地址(如果宿主機是linux操作系統則跳過這一步)
[root@b5926410fe60 /]# ip addr ls eth0
84: eth0@if85: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::42:acff:fe11:2/64 scope link
valid_lft forever preferred_lft forever
五. 將當前容器保存爲鏡像
hr:centos7 hr$ docker ps -all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b5926410fe60 centos:7 "/bin/bash" 4 minutes ago Exited (0) 4 seconds ago centos7ssh
hr:centos7 hr$ docker commit b5926410fe60 herong/centos7-ssh
六. 在宿主機上基於新創建的鏡像啓動新的容器
--先刪除之前的容器
hr:centos7 hr$ docker ps -all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4122f818a741 herong/centos7-ssh:latest "/usr/sbin/sshd" 13 seconds ago Exited (0) 13 seconds ago happy_mclean
hr:centos7 hr$ docker rm -f 4122f818a741
--基於新鏡像運行容器
hr:centos7 hr$ docker run -d -p 10022:22 herong/centos7-ssh:latest /usr/sbin/sshd -D
--查看映射端口是否成功
hr:centos7 hr$ docker ps -all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4966d35fe0a3 herong/centos7-ssh:latest "/usr/sbin/sshd -D" 3 seconds ago Up 3 seconds 0.0.0.0:10022->22/tcp compassionate_kowalevski
hr:centos7 hr$ docker port 4966d35fe0a3
22/tcp -> 0.0.0.0:10022
七. 從宿主機連接到容器
w 如果宿主機是非linux操作系統,則需要通過docker-machine ip連到容器
-- 查看docker-machine Ip地址
hr:centos7 hr$ docker-machine ip default
192.168.99.100
--通過docker-machine ip 連接到容器,輸入之前設置的密碼即可登錄成功
hr:centos7 hr$ ssh [email protected] -p 10022
The authenticity of host '[192.168.99.100]:10022 ([192.168.99.100]:10022)' can't be established.
ECDSA key fingerprint is SHA256:d3JNckcTVv1ASJlwv+IT/bJwlzMC4U1T/PmsKYIHMhQ.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.99.100]:10022' (ECDSA) to the list of known hosts.
[email protected]'s password:
[root@4966d35fe0a3 ~]# pwd
/root
w 如果宿主機是linux操作系統,則通過第4步查看到的ip地址連接
hr:centos7 hr$ ssh [email protected] -p 10022
vi /etc/ssh/sshd_config
RSAAuthentication yes #啓用 RSA 認證
PubkeyAuthentication yes #啓用公鑰私鑰配對認證方式
AuthorizedKeysFile .ssh/authorized_keys #公鑰文件路徑(和上面生成的文件同)
PermitRootLogin yes #root能使用ssh登錄123456
1、啓動一個docker容器
# docker run -t -i ubuntu/ruby:v1 /bin/bash
2、然後在容器裏,安裝openssh-server openssh-client
# apt-get install openssh-server openssh-client
3、完成之後,修改root密碼
# passwd
4、退出容器,並保存以上修改
# docker commit 3ea7a99a0025 ubuntu/ruby:v2
5、停止,並刪除剛纔的容器
# docker stop [container-id]
# docker rm [container-id]
查看容器id
# docker ps -a
查看鏡像列表
# docker p_w_picpaths
6、用剛保存的鏡像,後臺啓動一個新的容器
docker run --name [p_w_picpath-name] -i -t -p 50001:22 [p_w_picpath-id]
例: # docker run -d -p 50001:22 ubuntu/ruby:v2 /usr/sbin/sshd -D
7、ssh遠程登錄該容器
# ssh root@localhost -p 50001
如果想要通過ssh密鑰登錄,則在第2步,修改 /etc/ssh/sshd_config 文件,將RSAAuthentication 和 PubkeyAuthentication 後面的值都改成yes ,保存。
將本地.ssh下的id_rsa.pub上傳到容器中的 id_rsa.pub,
並
# mv /root/.ssh/id_rsa.pub/root/.ssh/authorized_keys,然後 # chmod 600 /root/.ssh/authorized_keys 即可.