# Networking and ports

Which ports are already taken on a QNAP NAS, how to check one, and where to bind a service you add.

## Checking a port

`ss` does not exist on the NAS. Use `netstat`:

```sh
netstat -tulpn | grep ':8090 '   # anything listening on TCP or UDP port 8090, with its program
```

A port that looks free today can be taken by a package installed later, and some packages bind the wildcard
address: another package's nginx on `0.0.0.0` took two loopback ports another program expected, so its server
failed with "Address already in use". Check at start, and fail with a message that names the port.

## Ports the firmware uses

The ports each QTS 6.1 init script names in its code, from QNAP Club Poland's service reference. Stay off them:

| Script | What it runs | Ports |
|---|---|---|
| `Qthttpd.sh` | a web listener of the administration panel (Qthttpd, apache) | 80, 443, 5000, 8080, 8081 |
| `stunnel.sh` | TLS in front of services without their own | 80, 443, 8007 |
| `login.sh` | the console login (sshd, utelnetd) | 22, 13131 |
| `atalk.sh` | Apple file sharing and Time Machine | 548 |
| `mariadb.sh` | the built-in database server | 3310 |

On a QTS 6.1 unit, dnsmasq also answered on port 53 of the container bridges, on `127.0.1.1` and on `::1`. A DNS
server bound to the wildcard address then fails with "Address already in use"; bind it to the NAS's LAN address
instead.

## Where to bind

- A service only other programs on the NAS use: `127.0.0.1`.
- A service for the LAN: the NAS's LAN address, or the wildcard address when no other program holds the port on
  any address.
- Take the port from your package's settings (`setcfg <QPKG_NAME> <Key> <port> -f /etc/config/qpkg.conf`) so the
  admin can move it, and put the web port in `QPKG_WEB_PORT` for App Center's Open link ([qpkg.md](qpkg.md)).
- Ports below 1024 need root. A QPKG's service runs as root, so it can bind one, but the firmware already holds
  the common ones (above).

## Bucket & Code Server IDE's ports

- 9950 (HTTP), 9951 (HTTPS) and 9952 (Git over SSH) by default, changed on the panel's Network page; ports there are
  1024 to 65535 and differ from each other.
- On `127.0.0.1`: 9960 and 9961 for its modules, or a free port of 9962 to 9989 when another program took one.
- VS Code's Ports view in the editor reaches a server you run during development at `/ide/proxy/<port>/`; the
  package's own loopback ports are refused there.
- Published on the internet through a Cloudflare Tunnel, the package needs no open port on the router. Git over
  SSH does not go through the tunnel: an SSH clone address names the LAN address, or a name the admin set.

## Sources

QNAP Club Poland (forum.qnap.net.pl): [services-qts61.md](https://forum.qnap.net.pl/llms/services-qts61.md) (each
init script's ports), [linux-gap.md](https://forum.qnap.net.pl/llms/linux-gap.md) (`ss`, `netstat`). The port 53
and wildcard-address findings were measured while building Bucket & Code Server IDE's DNS server example and its
modules; the package's ports come from its user guide (`bvs guide`, "Network").
