docker安装ssh
时间: 2023-09-06 20:14:08 浏览: 93
docker安装
要在Docker容器中安装SSH,可以按照以下步骤进行操作:
1. 创建一个Docker容器,并进入容器内部:
```
docker run -t -i ubuntu/ruby:v1 /bin/bash
```
2. 在容器内部安装openssh-server和openssh-client:
```
apt-get install openssh-server openssh-client
```
3. 完成安装后,修改root密码:
```
passwd
```
4. 退出容器,并保存对容器的修改:
```
exit
docker commit [container-id ubuntu/ruby:v2
```
5. 停止并删除原来的容器:
```
docker stop [container-id]
docker rm [container-id]
```
6. 启动一个新的容器,并后台运行SSH服务:
```
docker run -d -p 50001:22 ubuntu/ruby:v2 /usr/sbin/sshd -D
```
7. 使用SSH远程登录该容器:
```
ssh root@localhost -p 50001
```
如果想使用SSH密钥登录容器,可以在第2步中修改`/etc/ssh/sshd_config`文件,将`RSAAuthentication`和`PubkeyAuthentication`后面的值都改为`yes`,并将本地的`.ssh/id_rsa.pub`上传到容器中的`.ssh/authorized_keys`文件。然后运行以下命令:
```
mv /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
```
这样就可以使用SSH密钥登录容器了。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [docker容器里安装ssh](https://blog.csdn.net/chengxuyuanyonghu/article/details/76619097)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [docker中安装配置ssh服务](https://blog.csdn.net/YBK233/article/details/83094935)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [Docker容器安装ssh](https://blog.csdn.net/weixin_38620434/article/details/127336077)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文