code-server 是一个基于 VS Code 的在线编辑器,在任何地方的任何机器上运行 VS Code 并在浏览器中访问它,实现任何设备通过浏览器即可访问 VS Code,进而实现远程在线开发。
它的亮点是:
官方建议服务器至少要有 1 GB 内存和 2 核。
项目地址是:
https://github.com/cdr/code-server
脚本安装
官方提供了一个脚本来为 Linux、macOS 和 FreeBSD 安装 code-server。
首先尝试打印安装过程中的详细内容,这个过程不会进行安装:
curl?-fsSL?https://code-server.dev/install.sh?|?sh?-s?--?--dry-run?
真正安装:
curl?-fsSL?https://code-server.dev/install.sh?|?sh?
独立安装
官方提供了独立的压缩安装文件,下载对应服务器的版本,解压缩即可使用。
//?完成后访问?http://127.0.0.1:8080,密码保存在?~/.config/code-server/config.yaml?配置文件中??mkdir?-p?~/.local/lib?~/.local/bin??curl?-fL?https://github.com/cdr/code-server/releases/download/v$VERSION/code-server-$VERSION-linux-amd64.tar.gz?\??|?tar?-C?~/.local/lib?-xz??mv?~/.local/lib/code-server-$VERSION-linux-amd64?~/.local/lib/code-server-$VERSION??ln?-s?~/.local/lib/code-server-$VERSION/bin/code-server?~/.local/bin/code-server??PATH="~/.local/bin:$PATH"??code-server??#?Your?password?is?in?
Docker 安装
#?这将启动一个?code-server?容器并通过?http://127.0.0.1:8080?访问??mkdir?-p?~/.config??docker?run?-it?--name?code-server?-p?127.0.0.1:8080:8080?\??-v?"$HOME/.config:/home/coder/.config"?\??-v?"$PWD:/home/coder/project"?\??-u?"$(id?-u):$(id?-g)"?\??-e?"DOCKER_USER=$USER"?\??codercom/code-server:latest?
官方强烈建议不要在公网暴露不需要认证或加密的 code-server,在默认情况下,code-server 会使用密码验证,密码从配置文件(~/.config/code-server/config.yaml)中获取。它只监听 localhost 的请求以避免将自己暴露出去。如果想从不同的地方安全地使用 code-server,则可以采用以下方法。
SSH 转发
官方推荐这种方法,因为不需要任何额外的设置,只需要在远程机器上有一个 SSH 服务。缺点是无法在没有 SSH 客户端的设备上访问,如 iPad。
首先,通过 ssh 进入实例并编辑 code-server 配置文件以禁用密码身份验证:
#?将?“auth:?password”?替换为?“auth:?none”??sed?-i.bak?'s/auth:?password/auth:?none/'?~/.config/code-server/config.yaml?
重启:
sudo?systemctl?restart?code-server@$USER?
本地计算机运行以下命令,将本地端口 8080 转发到远程实例:
#?-N?disables?executing?a?remote?shell??ssh?-N?-L?8080:127.0.0.1:8080?[user]@<instance-ip>?
现在就可以在本地计算机通过 http://127.0.0.1:8080 访问 code-server
使用 Let's Encrypt
sudo?apt?update??sudo?apt?install?-y?nginx?certbot?python3-certbot-nginx?
? ? ?4. 将以下内容放到 /etc/nginx/sites-available/code-server 文件中:
server?{??listen?80;??listen?[::]:80;??server_name?mydomain.com;??location?/?{??proxy_pass?http://localhost:8080/;??proxy_set_header?Host?$host;??proxy_set_header?Upgrade?$http_upgrade;??proxy_set_header?Connection?upgrade;??proxy_set_header?Accept-Encoding?gzip;??}??}?
mydomain.com 是你使用的域名。
5. 启用配置
sudo?ln?-s?../sites-available/code-server?/etc/nginx/sites-enabled/code-server??sudo?certbot?--non-interactive?--redirect?--agree-tos?--nginx?-d?mydomain.com?-m?me@example.com?
其中 me@example.com 使用你自己实际的电子邮件。访问 https://your-domain-name 即可访问 code-server。
6. 以上 3 到 5 可使用 Caddy 替代,详细说明参考官网。
使用自签名证书
建议将次作为最后的手段,因为自签名证书不适用与 iPad,并可能导致其他奇怪的问题。
首先 ssh 进入 code-server 服务器并编辑配置文件以使用随机生成的自签名证书:
#?Replaces?"cert:?false"?with?"cert:?true"?in?the?code-server?config.??sed?-i.bak?'s/cert:?false/cert:?true/'?~/.config/code-server/config.yaml??#?Replaces?"bind-addr:?127.0.0.1:8080"?with?"bind-addr:?0.0.0.0:443"?in?the?code-server?config.??sed?-i.bak?'s/bind-addr:?127.0.0.1:8080/bind-addr:?0.0.0.0:443/'?~/.config/code-server/config.yaml??#?Allows?code-server?to?listen?on?port?443.??sudo?setcap?cap_net_bind_service=+ep?/usr/lib/code-server/lib/node?
重启:
sudo?systemctl?restart?code-server@$USER?
FAQ
使用 code-server 时会遇到一些问题。比如与 VS Code 的差异?如何禁用遥测?如何调试代码服务器?这些问题都可以在官方 FAQ 文档中找到:
https://github.com/cdr/code-server/blob/main/docs/FAQ.md#how-should-i-expose-code-server-to-the-internet?
领取专属 10元无门槛券
私享最新 技术干货