首页 > 技术文章 > Docker 容器开启SSH

yako 2022-02-17 16:50 原文

Docker 容器开启SSH需要手动安装SSH相关软件,并启动,且做好容器端口和宿主机端口映射

 

1.安装软件

   yum install passwd openssl openssh-server -y

 

2.编辑 /etc/ssh/sshd_config

   RSAAuthentication yes #启用 RSA 认证

   PubkeyAuthentication yes #启用公钥私钥配对认证方式

    AuthorizedKeysFile .ssh/authorized_keys #公钥文件路径(和上面生成的文件同)

    PermitRootLogin yes #root能使用ssh登录

 

3.启动SSHD服务

   /usr/sbin/sshd -D&

   或者service sshd restart

   如果不能正常启动,报错 sshd: no hostkeys available -- exiting. 需要执行 ssh-keygen -A 再次启动SSHD服务即可

 

4.创建新镜像  

docker commit -m "rocky with ssh" -a "terry" [container]  [image]:[tag]

 

 5.创建并运行容器

docker run -p [hostport]:[containerport] -itd [image] /usr/sbin/sshd -D

  

推荐阅读