1. 运行容器
docker run -itd --name openresty-test --privileged=true -p 2222:80 openresty:1.21.4.1
2. 容器内安装 ps
docker exec -it `docker ps -a | grep openresty | awk '{print $1}'` bash
yum -y install procps
3. 更新容器密码
passwd
新密码
4. 容器内安装 sshd
yum -y install openssh-server # 宿主机
ll /usr/sbin/sshd
yum -y install openssh-server openssh-clients # 容器内
ps -ef | grep sshd
ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ''
ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key -N ''
启动 sshd
/usr/sbin/sshd
ps -ef | grep sshd
修改
/etc/ssh/sshd_config
配置并保存:PermitRootLogin yes
、UsePAM no
vi /etc/ssh/sshd_config
......
PermitRootLogin yes #root能使用ssh登录
PubkeyAuthentication yes #启用公钥私钥配对认证方式
AuthorizedKeysFile .ssh/authorized_keys #公钥文件路径(和上面生成的文件同)
......
重启 sshd
pkill sshd
/usr/sbin/sshd
5. 将容器提交为新的镜像
docker commit -m 'install openssh' -a 'Docker Newbee' 20339d864883 openresty-sshd:1.21.4.1
6. 运行新的镜像
docker run -itd --name openresty-sshd -p 3333:80 openresty-sshd:1.21.4.1 /usr/sbin/sshd -D
7. 连接
C:\Users\15205>ssh -p 3333 root@192.168.10.66
The authenticity of host '[192.168.10.66]:3333 ([192.168.10.66]:3333)' can't be established.
ECDSA key fingerprint is SHA256:DjVaDeDlU+wjVDolVEbSFoWWIDMkUN97XPpFjQv2zno.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[192.168.10.66]:3333' (ECDSA) to the list of known hosts.
root@192.168.10.66's password:
Last login: Sun Aug 21 02:36:43 2022 from 192.168.10.1
[root@543cf2c4686e ~]# ls
anaconda-ks.cfg
[root@543cf2c4686e ~]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 112936 4316 ? Ss 02:35 0:00 /usr/sbin/sshd -D
root 7 0.0 0.1 153408 5928 ? Ss 02:36 0:00 sshd: root@pts/0
root 9 0.0 0.0 11824 1860 pts/0 Ss+ 02:36 0:00 -bash
root 24 0.0 0.1 153408 6032 ? Ss 02:43 0:00 sshd: root@pts/1
root 26 0.0 0.0 11828 1892 pts/1 Ss 02:43 0:00 -bash
root 40 0.0 0.0 51732 1716 pts/1 R+ 02:43 0:00 ps aux