guide.md 10.9 KB
Newer Older
A
Anmol Sethi 已提交
1 2 3 4 5 6
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
# Setup Guide

- [1. Acquire a remote machine](#1-acquire-a-remote-machine)
  - [Requirements](#requirements)
7
  - [Google Cloud](#google-cloud)
A
Anmol Sethi 已提交
8 9 10 11
- [2. Install code-server](#2-install-code-server)
- [3. Expose code-server](#3-expose-code-server)
  - [SSH forwarding](#ssh-forwarding)
  - [Let's Encrypt](#lets-encrypt)
A
Anmol Sethi 已提交
12
    - [NGINX](#nginx)
A
Anmol Sethi 已提交
13 14 15 16 17 18
  - [Self Signed Certificate](#self-signed-certificate)
  - [Change the password?](#change-the-password)
  - [How do I securely access development web services?](#how-do-i-securely-access-development-web-services)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

A
Anmol Sethi 已提交
19 20
This guide demonstrates how to setup and use `code-server`.
To reiterate, `code-server` lets you run VS Code on a remote server and then access it via a browser.
A
Anmol Sethi 已提交
21

A
Anmol Sethi 已提交
22 23
Further docs are at:

A
Anmol Sethi 已提交
24 25 26 27
- [README](../README.md) for a general overview
- [INSTALL](../doc/install.md) for installation
- [FAQ](./FAQ.md) for common questions.
- [CONTRIBUTING](../doc/CONTRIBUTING.md) for development docs
A
Anmol Sethi 已提交
28

A
Anmol Sethi 已提交
29
We highly recommend reading the [FAQ](./FAQ.md) on the [Differences compared to VS Code](./FAQ.md#differences-compared-to-vs-code) before beginning.
A
Anmol Sethi 已提交
30

A
Anmol Sethi 已提交
31
We'll walk you through acquiring a remote machine to run `code-server` on
A
Anmol Sethi 已提交
32
and then exposing `code-server` so you can securely access it.
A
Anmol Sethi 已提交
33 34 35

## 1. Acquire a remote machine

A
Anmol Sethi 已提交
36
First, you need a machine to run `code-server` on. You can use a physical
A
Anmol Sethi 已提交
37 38 39 40 41 42 43 44 45
machine you have lying around or use a VM on GCP/AWS.

### Requirements

For a good experience, we recommend at least:

- 1 GB of RAM
- 2 cores

46
You can use whatever linux distribution floats your boat but in this guide we assume Debian on Google Cloud.
A
Anmol Sethi 已提交
47

48
### Google Cloud
A
Anmol Sethi 已提交
49 50 51 52 53 54 55 56 57

For demonstration purposes, this guide assumes you're using a VM on GCP but you should be
able to easily use any machine or VM provider.

You can sign up at https://console.cloud.google.com/getting-started. You'll get a 12 month \$300
free trial.

Once you've signed up and created a GCP project, create a new Compute Engine VM Instance.

58 59 60 61 62 63 64 65
1. Navigate to `Compute Engine -> VM Instances` on the sidebar.
2. Now click `Create Instance` to create a new instance.
3. Name it whatever you want.
4. Choose the region closest to you based on [gcping.com](http://www.gcping.com).
5. Any zone is fine.
6. We'd recommend a `E2` series instance from the General-purpose family.
   - Change the type to custom and set at least 2 cores and 2 GB of ram.
   - Add more vCPUs and memory as you prefer, you can edit after creating the instance as well.
A
Anmol Sethi 已提交
66
   - https://cloud.google.com/compute/docs/machine-types#general_purpose
A
Anmol Sethi 已提交
67
7. We highly recommend switching the persistent disk to an SSD of at least 32 GB.
68 69 70
   - Click `Change` under `Boot Disk` and change the type to `SSD Persistent Disk` and the size
     to `32`.
   - You can always grow your disk later.
A
Anmol Sethi 已提交
71
8. Navigate to `Networking -> Network interfaces` and edit the existing interface
72 73
   to use a static external IP.
   - Click done to save network interface changes.
A
Anmol Sethi 已提交
74
9. If you do not have a [project wide SSH key](https://cloud.google.com/compute/docs/instances/adding-removing-ssh-keys#project-wide), navigate to `Security -> SSH Keys` and add your public key there.
A
Anmol Sethi 已提交
75 76 77
10. Click create!

Remember, you can shutdown your server when not in use to lower costs.
A
Anmol Sethi 已提交
78

A
Anmol Sethi 已提交
79 80 81 82 83
We highly recommend learning to use the [`gcloud`](https://cloud.google.com/sdk/gcloud) cli
to avoid the slow dashboard.

## 2. Install code-server

84
We have a [script](../install.sh) to install `code-server` for Linux, macOS and FreeBSD.
A
Anmol Sethi 已提交
85

A
Anmol Sethi 已提交
86
It tries to use the system package manager if possible.
A
Anmol Sethi 已提交
87

A
Anmol Sethi 已提交
88
First run to print out the install process:
A
Anmol Sethi 已提交
89 90

```bash
91
curl -fsSL https://code-server.dev/install.sh | sh -s -- --dry-run
A
Anmol Sethi 已提交
92 93
```

A
Anmol Sethi 已提交
94 95 96 97 98 99
Now to actually install:

```bash
curl -fsSL https://code-server.dev/install.sh | sh
```

A
Anmol Sethi 已提交
100
The install script will print out how to run and start using `code-server`.
A
Anmol Sethi 已提交
101

102
Docs on the install script, manual installation and docker image are at [./install.md](./install.md).
A
Anmol Sethi 已提交
103

A
Anmol Sethi 已提交
104 105
## 3. Expose code-server

106 107
**Never**, **ever** expose `code-server` directly to the internet without some form of authentication
and encryption as someone can completely takeover your machine with the terminal.
A
Anmol Sethi 已提交
108

A
Anmol Sethi 已提交
109 110 111
By default, `code-server` will enable password authentication which will require you to copy the
password from the`code-server`config file to login. It will listen on`localhost` to avoid exposing
itself to the world. This is fine for testing but will not work if you want to access `code-server`
112 113
from a different machine.

A
Anmol Sethi 已提交
114
There are several approaches to securely operating and exposing `code-server`.
A
Anmol Sethi 已提交
115

A
Anmol Sethi 已提交
116
**tip**: You can list the full set of `code-server` options with `code-server --help`
A
Anmol Sethi 已提交
117 118 119 120 121

### SSH forwarding

We highly recommend this approach for not requiring any additional setup, you just need an
SSH server on your remote machine. The downside is you won't be able to access `code-server`
A
Anmol Sethi 已提交
122
on any machine without an SSH client like on iPad. If that's important to you, skip to [Let's Encrypt](#lets-encrypt).
A
Anmol Sethi 已提交
123

A
Anmol Sethi 已提交
124
First, ssh into your instance and edit your `code-server` config file to disable password authentication.
A
Anmol Sethi 已提交
125 126 127 128 129 130

```bash
# Replaces "auth: password" with "auth: none" in the code-server config.
sed -i.bak 's/auth: password/auth: none/' ~/.config/code-server/config.yaml
```

A
Anmol Sethi 已提交
131
Restart `code-server` with (assuming you followed the guide):
A
Anmol Sethi 已提交
132 133

```bash
A
Anmol Sethi 已提交
134
sudo systemctl restart code-server@$USER
A
Anmol Sethi 已提交
135 136
```

137
Now forward local port 8080 to `127.0.0.1:8080` on the remote instance by running the following command on your local machine.
138

A
Anmol Sethi 已提交
139 140
Recommended reading: https://help.ubuntu.com/community/SSH/OpenSSH/PortForwarding.

141 142
```bash
# -N disables executing a remote shell
143
ssh -N -L 8080:127.0.0.1:8080 [user]@<instance-ip>
144 145
```

A
Anmol Sethi 已提交
146
Now if you access http://127.0.0.1:8080 locally, you should see `code-server`!
A
Anmol Sethi 已提交
147 148 149 150 151

If you want to make the SSH port forwarding persistent we recommend using
[mutagen](https://mutagen.io/documentation/introduction/installation).

```
S
Sandro Jäckel 已提交
152
# Same as the above SSH command but runs in the background continuously.
A
Anmol Sethi 已提交
153
# Add `mutagen daemon start` to your ~/.bashrc to start the mutagen daemon when you open a shell.
154
mutagen forward create --name=code-server tcp:127.0.0.1:8080 <instance-ip>:tcp:127.0.0.1:8080
A
Anmol Sethi 已提交
155 156 157 158 159 160 161 162 163 164
```

We also recommend adding the following lines to your `~/.ssh/config` to quickly detect bricked SSH connections:

```bash
Host *
ServerAliveInterval 5
ExitOnForwardFailure yes
```

A
Anmol Sethi 已提交
165 166
You can also forward your SSH and GPG agent to the instance to securely access GitHub
and sign commits without copying your keys.
A
Anmol Sethi 已提交
167 168 169 170 171 172

1. https://developer.github.com/v3/guides/using-ssh-agent-forwarding/
2. https://wiki.gnupg.org/AgentForwarding

### Let's Encrypt

A
Anmol Sethi 已提交
173 174
[Let's Encrypt](https://letsencrypt.org) is a great option if you want to access `code-server` on an iPad
or do not want to use SSH forwarding. This does require that the remote machine be exposed to the internet.
A
Anmol Sethi 已提交
175 176 177

Assuming you have been following the guide, edit your instance and checkmark the allow HTTP/HTTPS traffic options.

178 179 180
1. You'll need to buy a domain name. We recommend [Google Domains](https://domains.google.com).
2. Add an A record to your domain with your instance's IP.
3. Install caddy https://caddyserver.com/docs/download#debian-ubuntu-raspbian.
A
Anmol Sethi 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

```bash
echo "deb [trusted=yes] https://apt.fury.io/caddy/ /" \
    | sudo tee -a /etc/apt/sources.list.d/caddy-fury.list
sudo apt update
sudo apt install caddy
```

4. Replace `/etc/caddy/Caddyfile` with sudo to look like this:

```
mydomain.com

reverse_proxy 127.0.0.1:8080
```

197 198
Remember to replace `mydomain.com` with your domain name!

A
Anmol Sethi 已提交
199 200 201 202 203 204
5. Reload caddy with:

```bash
sudo systemctl reload caddy
```

A
Anmol Sethi 已提交
205
Visit `https://<your-domain-name>` to access `code-server`. Congratulations!
A
Anmol Sethi 已提交
206

A
Anmol Sethi 已提交
207
In a future release we plan to integrate Let's Encrypt directly with `code-server` to avoid
A
Anmol Sethi 已提交
208 209
the dependency on caddy.

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
#### NGINX

If you prefer to use NGINX instead of Caddy then please follow steps 1-2 above and then:

3. Install `nginx`:

```bash
sudo apt update
sudo apt install -y nginx certbot python-certbot-nginx
```

4. Put the following config into `/etc/nginx/sites-available/code-server` with sudo:

```nginx
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;
    }
}
```

Remember to replace `mydomain.com` with your domain name!

5. Enable the config:

```bash
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
```

Make sure to substitute `me@example.com` with your actual email.

Visit `https://<your-domain-name>` to access `code-server`. Congratulations!

A
Anmol Sethi 已提交
252 253 254
### Self Signed Certificate

**note:** Self signed certificates do not work with iPad and will cause a blank page. You'll
255
have to use [Let's Encrypt](#lets-encrypt) instead. See the [FAQ](./FAQ.md#blank-screen-on-ipad).
A
Anmol Sethi 已提交
256

257
Recommended reading: https://security.stackexchange.com/a/8112.
A
Anmol Sethi 已提交
258

A
Anmol Sethi 已提交
259 260
We recommend this as a last resort because self signed certificates do not work with iPads and can
cause other bizarre issues. Not to mention all the warnings when you access `code-server`.
261 262
Only use this if:

A
Anmol Sethi 已提交
263 264
1. You do not want to buy a domain or you cannot expose the remote machine to the internet.
2. You do not want to use SSH forwarding.
A
Anmol Sethi 已提交
265 266 267 268 269 270 271 272 273 274 275 276

ssh into your instance and edit your code-server config file to use a randomly generated self signed certificate:

```bash
# 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
```

A
Anmol Sethi 已提交
277
Assuming you have been following the guide, restart `code-server` with:
A
Anmol Sethi 已提交
278 279

```bash
A
Anmol Sethi 已提交
280
sudo systemctl restart code-server@$USER
A
Anmol Sethi 已提交
281 282 283 284
```

Edit your instance and checkmark the allow HTTPS traffic option.

A
Anmol Sethi 已提交
285
Visit `https://<your-instance-ip>` to access `code-server`.
A
Anmol Sethi 已提交
286 287
You'll get a warning when accessing but if you click through you should be good.

288
To avoid the warnings, you can use [mkcert](https://mkcert.dev) to create a self signed certificate
A
Anmol Sethi 已提交
289
trusted by your OS and then pass it into `code-server` via the `cert` and `cert-key` config
A
Anmol Sethi 已提交
290
fields.
A
Anmol Sethi 已提交
291 292 293

### Change the password?

A
Anmol Sethi 已提交
294 295
Edit the `password` field in the `code-server` config file at `~/.config/code-server/config.yaml`
and then restart `code-server` with:
A
Anmol Sethi 已提交
296 297

```bash
A
Anmol Sethi 已提交
298
sudo systemctl restart code-server@$USER
A
Anmol Sethi 已提交
299 300 301 302
```

### How do I securely access development web services?

A
Anmol Sethi 已提交
303
If you're working on a web service and want to access it locally, `code-server` can proxy it for you.
A
Anmol Sethi 已提交
304

305
See the [FAQ](./FAQ.md#how-do-i-securely-access-web-services).