# Remote Access — Quick Setup Guide

This guide walks you through enabling remote access to `CanKingService` and connecting to it from another machine. For full configuration options see [Remote Access for CanKingService](./REMOTE_ACCESS.md).

---

## On the server (the machine running CanKingService)

### Step 1 — Generate a TLS certificate

Run this command, replacing `myserver.example.com` with your machine's hostname or IP address:

```bash
ck remote generate-cert myserver.example.com
```

On success, two files are created:

- `myserver.example.com.pfx` — the server certificate (private, stays on the server). It is saved in the default certificate directory: `/usr/share/Kvaser/CanKing/ca-certificates` on Linux and `C:\ProgramData\Kvaser\CanKing\ca-certificates` on Windows.
- `myserver.example.com.pem` — the public certificate (needs to be copied to each client). It is saved in the directory where you ran the command.

The command prints the full path of both files.

> **Tip:** Add `--password MySecret` to protect the PFX with a password, and `--ip 192.168.1.10` if clients will connect by IP address instead of hostname. Use `--output` and `--pem-output` to save the files somewhere else.

### Step 2 — Enable remote connections

Point `--cert-path` at the PFX file from Step 1:

```bash
# Linux
ck remote enable --cert-path /usr/share/Kvaser/CanKing/ca-certificates/myserver.example.com.pfx \
  --service-api-key a-strong-secret
```

```powershell
# Windows PowerShell
ck remote enable --cert-path "C:\ProgramData\Kvaser\CanKing\ca-certificates\myserver.example.com.pfx" `
  --service-api-key a-strong-secret
```

The `--service-api-key` is a shared secret that the clients need to include in each call to be authenticated.

Include `--cert-password` if you set a password in Step 1:

```bash
ck remote enable \
  --cert-path /usr/share/Kvaser/CanKing/ca-certificates/myserver.example.com.pfx \
  --cert-password MySecret \
  --service-api-key a-strong-secret
```

> **Note:** `ck remote` commands must reach the local `CanKingService`. If `CANKING_REMOTE_URL` is set on the server, unset it or add `--url localhost`, otherwise the command is sent to the remote host.

`CanKingService` restarts automatically and is now reachable over the network.

### Step 3 — Share the public certificate

Copy `myserver.example.com.pem` to every client machine that needs to connect. You can use any file transfer method (USB, network share, `scp`, email, etc.). Only the `.pem` file is needed on the client — never share the `.pfx`.

Make a note of the port number used by CanKingService, it will be needed when connecting the client.

---

## On the client (the machine running `ck`)

### Step 4 — Install the server certificate

The client needs to trust the server certificate before a secure connection can be established.

**Linux (Debian/Ubuntu):**

```bash
sudo cp myserver.example.com.pem /usr/share/Kvaser/CanKing/ca-certificates/
```

-or-

```bash
sudo cp myserver.example.com.pem /usr/local/share/ca-certificates/myserver.example.com.crt
sudo update-ca-certificates
```

> **Note:** The file extension is changed from `.pem` to `.crt` since `update-ca-certificates` only processes `.crt` files.

**Linux (RHEL/Fedora/CentOS):**

```bash
sudo cp myserver.example.com.pem /usr/share/Kvaser/CanKing/ca-certificates/
```

-or-

```bash
sudo cp myserver.example.com.pem /etc/pki/ca-trust/source/anchors/
sudo update-ca-trust extract
```

**Windows (PowerShell):**

```powershell
Copy-Item .\myserver.example.com.pem $env:ProgramData\Kvaser\CanKing\ca-certificates\
```

-or-

```powershell
Import-Certificate -FilePath ".\myserver.example.com.pem" -CertStoreLocation Cert:\CurrentUser\Root
```

### Step 5 — Set the API key

```bash
# Linux / macOS
export CANKING_REMOTE_API_KEY="a-strong-secret"

# Windows PowerShell
$env:CANKING_REMOTE_API_KEY = "a-strong-secret"
```

### Step 6 — Verify the connection

```bash
ck get about --url https://myserver.example.com:36701
```

A successful response shows the remote service version. You can now use any `ck` command against the remote service by appending `--url https://myserver.example.com:36701`. The end of the URL (36701) should match the port number used by CanKingService.

> **Tip:** Set `CANKING_REMOTE_URL=https://myserver.example.com:36701` in your environment to avoid typing `--url` on every command. While it is set, every `ck` command goes to the remote service; add `--url localhost` to target a local `CanKingService`.

### Step 7 — Connecting CanKing GUI Application

Click on the `Remote Connection` button in the left-side navigation bar. Enter the `Service URL` and the `API Key` and click on Connect. When connected a green banner will be visible in CanKing's title bar.

---

## Disabling remote access

Run this command on the server to turn off the remote listener:

```bash
ck remote disable
```

`CanKingService` restarts automatically with remote access disabled.
