Guide · Agent keep-alive

Agent keep-alive · LAN mode One host watches every phone.

Install a watchdog on an always-on Linux host or VM on your network. When ketec is killed, hangs or the phone restarts, it re-mounts the developer disk image and launches ketec again. No VPN on the phones — built for device racks and fleets.

What it is

Agent mode needs ketec to stay running, and iOS does not let an app relaunch itself. So ketec's Agent keep-alive comes in two modes:

VPN mode

The phone watches itself, no computer needed. The catch: it keeps the iOS VPN slot busy (iOS allows only one VPN at a time), so it is off by default.

LAN mode (this guide)

An always-on Linux host or VM on your network watches all phones: when ketec is killed, hangs or the phone restarts, it re-mounts the developer disk image and launches ketec. No VPN on the phones. Made for device racks and fleets.

Four steps in total: install the service on the host → pair each phone once on a Mac → register the pairing files on the host → turn on “LAN keep-alive” in the app.

Before you start

You need a watchdog host, a Mac (only for the first pairing) and phones that already run ketec.

Watchdog host

  • An always-on x86_64 Linux machine: Ubuntu 22.04+ / Debian 12+ / Rocky or Alma 9+ (glibc 2.34+ and systemd). Bare metal or a VM; 1 CPU core and 512 MB RAM are enough.
  • It can reach every phone's IP — same subnet or routed — with TCP 49152–49160 and 9800 open from the host to the phones.
  • It has internet access: after a phone reboots, re-mounting the developer disk image means downloading the image and fetching a signature from Apple's servers. The installer also downloads the program from GitHub.
  • The phones can reach TCP 9811 on the host (used to report a new address after an IP change; see What if a phone changes its IP?).

A Mac with Apple silicon + a cable

  • Used once per phone for the first pairing. After that everything goes over Wi‑Fi.
  • On iOS 27 and later you can pair on the phone with a 6-digit code instead, no cable (ketec-lan pair-host, see the FAQ).

Each phone

  • Developer Mode is on and ketec already works in Agent mode.
  • Has a fixed IP reserved on your router (DHCP reservation): the host finds phones by IP.
  • Settings → Display & Brightness → Auto-Lock is set to Never: when the screen sleeps, Wi‑Fi sleeps too and the host can no longer reach the phone.
  • Preferably no passcode: with a passcode, the phone must be unlocked once after every restart before it joins the network.

1. Install the service on the host

Run one command on the host. The script downloads and verifies ketec-lan, installs it as the systemd service ketec-lan-watch (starts at boot, restarts 5 seconds after a crash), generates an access token and opens port 9811 in firewalld / ufw.

terminal — host
curl -fsSL https://ketec.top/lan/install.sh | sudo bash

When it finishes it prints something like this (the token is random; shown here as <token>):

output
==> 下载 ketec-lan(lan-v1.0.0) / downloading ketec-lan
==> 安装到 /opt/ketec-lan / installing
==> firewalld 已放行 9811/tcp

  ✅ ketec-lan-watch 已运行 / running(已登记 0 台设备 / 0 device(s) registered)

  下一步 Next:
    1. 在 Mac 上用数据线配对每台手机 / pair each iPhone over USB on a Mac:
         ketec-lan pair --name 01
    2. 把 ~/.ketec-lan/pairings/<UDID>.plist 拷到本机,然后登记 / copy it here, then register:
         ketec-lan add /path/<UDID>.plist --host <手机IP> --name 01
    3. 看状态 / status:
         curl -H "Authorization: Bearer <token>" http://192.168.1.50:9811/api/watch
       日志 / logs:  journalctl -u ketec-lan-watch -f

  令牌 Token: <token>   (保存在 /etc/ketec-lan/env)
  教程 Guide: https://ketec.top/guide-lan-keepalive.html
  • Token: stored in /etc/ketec-lan/env (mode 600). You need it to read the status and call the API. Forgot it? Run sudo cat /etc/ketec-lan/env.
  • Status URL: http://host-ip:9811/api/watch — see “Check it works” below.
  • Files: the program lives in /opt/ketec-lan/ (command ketec-lan); pairing files and the device list live in /var/lib/ketec-lan/.
  • Upgrade: run the same command again. The token and registered devices are kept.

