ubuntu 18.04 里面没有 rc.local ubuntu 18.04 容器中没有 systemctl,只有 service ubuntu 18.04 想要设置 sshd 开机启动,折腾了很多时间也没有成功 ubuntu 18.04 想要设置开机启动不是不能够,实在好困难!等之后熟练以后再来处理! 本次学习简化 ssh 链接,容许 root 登陆,不使用 PAM 忽略防火墙设置
docker run -itd -p 10023:22 --name ubuntu-ssh rastasheep/ubuntu-sshd /bin/bash 这个是 18.04,没有自动启动 sshd sudo docker run -d -p 10022:22 rastasheep/ubuntu-sshd:16.04 这个是 16.04 ,自动启动了 sshd
http://blog.51cto.com/12943999/2087906docker
docker pull ubuntu docker run -itd --name ubuntu ubuntu /bin/bash
docker exec -it ubuntu bash 确认 sshd 服务是否开启 ps -ef |grep ssh 没有 sshd 安装 openssh-server apt update apt install vim apt install openssh-server 确认安装包 dpkg -l |grep ssh ii openssh-client 1:7.6p1-4ubuntu0.1 amd64 secure shell (SSH) client, for secure access to remote machines ii openssh-server 1:7.6p1-4ubuntu0.1 amd64 secure shell (SSH) server, for secure access from remote machines ii openssh-sftp-server 1:7.6p1-4ubuntu0.1 amd64 secure shell (SSH) sftp server module, for SFTP access from remote machines ii ssh-import-id 5.7-0ubuntu1.1 all securely retrieve an SSH public key and install it locally 确认 sshd 位置,一下子执行 sshd 服务可能用到 which sshd /usr/sbin/sshd 修改 sshd 配置 vim /etc/ssh/sshd_config 增长一行 PermitRootLogin yes 修改一处 UsePAM no 开启 sshd 服务 systemctl restart sshd System has not been booted with systemd as init system (PID 1). Can't operate. ** 没有 systemctl 换一个方式启动服务 service ssh start ** 若是还不支持,直接进入 sshd 目录去执行 /usr/sbin/sshd 再次确认 sshd 服务是否开启 ps -ef |grep ssh root 4006 1 0 02:38 ? 00:00:00 /usr/sbin/sshd root 4199 10 0 02:56 pts/1 00:00:00 grep --color=auto ssh ** 如今能够从宿主机 ssh 到 Docker 容器了! ** exit 退出容器,或者再打开一个 Terminal ,ssh到宿主机 192.168.1.192
docker inspect ubuntu-ssh ... "Gateway": "172.17.0.1", "IPAddress": "172.17.0.3", ... ssh root@172.17.0.2 ** 若是报错 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! vim /home/dhbm/.ssh/known_hosts,删除以前的 ssh 记录以后从新 ssh 链接 仍是出现错误! The authenticity of host '172.17.0.2 (172.17.0.2)' can't be established. ECDSA key fingerprint is SHA256:DLhQdo4eKOPNmpKIyR5VUFhsNiTdXqqe6+Omiu7c+uY. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '172.17.0.2' (ECDSA) to the list of known hosts. root@172.17.0.2's password: Permission denied, please try again. root@172.17.0.2's password: Permission denied, please try again. root@172.17.0.2's password: ** 必须重设密码! ubuntu 18.04 没有设置 root 密码! 回到前一个已经进入 容器的 Terminalm,设置 root 密码 ** 若是刚才是 exit 退出了容器,再次 docker exec -it ubuntu bash 进入容器 passwd root
ok!可是,ip,ifconfig ,ping 都没有 安装这几个基本的工具 apt-get install net-tools apt-get install iputils-ping apt-get install iproute2
docker commit -a "wzh.2019" -m "message 20190107 ssh docker" ubuntu ubuntu-ssh:v1 sha256:5638786e4005b4a1c743e2b5edcbc651c6342efbd80fa07b46e6febfe0d9d81b
docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu-ssh v1 5638786e4005 About a minute ago 264MB ...
docker run -itd -p 10022:22 --name ubuntu-ssh2 ubuntu-ssh:v1 /bin/bash 06621aed44e0987fb7ccd69e1900630ebbe97d50de7dd22537ba359c76ded411
docker inspect ubuntu-ssh2 ... "Gateway": "172.17.0.1", "IPAddress": "172.17.0.3", ... 从宿主机测试 ssh,悲剧了!仍是不行! ssh root@172.17.0.3 ssh: connect to host 172.17.0.3 port 22: Connection refused 进入容器看看! docker exec -it ubuntu-ssh2 bash 检查 sshd 服务,仍是没有! 哪里不对呢 docker stop ubuntu-ssh2 和 docker stop ubuntu-ssh2 来回从新启动,也是枉然! ** 结论:ubuntu-ssh2 容器启动时,没有自动开启 sshd ** 简单纪录一下个人尝试 打开/etc/rc.local文件,在exit 0语句前加入: vim /etc/rc.local 加入 /etc/init.d/ssh start; 或者 /usr/sbin/sshd 结论:ubuntu 18.04 容器 死活就不自动启动 sshd ! 网上找了几个都是关于 Centos 7 容器的 在docker run 的时候运行/usr/sbin/init https://blog.csdn.net/u012814856/article/details/80493760 https://blog.csdn.net/cxin917/article/details/79410984 centos 容器的 init 在 /usr/sbin/init 个人 ubuntu 18.04 容器里面找找 init which init /sbin/init docker run -itd --name ubuntu-ssh --privileged ubuntu-ssh:v3 /sbin/init 第一次 run 以后,哈哈哈!立刻检查 sshd 服务,确实有了! 后来关机以后,从新 docker stop ubuntu-ssh3 ,又没有了!白高兴了! 结论: ubuntu 18.04 容器的init 只在第一次建立的时候会执行init 后续在 start 时,再也不执行 init (我的猜想,没有仔细去分析) 只有在 run 的时候,直接让他执行shell,启动 sshd 重复以上过程,到 commit 只一步
进入到以前的 ubuntu 基础容器 (前面已经安装过 sshd 的) docker exec -it ubuntu bash 创建一个 start.sh 文件,加入一条 shell 命令 vim start.sh #!/bin/bash # by wzh 20190108 /usr/sbin/sshd -D root@20fca9c46e82:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv start.sh sys tmp usr var
docker commit -a "wzh.2019" -m "message 20190108 ssh docker by start.sh " 20fca9c46e82 ubuntu1804-ssh:v1 新建一个容器,名字 ubuntu1804-ssh,端口 10024 docker run -itd -p 10024:22 --name ubuntu1804-ssh ubuntu1804-ssh:v1 /run.sh 检查确认 docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 87d09b047e4c ubuntu1804-ssh:v1 "/start.sh" 29 hours ago Exited (137) 22 hours ago ubuntu1804-ssh ```
docker inspect ubuntu-ssh2 ... "Gateway": "172.17.0.1", "IPAddress": "172.17.0.4", ... ssh root@172.17.0.4 ok !
从个人工做电脑仔打开一个 Terminal ssh -p 10024 root@192.168.1.192 ok!