Restrict a Service to the Local Network
Keep a service on your LAN with systemd's IPAddressDeny and IPAddressAllow. Test it with systemd-run first, then learn what an address filter cannot stop.
Restrict a service to the local network with two systemd lines
To restrict a service to the local network, add IPAddressDeny=any to its unit. Then list the ranges it may still use in IPAddressAllow=: localhost, link-local, and the private address ranges of your LAN (local area network). systemd then filters every IP packet that the service's sockets send or receive. Traffic to or from any other address is dropped in both directions. A public client cannot connect in, and the service cannot connect out to the internet.
[Service]
IPAddressDeny=any
IPAddressAllow=localhost link-local 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 fc00::/7The filter is a BPF (Berkeley Packet Filter) program that systemd attaches to the unit's control group (cgroup), which is the kernel's grouping of all the service's processes. The sibling guide on systemd sandboxing with ProtectSystem and PrivateTmp gives these settings one line in a wider hardening tour. This page answers only the LAN-only question.
Some details decide whether these two lines do what you expect. The filter matches addresses, not network cards. On some systems it silently does nothing, so you must test it. DNS (Domain Name System) lookups take a path you should check before you trust the result. The sections below cover each one, and then the other tools: RestrictNetworkInterfaces=, PrivateNetwork=yes, and an nftables rule for one-off commands.
How IPAddressAllow and IPAddressDeny decide
The systemd.resource-control(5) man page gives a fixed order for every packet:
- If the address matches an entry in
IPAddressAllow=, access is granted. - Otherwise, if it matches an entry in
IPAddressDeny=, access is denied. - Otherwise, access is granted.
The allow list wins. That is why IPAddressDeny=any alone blocks everything, including localhost, and why the allow list must name every range the service needs.
Which address is checked depends on direction. For incoming (ingress) traffic, the filter checks the source address. For outgoing (egress) traffic, it checks the destination address. So a request from a public IP to your service is dropped on the way in, and a connection from your service to a public IP is dropped on the way out.
Each setting takes a space-separated list of IPv4 or IPv6 addresses. An address can carry a prefix length in CIDR (classless inter-domain routing) form, such as /24. An address without a prefix matches one host. Four special names save typing:
anycovers0.0.0.0/0and::/0.localhostcovers127.0.0.0/8and::1/128.link-localcovers169.254.0.0/16andfe80::/64.multicastcovers224.0.0.0/4andff00::/8.
There is no special name for private ranges, so you write them out. RFC 1918 (Request for Comments 1918) defines three private IPv4 blocks: 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. Note the /12. The middle block runs from 172.16.0.0 to 172.31.255.255, and a common mistake is to write 172.16.0.0/16, which covers only a sixteenth of it. For IPv6, RFC 4193 defines ULA (unique local addresses) as fc00::/7. Write that full range in the unit file.
A few more rules from the man page matter in practice. Both lists are empty by default. Repeated lines in one unit add to the list, and an empty assignment such as IPAddressAllow= resets it. The lists merge with those of the parent slice. The filter applies to all sockets created by processes of the unit, and you cannot bypass it by prefixing an ExecStart= path with +.
Check your systemd version and your addresses
Run this first:
systemctl --version
ip -brief addressThe first line of systemctl --version prints the systemd version number. IPAddressAllow= and IPAddressDeny= were added in version 235. RestrictNetworkInterfaces=, covered further down, was added in version 250.
ip -brief address lists every interface with its addresses. Read it carefully, because this is your real LAN. On a VPS, the "local network" is usually the provider's private network on a second interface, or a VPN tunnel. It is rarely a home router's 192.168.1.0/24. If your private interface shows 10.20.0.5/24, then 10.20.0.0/24 is the range you want, and allowing all of 10.0.0.0/8 is far wider than you need.
Test the filter with systemd-run before you edit a unit
systemd-run starts a command as a temporary (transient) service. Its -p option sets a property on that unit, in the same format as systemctl set-property. So you can try the exact filter on one command, see it work, and only then write it into a real unit.
First, a baseline with no filter:
sudo systemd-run --pipe --quiet \
curl -sS --max-time 5 -o /dev/null -w '%{http_code}\n' http://1.1.1.1--pipe connects the command's input and output to your terminal and waits for it to finish. --quiet hides systemd-run's own status lines. You should see a three-digit HTTP status code. That proves the command can reach a public address.
Now the same command with the LAN-only filter:
sudo systemd-run --pipe --quiet \
-p IPAddressDeny=any \
-p 'IPAddressAllow=localhost link-local 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 fc00::/7' \
curl -sS --max-time 5 -o /dev/null -w '%{http_code}\n' http://1.1.1.1This time curl should print an error line and the code 000. curl prints 000 when it got no HTTP response at all. That is the healthy result: the destination 1.1.1.1 is not in the allow list, so the packet is dropped.
Then point the filtered command at a host on your private network. Replace the address with one from your own ip -brief address output, such as a database or another server on the same private network:
sudo systemd-run --pipe --quiet \
-p IPAddressDeny=any \
-p 'IPAddressAllow=localhost link-local 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 fc00::/7' \
curl -sS --max-time 5 -o /dev/null -w '%{http_code}\n' http://10.20.0.6:8080A status code here means LAN traffic passes. You now know the filter works on this host, in both directions of the test.
If the public test still prints a status code
Then the filter is not active, and nothing told you. The man page warns that these settings might not be supported on some systems, for example where eBPF (extended BPF) cgroup support is missing in the kernel or in the container manager. In that case the settings have no effect. A VPS that is itself a container is the place to suspect first. Do not rely on IPAddressDeny= on a host where this test fails. Use a firewall rule instead.
Does DNS still work through the 127.0.0.53 stub?
This is the question to answer on your own host before you deploy. A service that cannot resolve names often fails in confusing ways.
The reasoning first. On hosts that use systemd-resolved, its DNS stub listens on 127.0.0.53 port 53, and a limited proxy-mode stub listens on 127.0.0.54. Both addresses sit inside 127.0.0.0/8, which the localhost name covers. So a query from your service to the stub is allowed. The query to the upstream DNS server is then sent by systemd-resolved, which is a different service in a different cgroup. Your unit's filter only covers sockets created by your unit's processes, so the upstream query is outside it. Some lookups never touch IP at all: the nss-resolve module talks to systemd-resolved over the AF_UNIX socket /run/systemd/resolve/io.systemd.Resolve, and the IP filter covers only IPv4 and IPv6 sockets.
Reasoning is not proof, so check two things. First, see where your resolver points:
cat /etc/resolv.confLook at the nameserver lines. 127.0.0.53 means the local stub. A public address there means programs send DNS queries straight to that public server.
Second, resolve a name under the filter:
sudo systemd-run --pipe --quiet \
-p IPAddressDeny=any \
-p 'IPAddressAllow=localhost link-local 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 fc00::/7' \
getent ahosts example.comIf this prints addresses, lookups work under the filter on this host. If it prints nothing and exits with an error, check /etc/resolv.conf again. A public nameserver line is a destination outside the allow list, so those queries are dropped like any other public traffic. Fix it in one of two ways. Add that one resolver's address to IPAddressAllow=, or point the host at a local resolver.
One more check is worth the minute. getent uses the system resolver library. Some programs read /etc/resolv.conf themselves and send their own DNS packets. Run your real program under the same filter, not only getent, before you trust the result.
Write the filter into the service unit
Use a drop-in file, so a package upgrade does not overwrite your change:
sudo systemctl edit myapp.serviceAdd the lines in the editor that opens. Use the narrowest list that works. This example allows only localhost and one private subnet:
[Service]
IPAddressDeny=any
IPAddressAllow=localhost 10.20.0.0/24If the service uses discovery protocols that send to multicast addresses, add multicast to the allow list. If it talks to link-local addresses, add link-local. Then restart it and read back the settings:
sudo systemctl restart myapp.service
systemctl show myapp.service -p IPAddressDeny -p IPAddressAllowsystemctl show should list the ranges you wrote. An empty value means the drop-in did not apply. Check the file name in systemctl cat myapp.service. The same systemctl edit drop-in is how you limit a service's memory and CPU with systemd, so you can keep both kinds of limits in one file.
Two cases need extra care. A socket-activated service has two units. The lists on the .socket unit cover the sockets that unit creates, not the sockets of the service it starts, and the reverse is also true. Put the same lines on both units. Second, if the service needs a public server at startup and exits when it cannot reach it, the unit's Restart= setting decides whether it loops. Read how systemd restart policies work before you blame the filter for a crash loop.
The man page also describes the opposite design: deny any on a higher slice, such as system.slice, and then allow addresses service by service. That is stricter for the whole host. This guide restricts one service and leaves the rest alone, which is the safer change on a server that is already running.
Prove it from both sides
The service still listens on its port, so ss -tlnp on the server shows the socket as before. Listening is not the test. Connecting is.
From a machine on the public internet, connect to the server's public address and the service port. The connection should time out, because the incoming packets come from a public source address and are dropped. From a machine on the private network, the same connection should work. The guide on checking if a port is open on Linux covers the client-side commands for this.
If you only need the service to stop listening on the public address, the simpler fix is the application's own listen setting: bind it to the private address. The systemd filter adds what a listen address cannot. It also blocks the service's outgoing traffic, and it still holds if someone later changes the listen address back to 0.0.0.0.
What an address filter does not stop
The filter checks addresses. It does not know which network card a packet uses or who owns an address. That leads to some limits you should know.
Any private address counts as local. A WireGuard tunnel that uses 10.8.0.0/24 is "local" under a 10.0.0.0/8 allow rule, even if the peer is in another country. Docker networks also count. Docker's built-in default address pools are inside 172.16.0.0/12 and 192.168.0.0/16, so a container on a Docker bridge network passes a full RFC 1918 allow list. Run docker network inspect bridge or ip address show docker0 to see the subnet on your host. The fix is the narrow list: allow your real LAN subnet, not every private range.
Local relays forward traffic for you. The filter allows localhost, and anything listening there runs in its own cgroup with its own rules. A local HTTP proxy with internet access will fetch public pages for your service. The DNS stub is a relay too. Names your service looks up are forwarded to the upstream resolver, so data can still leave the host inside DNS queries.
Other socket families are outside the filter. The man page scopes it to AF_INET and AF_INET6 sockets. To restrict socket() calls to those families and local Unix sockets, add RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 to the same drop-in. This does not close every route to other families: sockets created through io_uring and sockets passed into the process, including through socket activation, are unaffected. Sockets created with socketpair() are also unaffected. By default RestrictAddressFamilies= applies no restriction at all. If the service then fails at start, read its journal. Programs that list network interfaces may also need AF_NETLINK.
Processes outside the unit are outside the filter. If your unit only runs a client that asks another daemon to start the real workload, the workload's processes may live in a different cgroup. Run systemctl status myapp.service and check that the processes doing the network work appear in its process tree. Docker containers in particular have their own network rules, described in why Docker published ports bypass ufw.
On some systems the filter does nothing. That is why the systemd-run test above is not optional.
RestrictNetworkInterfaces: filter by interface instead
RestrictNetworkInterfaces= limits the network interfaces the unit's processes can use. It takes a space-separated list of interface names. By default the list is an allow list. A ~ at the start turns it into a deny list. It was added in systemd 250, and it is documented in systemd.resource-control(5), next to the IP settings.
The man page warns that the loopback interface lo gets no special treatment. You must list it, or localhost traffic breaks, and that includes the DNS stub. If your private interface is eth1:
[Service]
RestrictNetworkInterfaces=lo eth1This solves the problem the address filter cannot: a WireGuard interface or a Docker bridge is excluded by name, whatever addresses it carries. It has the opposite weakness. It does not care which addresses sit behind eth1. You can use both settings in one unit, so the interface list rules out tunnels and bridges, and the address list rules out public addresses.
PrivateNetwork=yes: loopback only
PrivateNetwork=yes is not a LAN-only tool. It starts the service in a new network namespace that contains only the loopback device lo. No other network devices are available to the process. A client on your LAN cannot reach the service, and the service cannot reach the LAN.
The new namespace has its own loopback, so services that listen on the host's 127.0.0.1 are out of reach too, including the DNS stub. The man page adds that the option disconnects all socket families from the host, including AF_NETLINK and AF_UNIX. Host AF_UNIX sockets in the abstract namespace become unavailable. AF_UNIX sockets that live in the file system stay reachable. Host services remain reachable through those sockets, subject to permissions. Several units can share one private namespace with JoinsNamespaceOf=.
Use PrivateNetwork=yes for jobs that need no network at all, such as a converter or a batch job that reads and writes local files. Use the address filter when the service must still talk to your LAN.
One-off commands: an nftables rule that matches the cgroup
For a single command, systemd-run -p with the IP properties is usually enough. Sometimes you want the policy in the firewall instead, where nft list ruleset shows it and counters prove it matched. nftables can match the cgroup of the socket that sent a packet with socket cgroupv2 level N "<path>". The path is relative to /sys/fs/cgroup, and level 1 is its first component. The older meta cgroup match reads the net_cls class ID, which is zero on hosts that run only cgroup v2, so do not use it for this.
The plan: create a slice that sits directly under the cgroup root, run commands inside it, and reject their traffic to public addresses. First, the slice:
sudo tee /etc/systemd/system/lanonly.slice > /dev/null <<'EOF'
[Unit]
Description=Commands limited to the local network
EOF
sudo systemctl daemon-reload
sudo systemctl start lanonly.slice
ls -d /sys/fs/cgroup/lanonly.sliceThe last command should print the directory. Do not load the firewall rule until it does. Then save this as /etc/nftables-lanonly.nft:
table inet lanonly {
chain output {
type filter hook output priority filter; policy accept;
socket cgroupv2 level 1 "lanonly.slice" ip daddr { 127.0.0.0/8, 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 169.254.0.0/16 } accept
socket cgroupv2 level 1 "lanonly.slice" ip6 daddr { ::1, fe80::/64, fc00::/7 } accept
socket cgroupv2 level 1 "lanonly.slice" counter reject
}
}The table has its own name and an accept policy, so it leaves traffic from every other cgroup alone. Check the file, load it, and run a command in the slice:
sudo nft -c -f /etc/nftables-lanonly.nft
sudo nft -f /etc/nftables-lanonly.nft
sudo systemd-run --slice=lanonly.slice --pipe --quiet \
curl -sS --max-time 5 -o /dev/null -w '%{http_code}\n' http://1.1.1.1
sudo nft list table inet lanonlynft -c checks the file without loading it, so a syntax error stops you before anything changes. The curl command should print 000. The counter on the reject rule should now show packets. If curl prints a status code and the counter is still zero, the rule did not match your command's cgroup. Check the path with systemd-cgls while a command runs in the slice.
This rule sits in the output hook, so it covers traffic the commands send. For a long-running server that must also refuse public clients, use the unit settings above. The table lasts until reboot. To keep it, add the file to the nftables configuration your host loads at boot, and make sure the slice is started first. The guides on iptables and nftables on Ubuntu and listing the active firewall rules show where that configuration lives and how to read what is loaded.
FAQ
How do I restrict a systemd service to the local network?
Add a drop-in with sudo systemctl edit myapp.service containing IPAddressDeny=any and an IPAddressAllow= line that lists localhost and your private subnet, for example IPAddressAllow=localhost 10.20.0.0/24. Restart the service. The allow list is checked first, so only the listed ranges get through, in both directions. Test the same filter on one command with systemd-run -p first, and confirm that a public address is blocked.
Does IPAddressDeny=any break DNS lookups?
Not when lookups go through systemd-resolved and the allow list includes localhost. The stub listens on 127.0.0.53, which localhost covers, and the upstream query is sent by systemd-resolved, outside your unit's filter. If /etc/resolv.conf lists a public DNS server directly, those queries are blocked like any other public traffic, so allow that server's address. Confirm on your host with getent ahosts example.com run under systemd-run with the same properties.
Why can my LAN-only service still reach a VPN peer or a Docker container?
The filter matches IP addresses, not network interfaces. A WireGuard tunnel or a Docker bridge network that uses a private range passes an allow rule for that range. Allow only your real LAN subnet instead of all RFC 1918 ranges, or add RestrictNetworkInterfaces=lo eth1 to limit the service to named interfaces.
Why does IPAddressDeny have no effect on my server?
The systemd man page says these settings might not be supported on some systems, for example where eBPF cgroup support is missing in the kernel or the container manager, and then they do nothing. Run a blocked test with systemd-run -p IPAddressDeny=any and curl to a public address. If curl still gets a status code, use an nftables rule or a provider firewall instead. Also check that the processes doing the network work belong to the unit, since the filter covers only the unit's own cgroup.
What is the difference between IPAddressDeny, RestrictNetworkInterfaces and PrivateNetwork?
IPAddressDeny= with IPAddressAllow= filters by address, so the service can talk to the ranges you list. RestrictNetworkInterfaces= filters by interface name, and you must list lo yourself. PrivateNetwork=yes gives the service its own network namespace with only a loopback device, so it cannot directly reach the host's 127.0.0.1 services or your LAN through that namespace. Host services remain reachable through filesystem Unix sockets, subject to permissions.