Uninstall (pairing files stay in /var/lib/ketec-lan; delete them yourself once you are sure you no longer need them):

terminal — host
curl -fsSL https://ketec.top/lan/install.sh | sudo bash -s -- --uninstall

2. Pair each phone on a Mac

Pairing gives the host the key it needs to talk to the phone. Do it once per phone.

Download ketec-lan-macos-arm64 (checksums in SHA256SUMS). In Terminal, go to the download folder, make it executable and remove the quarantine flag:

terminal — Mac
cd ~/Downloads
chmod +x ketec-lan-macos-arm64
xattr -d com.apple.quarantine ketec-lan-macos-arm64

If it says No such xattr, the file has no quarantine flag — just carry on.

Connect the phone to the Mac with a cable and pair. --name is a name you choose for this phone; later commands accept it instead of the UDID:

terminal — Mac
./ketec-lan-macos-arm64 pair --name 01

When the phone asks “Trust This Computer?”, tap Trust and enter the passcode.

The pairing file is saved as ~/.ketec-lan/pairings/<UDID>.plist. Unplug the phone and repeat for the next one (names 02, 03 and so on).

terminal — Mac
ls ~/.ketec-lan/pairings/

3. Register the pairing files on the host

On the Mac, copy the pairing files to the host (replace <code>user</code> with your user name on the host):

