> For the complete documentation index, see [llms.txt](https://rosemary-2.gitbook.io/rosemary-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://rosemary-2.gitbook.io/rosemary-docs/tutorials/internal-dns-resolution.md). Agents can also search and read this documentation through its [MCP server](https://rosemary-2.gitbook.io/rosemary-docs/~gitbook/mcp).

# Internal DNS Resolution through an Agent

***

*Intermediate · 15 minutes*

Resolve internal hostnames (`dc01.corp.local`, `git.internal`, `*.svc.cluster.local`) directly from your operator machine, with no `/etc/hosts` edits and no manual `dig @internal-dns`. Rosemary intercepts DNS on the server and fans queries out to connected agents.

***

### How It Works

When the server boots it installs an iptables / WinDivert / PF rule that redirects all outbound DNS traffic from your machine to the Rosemary DNS proxy on port `5300`:

```
OUTPUT -d <agent-subnet> -p udp --dport 53 -j REDIRECT --to-port 5300
```

The proxy then:

1. Fans the query out to **every connected agent in parallel** over the QUIC channel (`dns_request` message).
2. Returns the **first successful answer**.
3. Falls back to the server's saved system resolvers and to `8.8.8.8` / `1.1.1.1` if no agent has an answer.

The agent itself resolves with its own system resolver first, then public fallbacks. So if `agent-2` is joined to the internal AD domain, it will see and return `dc01.corp.local`; if it is a generic Linux box on the same VLAN, it will use `/etc/resolv.conf`.

***

### Prerequisites

* Rosemary server running with at least one agent connected
* The agent host can already resolve the internal name on its own (test on the agent first with `nslookup dc01.corp.local`)
* No conflicting local DNS server bound to `127.0.0.53:53` if you are using `systemd-resolved` (see Troubleshooting below)

***

### Step 1 - Confirm the DNS Proxy Is Up

On the server REPL:

```
rosemary> settings
```

Look for `dns_port: 5300`. To change it (useful when port 53 is occupied by a stub resolver):

```
rosemary> dns-port 5301
```

***

### Step 2 - Resolve an Internal Name from the Operator

Once an agent is connected, just use any standard tool. No proxy settings, no `--resolver` flag.

```bash
dig dc01.corp.local
```

```
;; ANSWER SECTION:
dc01.corp.local.   60   IN   A   192.168.10.10
```

```bash
host fileserver.corp.local
ping intranet.local
ssh admin@gitlab.corp.local
```

All of these go through the same path:

```
operator → :5300 (Rosemary DNS proxy) → QUIC → agent → agent's system resolver
```

***

### Step 3 - Verify Which Agent Answered

The Rosemary server logs every intercepted query and which agent (or fallback resolver) supplied the answer. Watch the server's stdout / log file while you run lookups from the operator:

```
[dns] dc01.corp.local         A     192.168.10.10   answered by agent-2 (3ms)
[dns] svc.cluster.local       A     10.96.0.1       answered by agent-4 (8ms)
[dns] example.com             A     93.184.216.34   answered by fallback (8.8.8.8) (22ms)
```

Useful when you have multiple agents on different internal networks and want to know which side resolved a name.

***

### Step 4 - Multiple Agents, Overlapping Names

If two agents are joined to different domains and both happen to know the same name (`gitlab.local`, for example), the **first answer wins**. The race is in milliseconds, and the loser's answer is dropped.

If you want deterministic resolution, tag the right agent and use SOCKS5 from your application instead (the SOCKS5 proxy uses *that specific agent's* resolver). For example:

```
rosemary> socks add agent-2 1080
```

```bash
curl --socks5-hostname 127.0.0.1:1080 https://gitlab.local
```

`--socks5-hostname` (note: not plain `--socks5`) makes curl ask the SOCKS server to resolve the name, which keeps the lookup on the agent side.

***

### Step 5 - Reverse DNS (PTR)

PTR lookups for tunneled subnets work the same way. From the operator:

```bash
dig -x 192.168.10.10
```

The query is intercepted, sent to the agent that owns `192.168.10.0/24`, and answered by that agent's resolver.

***

### Troubleshooting

**Lookups time out or return `SERVFAIL`**

The most common cause is a stub resolver on the operator machine binding to `127.0.0.53:53`. The Rosemary iptables rule only intercepts traffic to external `:53`, not to `127.0.0.53`. Either disable `systemd-resolved`'s stub:

```bash
sudo sed -i 's/^#\?DNSStubListener=.*/DNSStubListener=no/' /etc/systemd/resolved.conf
sudo systemctl restart systemd-resolved
sudo ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
```

Or point `/etc/resolv.conf` at a real upstream you control and let Rosemary intercept the outbound query.

**The agent host itself can't resolve the name**

Rosemary uses the agent's resolver as the source of truth. If `nslookup dc01.corp.local` fails on the agent, it will fail through the tunnel too. Fix `/etc/resolv.conf` on the agent first (point it at the internal DNS server you care about).

**Wrong agent answers an ambiguous name**

The DNS proxy returns whichever agent answers first; there is no built-in way to pin a hostname to a specific agent. Use the SOCKS5-per-agent pattern from Step 4 instead: start a SOCKS5 proxy on the agent you want, and have your client resolve through it with `--socks5-hostname`.

***

### Key Points

* DNS goes out :53, gets redirected to the Rosemary DNS proxy on :5300, and is fanned out to every agent in parallel
* The first successful answer wins; falls back to public resolvers on miss
* `--socks5-hostname` is the deterministic way to send a lookup through a specific agent
* The agent's `/etc/resolv.conf` is the upstream — fix it there if internal names don't resolve
* On Linux operators with `systemd-resolved`, disable the stub listener or the intercept rule will be bypassed
