以下以amazon 的镜像作为例子(基于yum,对于debian系列也会给出建议)
一、在服务器容器中配置ssh
1. 通过一个其他的端口进入容器,这里使用5678端口
docker run -it -d -p 5678:22 amazon
2. 下载openssh
centos/fedora
yum install openssh-server -y
debian/ubuntu
apt-get install openssh-server
3.设置root密码
passwd
如果容器内没有这个模块,可以安装之后再尝试
4. 修改配置文件
vi /etc/ssh/sshd_config
把这三行的注释去掉:
Port 22
PermitRootLogin yes
PubkeyAuthentication yes
5. 启动ssh服务
这里可能会报错,提示没有 /etc/ssh/ssh_host_rsa_key
、/etc/ssh/ssh_host_ecdsa_key
、/etc/ssh/ssh_host_ed25519_key
这些文件
解决方案:
ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key
ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key
启动ssh服务
/usr/sbin/sshd -D &
6. 验证效果
在容器所在的主机上连接:
ssh -p 5678 [email protected]
远程机器连接:
ssh -p 5678 root@remote_host
7. 免密钥登陆
这里和正常ssh免密钥登陆流程一样,这里仅仅提一下,详细可以bing下
ssh-copy-id -p 5678 root@remote_host
二、使用vscode连接
首先需要确保容器内包含 tar
这个工具,否则会报Fail to install VS-Code Server
这样的错误
我是在windows电脑上安装的vscode,只需要在vscode中安装remote-ssh
这个插件,然后就像连接普通远程服务器那样操作就可以了。
参考:
- https://zhuanlan.zhihu.com/p/361934730
- https://www.cnblogs.com/netonline/p/7410586.html
- https://blog.csdn.net/wang704987562/article/details/72722263
- https://www.cnblogs.com/sunshine-2015/p/6384471.html