sshd Match Address and LocalAddress Rules for Hosts in Your Free /48
Disclosure up front: I run IPYE, a company that leases IP space. Most of this article is practical how-to; IPYE appears where a free /48, LOA, or ROA helps you try the work, with a short call to action at the end. OpenS
Disclosure up front: I run IPYE, a company that leases IP space. Most of this article is practical how-to; IPYE appears where a free /48, LOA, or ROA helps you try the work, with a short call to action at the end.
OpenSSH can apply different rules depending on where a connection comes from and which local address it reached. With addresses from your own prefix, those distinctions are precise: your management network is a known /64, your service addresses are known /128s, and nothing hides behind a shared NAT address. This guide uses Match Address and Match LocalAddress in sshd_config to apply stricter rules to the Internet than to your own network, for hosts numbered from a free IPv6 /48 from IPYE.
The plan
- management network:
2001:db8:100:5::/64 - the rest of your network:
2001:db8:100::/48 - everyone else: the Internet
The host listens on its management address and, for a deploy user that runs automated jobs, on one service address. Rules tighten as the source moves further away.
Global defaults: strict
# /etc/ssh/sshd_config.d/20-policy.conf
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
AllowTcpForwarding no
X11Forwarding no
AllowAgentForwarding no
MaxAuthTries 3
Settings outside any Match block apply to every connection. Starting strict means that anything a later block forgets to relax stays safe.
Relax for the management network
Match Address 2001:db8:100:5::/64
AllowTcpForwarding yes
AllowAgentForwarding yes
PermitRootLogin prohibit-password
Match Address accepts CIDR prefixes and lists. Connections from the management /64 may forward ports and agents, and root may log in with a key, which helps during recovery. Connections from anywhere else keep the strict defaults.
Restrict the Internet further
Match Address *,!2001:db8:100::/48
AllowUsers deploy
PermitTTY no
ForceCommand /usr/local/bin/deploy-entry
The pattern *,!2001:db8:100::/48 matches any address except those inside your /48. Connections from outside your network may log in only as deploy, without a terminal, and run only a fixed command. That suits automated deployment from an external CI service while keeping interactive logins internal.
Order matters. sshd applies the first value it finds for each setting, scanning Match blocks in file order, so put the more specific block first when two blocks could match the same connection.
Match on the local address
Match LocalAddress applies rules based on which of the host's addresses the client connected to:
Match LocalAddress 2001:db8:100:10::22
AllowUsers deploy
PasswordAuthentication no
With sshd listening on both the management address and a service address, this keeps the service address limited to the deploy user, whatever the source. Combine it with ListenAddress lines for the two addresses so sshd does not listen anywhere else.
Test before you reload
sshd -t checks syntax, and sshd -T with a connection specification shows the effective settings for a hypothetical connection:
sshd -t
sshd -T -C user=root,host=test,addr=2001:db8:100:5::10 | grep -E "permitrootlogin|allowtcpforwarding"
sshd -T -C user=root,host=test,addr=2001:db8:ffff::1 | grep -E "permitrootlogin|allowusers"
The first test should show root login allowed by key and forwarding enabled for a management source. The second should show root login refused and allowusers deploy for an outside source. Checking a few cases this way is far safer than reloading and finding out from a locked-out session.
Reload with systemctl reload ssh while keeping an existing session open, and test a new login before closing it.
Pair it with the firewall
Match rules decide what a connection may do after it reaches sshd. The firewall decides whether it reaches sshd at all. If nothing outside your network should ever use SSH, block port 22 from outside at the host or edge firewall and keep the Match rules as a second layer. If the deploy user must be reachable from outside, open port 22 only on the service address it uses.
Logging
Set LogLevel VERBOSE to record the key fingerprint used for each login. Combined with source addresses from your /48, the logs then show exactly which key logged in from which network. Feed them to your log server so they survive a compromised host.
The addresses come from your plan
The rules above are only as good as the address plan behind them. Keep the management /64 for management hosts only, and record it in your IPAM. The /48 itself is announced from your edge with the LOA from the IPYE portal, and the ROA that IPYE creates validates it.
Next step
Claim your free /48, set aside a management /64, and write Match Address rules that relax only for it. Questions: [email protected].
About IPYE
IPYE (https://ipye.com) leases IPv4, IPv6 and ASN space with instant setup. Your first IPv6 /48 is free for the first year, a real allocation you can announce on your own ASN, with a LOA from the portal and the ROA created automatically. ASN and region changes apply automatically. IPv4 /24 to /22 leases include LOA, ROA and per-/24 rDNS; WHOIS is editable on IPv6 /44 and larger. Locations: Los Angeles, New York, Ashburn, Salt Lake City, London, Amsterdam, Frankfurt. Claim your free /48: https://my.ipye.com/products/ipv6-rental. Questions: [email protected].
Disclosure: I run IPYE
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.