远程登录 docker 容器(CMD、SecureCRT)

94 篇文章 9 订阅
31 篇文章 3 订阅


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 yesUsePAM 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值