使用vscode连接toolbox容器比较困难,使用ssh是其中一条比较简单的途径。
本文是Integrating Fedora Toolbox into VS Code (with the help of SSH)的译介,原作者Juraj Fiala。
创建一个toolbox容器
toolbox create test
toolbox enter test在容器中使用运行sshd
在toolbox几乎不能在root环境下使用systemd,因此无法通过常规的sshd service的方式使用它。因此需要手动执行sshd运行openssh守护程序。
安装openssh-server
dnf install openssh-server手动运行openssh守护程序
由于不能通过systemd运行sshd,因此不能触发sshd-keygen.target来生成密钥,需要手动进行这一步骤。运行下面其中一条指令即可。
/usr/libexec/openssh/sshd-keygen rsa
/usr/libexec/openssh/sshd-keygen ecdsa
/usr/libexec/openssh/sshd-keygen ed25519修改容器中的/etc/ssh/sshd_config,添加下面的设置
# For VS Code
Port 2234 # Prevent conflicts with other SSH servers
ListenAddress localhost # Don’t allow remote connections
PermitEmptyPasswords yes # Containers lack passwords by default
PermitUserEnvironment yes # Allow setting DISPLAY for remote connections之后运行指令启动sshd
/usr/sbin/sshd连接
在宿主机的配置文件~/.ssh/config中添加如下行:
Host toolbox-test
HostName localhost
Port 2234之后就可以使用ssh toolbox-test连接toolbox开发容器了。
注意
原作者提示:
Bonus tip: Open your projects via /var/home/… instead of /home/… to avoid symlink bugs.
我在使用中没有遇到这类bug。