欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

docker centos7 安装ssh具体步骤

程序员文章站 2022-07-20 22:13:15
 docker centos7 安装ssh具体步骤,这里记录下,也行能帮助到正在读文章的朋友。 一. 从docker hub 下载centos 官方镜像...

 docker centos7 安装ssh具体步骤,这里记录下,也行能帮助到正在读文章的朋友。

一. 从docker hub 下载centos 官方镜像

hr:centos7 hr$ docker pull centos:7 

下载完后,查看本地资源库:
hr:centos7 hr$ docker images
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 root@192.168.99.100 -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.
  root@192.168.99.100's password: 
  [root@4966d35fe0a3 ~]# pwd
  /root


  w 如果宿主机是linux操作系统,则通过第4步查看到的ip地址连接
  hr:centos7 hr$ ssh root@172.17.0.2 -p 10022

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!