# Start with the system

qKVM can start with the computer and keep running. In a container, `--restart unless-stopped` or Compose does this; on Linux, use a systemd service, and on Windows a scheduled task.

## Linux: a systemd service

1. Install qKVM in an environment of its own:

```
python3 -m venv /opt/qkvm
/opt/qkvm/bin/python -m pip install --upgrade pip
/opt/qkvm/bin/python -m pip install qkvm
```

2. Save this as `/etc/systemd/system/qkvm.service`:

```
[Unit]
Description=qKVM
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/opt/qkvm/bin/qkvm --data-dir /var/lib/qkvm --vault-key-file /var/lib/qkvm-keys/vault.key --https --setup-code
DynamicUser=yes
User=qkvm-web
Group=qkvm-web
StateDirectory=qkvm qkvm-keys
StateDirectoryMode=0700
UMask=0077
CapabilityBoundingSet=
ProtectKernelTunables=yes
ProtectKernelModules=yes
ProtectControlGroups=yes
Restart=on-failure

[Install]
WantedBy=multi-user.target
```

3. Run `systemctl enable --now qkvm`.
4. Find the setup code in `journalctl -u qkvm`, and choose the first password.

The service runs as a user of its own, with a private data folder (`/var/lib/qkvm`) and key folder (`/var/lib/qkvm-keys`). Back the two up separately.

**Use a name no account has.** `DynamicUser=yes` reuses an account whose name exists already, with its groups. Never run the service as an administrator, or as an account in `sudo`, `docker` or `lxd`.

### Keep the unlock key out of the data folder

```
install -d -m 700 /etc/qkvm
head -c 32 /dev/urandom | base64 > /etc/qkvm/unlock.key
chmod 600 /etc/qkvm/unlock.key
```

Then add `LoadCredential=qkvm-unlock:/etc/qkvm/unlock.key` under `[Service]`, and ` --unlock-file %d/qkvm-unlock` to the end of the `ExecStart` line. Sign in once; from the next restart on, this key opens the saved passwords.

## Windows: a task at log-on

In PowerShell:

```
schtasks /Create /TN "qKVM" /SC ONLOGON /RL LIMITED /TR "pythonw -P -m qkvm"
Set-ScheduledTask -TaskName qKVM -Settings (New-ScheduledTaskSettingsSet -ExecutionTimeLimit 0 -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries)
```

The second line keeps Windows from ending the task after three days, or when a laptop runs on its battery.

Started this way there is no terminal. What qKVM would say there, the setup code included, is in `qkvm.log` in the data folder; stop it with `qkvm stop` from any terminal. qKVM shows pictures as a Windows service and over SSH as well.

## The data folder's permissions

Keep the data folder writable by qKVM's account alone: whoever can write there can stop qKVM and change its files. qKVM says so at start when it is not, with the command that fixes it. On Windows, keep the folder on NTFS.

## Related

- [Keeping it running](https://qkvm.com/docs/web/keeping-it-running)
- [Command line](https://qkvm.com/docs/web/command-line)
- [Install and run](https://qkvm.com/docs/web/run-it)

[Full reference](https://github.com/qkvm/qkvm/blob/main/GUIDE.md#keeping-it-running)