terminal — Mac
scp ~/.ketec-lan/pairings/*.plist user@192.168.1.50:~/

Register each phone on the host. --host is the phone's fixed IP. As a regular user, just put sudo in front of the command (not needed as root):

terminal — host
sudo ketec-lan add ~/<UDID>.plist --host 192.168.1.101 --name 01
rm ~/<UDID>.plist

add copies the pairing file into /var/lib/ketec-lan/pairings/ (mode 600) and tries the phone once. check.ok: true in the reply means the host can reach it; false is fine too (for example, the screen is off) — the watchdog keeps retrying. Then delete the copy in your home directory (the second line above).

Within 15 seconds of registering, the watchdog starts watching the phone. No service restart needed.

Register many phones

With many devices, put one line per phone — “UDID IP name” — in devices.txt and register them in a loop:

devices.txt
00008110-000000000000001A 192.168.1.101 01
00008110-000000000000001B 192.168.1.102 02
00008110-000000000000001C 192.168.1.103 03
terminal — host
while read -r udid ip name; do
  [ -z "$udid" ] && continue
  sudo ketec-lan add "$HOME/$udid.plist" --host "$ip" --name "$name" && rm -f "$HOME/$udid.plist"
done < devices.txt
sudo ketec-lan devices

4. Turn on LAN keep-alive on the phone

Open ketec on the phone, go to the “Agent keep-alive” card in Settings and turn on the “LAN keep-alive” row (this turns “VPN keep-alive” off). Tap ⓘ at the right of a row for details; the LAN keep-alive info has a “Setup guide” button that opens this page.

Once it is on, check the status line: “Watched by LAN host (192.168.1.50)” means the host is watching this phone; “No LAN host detected yet” means the host has not reached it yet — see “Check it works” above and the FAQ.

Check it works

Read the watch status on the host. Only root can read the token file, so fetch the token with sudo first; from another computer, paste the token in directly:

terminal — host
TOKEN=$(sudo sed -n 's/^KETEC_LAN_TOKEN=//p' /etc/ketec-lan/env)
curl -H "Authorization: Bearer $TOKEN" http://192.168.1.50:9811/api/watch

Sample reply — one entry per device, keyed by the device name (or the UDID if it has no name):

GET /api/watch
{
  "success": true,
  "data": {
    "now": 1796380000,
    "devices": {
      "01": {
        "udid": "00008110-000000000000001A",
        "state": "watching",
        "addr": "192.168.1.101:49152",
        "appRunning": true,
        "httpOk": true,
        "pid": 1146,
        "launches": 3,
        "activations": 1,
        "relaunches": 0,
        "remounts": 1,
        "lastCheck": 1796379998,
        "lastLaunch": 1796376112,
        "lastError": ""
      }
    }
  }
}
FieldMeaning
statewatching watching · connecting connecting · offline phone unreachable · reconnecting reconnecting after a drop
addrAddress of the phone in use
appRunningWhether the ketec process is running
httpOkWhether ketec's HTTP service (port 9800) responds
pidketec's process ID
launchesTimes ketec was launched because it was not running
activationsTimes ketec was brought to the foreground because its process was there but HTTP was down
relaunchesTimes ketec was killed and restarted after bringing it forward did not help
remountsTimes the developer disk image was re-mounted (usually after a phone restart)
lastCheckTime of the last check (Unix time, seconds)
lastLaunchTime of the last launch / foreground / restart (Unix time, seconds)
lastErrorLast connection error; empty when all is well

Follow the log:

terminal — host
journalctl -u ketec-lan-watch -f

Sample log:

journalctl
Dec 04 21:18:02 lan-host ketec-lan[812]: [ketec-lan] 状态接口已启动:http://0.0.0.0:9811/api/watch(需要令牌)
Dec 04 21:18:02 lan-host ketec-lan[812]: [ketec-lan] 看门狗启动:1 台设备,每 5 秒检查一次 top.ketec.app(每 15 秒重读登记表,新加的设备自动看守)
Dec 04 21:18:04 lan-host ketec-lan[812]: [ketec-lan] [01] 已连上(192.168.1.101:49152),开始看守 top.ketec.app
Dec 04 21:25:41 lan-host ketec-lan[812]: [ketec-lan] [01] top.ketec.app 不在运行,已拉起(pid 1146)
Dec 04 22:03:17 lan-host ketec-lan[812]: [ketec-lan] [01] 已重挂开发者镜像
Dec 04 22:03:18 lan-host ketec-lan[812]: [ketec-lan] [01] 已连上(192.168.1.101:49152),开始看守 top.ketec.app
Dec 04 22:03:34 lan-host ketec-lan[812]: [ketec-lan] [01] 进程在但 HTTP 不通,已切到前台(pid 1187)

The service logs in Chinese for now. In order, the lines say: status API started; watchdog started; connected to 01 and watching top.ketec.app; ketec was not running and was launched; the developer disk image was re-mounted after a restart; the process was there but HTTP was down, so ketec was brought to the foreground.

How it watches

The watchdog keeps one persistent connection per phone and checks every 5 seconds:

  • Process gone (ketec missing in 2 checks in a row) → launch it.
  • Process there but HTTP down (suspended or prewarmed by iOS) → bring it to the foreground first, without killing it; if HTTP is still down, kill and restart it.
  • Phone restarted or offline → keep reconnecting; once connected, re-mount the developer disk image first, then launch ketec.

Measured (iPhone 12 / iOS 26)

ScenarioRecovery time
ketec killedabout 15 s
ketec hung (process there, HTTP down)about 30 s
Phone restartedabout 30 s after the phone is back on the network, Agent is usable again

What if a phone changes its IP?

A pairing file identifies the phone itself, not its IP: a phone with a new IP does not need to be paired again — the host just needs to learn the new address. In most cases that now happens automatically:

  • ① The app checks in: with “LAN keep-alive” on, ketec remembers the host that watches it. If the host has not probed it for more than 20 seconds (usually because the phone's IP changed), ketec sends its current IP to the host with POST /api/hello every 30 seconds. The host updates its record only after a handshake with the pairing file succeeds (forged check-ins do nothing), then reconnects to the new address right away — about 2 seconds in our tests. Requirement: the phone can reach port 9811 on the host (the reverse direction must work too).
  • ② Broadcast discovery: when host and phone share a subnet, the host recognises the phone's local network broadcast (Bonjour) by the identity in the pairing file while reconnecting, so it finds the phone even at a new IP. Across subnets (with a router in between) broadcasts do not get through, so ① does the job.
  • Manual fallback: neither works only if the IP changes while ketec is not running. Then run sudo ketec-lan host <device> <new-ip>. That is why a fixed IP reserved on the router is still recommended.

When a check-in succeeds, the host log shows:

journalctl
Dec 04 22:40:12 lan-host ketec-lan[812]: [ketec-lan] [01] App 报到新地址 192.168.1.102:49152,握手验证通过,已更新登记
Dec 04 22:40:14 lan-host ketec-lan[812]: [ketec-lan] [01] 已连上(192.168.1.102:49152),开始看守 top.ketec.app

Common commands

Run these on the host; as a regular user, put sudo in front. <device> can be the name, the UDID or the IP.

CommandWhat it does
ketec-lan devicesList registered devices
ketec-lan host 01 192.168.1.111Phone has a new IP: updates the record only; the watchdog uses the new address on its next reconnect, no restart needed
ketec-lan rename 01 A01Rename a device
ketec-lan forget 01Stop watching a phone and delete its pairing file
ketec-lan launch 01 top.ketec.app · ketec-lan stop 01 top.ketec.appLaunch / stop an app by hand
ketec-lan up 01Manual recovery: re-mount the developer disk image if needed, then launch ketec
systemctl status ketec-lan-watch · systemctl restart ketec-lan-watchService status / restart
journalctl -u ketec-lan-watch -fFollow the log
curl -fsSL https://ketec.top/lan/install.sh | sudo bash -s -- --uninstallUninstall the service (pairing files are kept)

API

Port 9811 also serves control endpoints, authorized with the same token (Authorization: Bearer), so you can plug the host into your own fleet tools. Request bodies are JSON:

MethodPathWhat it does
GET/api/watchWatch status of all devices (see “Check it works”)
GET/api/devicesRegistered devices
POST/api/launch {"device","bundleId","kill"?}Launch an app; add "kill": true to stop it first
POST/api/stop {"device","bundleId"}Stop an app
POST/api/up {"device","bundleId"?}Re-mount the developer disk image if needed, then launch the app (bundleId defaults to top.ketec.app)
POST/api/hello {"ip","rpPort"}Phone reports its new address after an IP change (called by the app; no token)
terminal
TOKEN=$(sudo sed -n 's/^KETEC_LAN_TOKEN=//p' /etc/ketec-lan/env)
curl -X POST http://192.168.1.50:9811/api/launch \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"device":"01","bundleId":"top.ketec.app"}'

Success returns {"success": true, "data": …}, failure {"success": false, "message": "…"}; a wrong token gets 401. For ketec's own endpoints on the phone, see the HTTP API reference.

/api/hello needs no token. Body: {"ip": "phone-ip", "rpPort": 0}; reply: {"matched": "<UDID>", "addr": "ip:port"} or {"matched": null} (wrapped in data as usual). The reported address is only a candidate: the host must complete a handshake with a registered pairing file at that address before it updates anything, so a forged check-in cannot change a thing.

terminal — phone side
curl -X POST http://192.168.1.50:9811/api/hello \
  -H "Content-Type: application/json" \
  -d '{"ip":"192.168.1.102","rpPort":0}'

{"success":true,"data":{"matched":"00008110-000000000000001A","addr":"192.168.1.102:49152"}}
{"success":true,"data":{"matched":null}}

FAQ

A phone got a new IP. What now?

In most cases the host follows the new address automatically — see What if a phone changes its IP? above. Only if the IP changed while ketec was not running do you need sudo ketec-lan host <device> <new-ip>.

The state stays offline. Why can't the host connect?

Check in this order: is the screen off (Auto-Lock must be Never)? Is the phone on Wi‑Fi rather than cellular only? Across subnets, do your router and firewall allow TCP 49152–49160 and 9800 from the host to the phone? A phone with a passcode must be unlocked once after a restart. The lastError field and the journalctl log show the exact reason.

Are pairing files safe?

A pairing file is the debugging key to that phone: whoever has it can control the phone over the network. Keep it only on the watchdog host (ketec-lan stores it with mode 600), delete the copies on the Mac and in your home directory on the host once registered, and encrypt any backup. After “Reset Location & Privacy”, “Reset All Settings” or “Erase All Content and Settings” on the phone, the old pairing stops working and you need to pair again.

Can I pair on Windows?

Not yet — please pair on a Mac with Apple silicon. The watchdog host itself only needs Linux.

Can I pair without a cable on iOS 27?

Yes. Run sudo ketec-lan pair-host --name lan-host on the host, then on the phone open Settings → Privacy & Security → Developer Mode → Pair, pick the host and enter the 6-digit code shown in the host's terminal. Host and phone must be on the same subnet (discovery uses Bonjour). The device is registered on the host right away; rename it with sudo ketec-lan rename.

How many phones can one host handle?

Each phone uses one persistent connection and very little resources, so dozens to a hundred or more phones are fine.

Can I use it together with VPN mode?

Not recommended. The two are mutually exclusive in the app: turning on LAN keep-alive switches VPN keep-alive off.