SSH 为建立在应用层和传输层基础上的安全协议。SSH 是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议。利用SSH 协议可以有效防止远程管理过程中的信息泄露问题。
从客户端来看,SSH提供两种级别的安全验证:
1、基于口令的验证
只要知道帐号和口令,就可以登录到远程主机。所有传输的数据都会被加密,但缺点是:不能保证你正在连接的服务器就是你想连接的服务器。以下是我画了的登录验证流程:
当第一次链接远程主机时,会提示您当前主机的”公钥指纹”,询问您是否继续,如果选择继续后就可以输入密码进行登录了,当远程的主机接受以后,该台服务器的公钥就会保存到~/.ssh/known_hosts文件中。
2、基于密钥的验证
这种验证的前提是客户端需要生成一对密钥,将公钥放到需访问的远程服务器。这种验证比上一种的好处是,不能仿冒真正的服务器,因为要仿冒必须拿到客户端生成的公钥。缺点就是验证等待过程稍长些。
如何生成密钥:
1、在客户端打开终端,执行ssh-keygen,该命令会默认在~/.ssh/目录下创建id_rsa、id_rsa.pub两个文件,分别为您的公钥和私钥。
2、将公钥id_rsa.pub文件拷贝到服务器端的~/.ssh/authorized_keys文件中,有三种方法:
· 通过scp拷贝:
· 例:scp -P 22 ~/.ssh/id_rsa.pub user@host:~/authorized_keys #可选参数-P代表指定用端口号22
· 通过ssh-copyid程序:
· 例:ssh-copy-id user@host #此种方式简单,不需追加改文件名,但不能指定端口号,默认以22端口
· 通过cat方法:
· 例:cat ~/.ssh/id_rsa.pub | ssh -p 22 user@host ‘cat>> ~/.ssh/authorized_keys’
在宿主主机上生成ssh密钥对,并创建authorized_keys 文件:
root@ubuntu-daisy:~/daisy_dockerbuild/sshd_ubuntu# ssh-keygen -t rsa
root@ubuntu-daisy:~/daisy_dockerbuild/sshd_ubuntu#cat ~/.ssh/id_rsa.pub>authorized_keys
root@ubuntu-daisy:~/daisy_dockerbuild/sshd_ubuntu# ls
authorized_keys Dockerfile
首先准备好基础镜像ubuntu,然后编写Dockerfile构建自定义镜像,达到能自定义登录功能
FROM ubuntu
MAINTAINER daisy_hu"diana_hu@foxmail.com"
# 更新源,安装sshserver
RUN exporthttp_proxy=http://proxy-prc.intel.com:911 && exporthttps_proxy=https://proxy-prc.intel.com:911 && apt-get update && apt-get install -y openssh-server
RUN mkdir/var/run/sshd
ADD authorized_keys /root/.ssh/authorized_keys
# 设置root ssh远程登录密码为123456
RUN echo "root:123456" |chpasswd
# 容器需要开放SSH22端口
EXPOSE 22
下一步运行命令构建支持ssh登录的ubuntu镜像
root@ubuntu-daisy:~/daisy_dockerbuild/sshd# docker build -tdaisy/login .
构建成功后查看新增image
root@ubuntu-daisy:~/daisy_dockerbuild/sshd# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
daisy/login latest 9645945b6860 13 minutes ago 220.7 MB
然后创建container,查看映射端口
root@ubuntu-daisy:~/daisy_dockerbuild/sshd# docker run--name=sshtest -d -P daisy/login /usr/sbin/sshd -D
8db23fd126156b1e94ba9b0114da0f076098ab7022e77a79d9b19d14bd94e970
root@ubuntu-daisy:~/daisy_dockerbuild/sshd# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8db23fd12615 daisy/login "/usr/sbin/sshd -D" 6seconds ago Up 5 seconds 0.0.0.0:32774->22/tcp sshtest
发现container被随机分配了一个端口32774,然后使用ssh登录
root@ubuntu-daisy:~/daisy_dockerbuild/sshd# ssh root@10.239.129.89 -p 32774
The authenticity of host '[10.239.129.89]:32774([10.239.129.89]:32774)' can't be established.
ECDSA key fingerprint isSHA256:5HYU7cJXjiJ3zVWbTr/CqkVOS7DcsWurq9Xp4m7l12A.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[10.239.129.89]:32774' (ECDSA)to the list of known hosts.
root@10.239.129.89's password:
输入密码成功ssh成功进入container
Create ssh container :
方法一:使用dockerfile ,映射端口,再远程登录到指定端口通信
方法二:使用pipework 工具,指定IP ,直接sship
方法一:使用dockerfile编写
首先创建sshd_ubuntu工作目录,在里面创建Dockerfile和run.sh文件
root@ubuntu-daisy:~/daisy_dockerbuild/sshd_ubuntu#cat run.sh
#!/bin/bash
/usr/sbin/sshd –D
在宿主主机上生成ssh密钥对,并创建authorized_keys 文件:
root@ubuntu-daisy:~/daisy_dockerbuild/sshd_ubuntu# ssh-keygen -t rsa
root@ubuntu-daisy:~/daisy_dockerbuild/sshd_ubuntu#cat ~/.ssh/id_rsa.pub>authorized_keys
root@ubuntu-daisy:~/daisy_dockerbuild/sshd_ubuntu# ls
authorized_keys Dockerfile run.sh
root@ubuntu-daisy:~/daisy_dockerbuild/sshd_ubuntu# cat Dockerfile
#设置继承镜像
FROM Ubuntu
#提供维护者信息
MAINTAINER daisy_hu diana_hu@foxmail.com
#安装ssh服务
RUN export http_proxy=http://proxy-prc.intel.com:911 && export https_proxy=https://proxy-prc.intel.com:911 && apt-get update && apt-get install –y openssh-server
RUN mkdir /var/run/sshd
RUN mkdir /root/.ssh
#取消pam限制
RUN sed -ri 's/session required pam_loginuid.so /#session required pam_loginuid.so/g' /etc/pam.d/sshd
#复制配置文件到相应位置,并赋予脚本可执行权限
ADD authorized_keys /root/.ssh/authorized_keys
ADD run.sh /run.sh
RUN chmod 755 /run.sh
#开放端口22
EXPOSE 22
#设置自启动命令
CMD ["/run.sh"]
创建镜像:
root@ubuntu-daisy:~/daisy_dockerbuild/sshd_ubuntu# docker build -t daisy-sshd .
Sending build context to Docker daemon 4.096 kB
Step 1 : FROM ubuntu
---> bd3d4369aebc
Step 2 : MAINTAINER daisy_hu"diana_hu@foxmail.com"
---> Using cache
---> 647abe287559
Step 3 : RUN export http_proxy=http://proxy-prc.intel.com:911&& export https_proxy=https://proxy-prc.intel.com:911 &&apt-get update && apt-get install -y openssh-server
---> Using cache
---> 468aa06fce60
Step 4 : RUN mkdir /var/run/sshd
---> Using cache
---> 7990cd44cf4e
Step 5 : RUN mkdir /root/.ssh
---> Using cache
---> fcd1349e2a91
Step 6 : RUN sed -ri 's/session required pam_loginuid.so /#session required pam_loginuid.so/g' /etc/pam.d/sshd
---> Using cache
---> 74aeae733485
Step 7 : ADD authorized_keys /root/.ssh/authorized_keys
---> Using cache
---> 87276a38d312
Step 8 : ADD run.sh /run.sh
---> Using cache
---> 6b4f1c96eb72
Step 9 : RUN chmod 755 /run.sh
---> Using cache
---> 41737fde2a01
Step 10 : EXPOSE 22
---> Using cache
---> d8f4cb47eec4
Step 11 : CMD /run.sh
---> Using cache
---> f8e8d7bb83d4
Successfully built f8e8d7bb83d4
root@ubuntu-daisy:~/daisy_dockerbuild/sshd_ubuntu# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
daisy-sshd latest f8e8d7bb83d4 17 hours ago 220.8 MB
测试镜像运行容器,查看端口映射:
root@ubuntu-daisy:~/daisy_dockerbuild/sshd_ubuntu# docker run --name=sshtest -d -P daisy-sshd /usr/sbin/sshd -D
6c7602803674f031c8fea30efde5d826a30d3ee8a2360c425ce77082cea79429
root@ubuntu-daisy:~/daisy_dockerbuild/sshd_ubuntu# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c7602803674 daisy-sshd "/usr/sbin/sshd -D" 7seconds ago Up 5 seconds 0.0.0.0:32769->22/tcp sshtest
对应端口是32769,开始ssh登录
root@ubuntu-daisy:~/daisy_dockerbuild/sshd_ubuntu# ssh 10.239.129.89 -p 32769
The authenticity of host '[10.239.129.89]:32769([10.239.129.89]:32769)' can't be established.
ECDSA key fingerprint isSHA256:9XXY7JjjCZhvqhV+DeTwxl4+2e7+8EwzflwHacWmBMM.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[10.239.129.89]:32769' (ECDSA)to the list of known hosts.
Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-36-genericx86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
The programs included with the Ubuntu system are freesoftware;
the exact distribution terms for each program are describedin the
individual files in /usr/share/doc/*/copyright.
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extentpermitted by
applicable law.
root@6c7602803674:~#
root@6c7602803674:~# exit
logout
Connection to 10.239.129.89 closed.
成功进入container。