Aliases in OPNsense: Clearer Firewall Rules That Are Easier to Maintain
Repeated rules and scattered addresses, host, network and port aliases, URL Tables, GeoIP and nested aliases, an example with rules and port forwarding, pf tables, clear names and mistakes to avoid.
The firewall works, but the rule list has grown long: dozens of nearly identical lines, with the same addresses and the same ports repeated everywhere. One day the web server changes address and every rule and every port forward that mentions it has to be corrected. Forget one, and a service stops working, or something that should have been closed stays open. Aliases in OPNsense solve this problem: let us look at what they are, which types exist, how to use them in a concrete example, what they become inside the firewall and which mistakes to avoid.
The problem: rules that repeat themselves
Take a small business with three networks: the LAN 192.168.1.0/24, a VLAN 10 for the offices, 192.168.10.0/24, and a DMZ 192.168.20.0/24 with the web server 192.168.20.10. The addresses are private (RFC 1918) and serve only as an example. The server must be reachable on TCP ports 80 and 443 from the LAN, from the office VLAN and from the Internet, through port forwarding.
Without aliases, six entries are needed: two rules on the LAN interface (port 80 and port 443), two on the VLAN 10 interface and two port forwards on the WAN. The address 192.168.20.10 appears six times. If the server is replaced and gets the address 192.168.20.12, six entries have to be corrected. In a real company, with more servers and more networks, they become dozens, and whoever reads the list has to remember what each number stands for.
How a single rule is read, with interface, source, destination, port and action, is explained in the article on how a firewall rule works. Here we focus on writing fewer, clearer rules.
What an alias is
An alias is a name that represents a set: one or more addresses, networks or ports. Instead of 192.168.20.10 the rule uses SERVER_WEB; instead of “80 and 443” it uses PORTE_WEB. The contents of the alias are written in one place, and every rule that refers to it uses them.
In OPNsense aliases are managed under Firewall › Aliases; menus may vary with the version, so it is worth checking the documentation for the installed release. Each alias has a few essential fields:
- Name: the name used in the rules. Only letters, digits and underscores are allowed, no spaces, with a maximum length of 31 characters.
- Type: the kind of content, which we look at shortly.
- Content: the entries, one per line or separated in the dedicated field.
- Description: a sentence explaining what the alias is for. It is not mandatory, but it is invaluable to whoever reads the configuration a year from now.
After saving, you press Apply: the firewall loads the new contents. In the drop-down lists of the rules, in the source, destination and port fields, aliases appear next to the interface networks. OPNsense also has some predefined aliases, for example bogons (addresses that should never appear on the Internet) and sshlockout (addresses blocked after too many failed login attempts).
The main types
The type determines what the alias may contain and where it can be used: a port alias cannot go in an address field, and vice versa.
| Type | Content | Example |
|---|---|---|
| Host(s) | One or more single IP addresses, or domain names (FQDN) that the firewall resolves at regular intervals | 192.168.20.10, www.example.com |
| Network(s) | Networks in CIDR notation, also mixed with single addresses | 192.168.1.0/24, 192.168.10.0/24 |
| Port(s) | Single ports or ranges | 80, 443, 8000:8080 |
| URL (IPs) | A list of addresses downloaded from a web address | a list published by a trusted source |
| URL Table (IPs) | Like the previous one, but downloaded again periodically at the configured frequency | lists of malicious networks updated daily |
| GeoIP | Networks assigned to one or more countries, taken from a geolocation database | the countries where the business has no customers |
Other types exist too, for example for MAC addresses or for contents managed by external programs: the list depends on the version.
Three details matter.
- Nested aliases. An alias can contain other aliases of the same kind. SERVER_DMZ, for example, can contain SERVER_WEB and SERVER_POSTA: when a mail server is added, the rule that uses SERVER_DMZ includes it straight away.
- URL and URL Table. The contents come from an external site. The firewall trusts that list: a careless or compromised source can insert addresses that have nothing to do with it, even your own networks. For very long lists you may need to raise the maximum number of table entries in the firewall's advanced settings.
- GeoIP. It needs a geolocation database to download and keep up to date, configured in the GeoIP settings tab of the aliases. A widely used database is MaxMind's GeoLite2, which requires a free registration and a licence key. The mapping between addresses and countries is approximate and changes over time; anyone using a VPN or a cloud server appears to be in another country.
A practical example
Back to the company from the beginning. Under Firewall › Aliases we create three aliases.
| Name | Type | Content | Description |
|---|---|---|---|
| SERVER_WEB | Host(s) | 192.168.20.10 | Company web server in the DMZ |
| PORTE_WEB | Port(s) | 80, 443 | HTTP and HTTPS |
| RETI_INTERNE | Network(s) | 192.168.1.0/24, 192.168.10.0/24 | LAN and office VLAN 10 |
The names are those of the example (in Italian: web ports and internal networks); any clear, consistent naming scheme works. Then we rewrite the entries.
- Under Firewall › Rules › LAN: Pass, protocol TCP, source LAN net, destination SERVER_WEB, destination port PORTE_WEB.
- Under Firewall › Rules on the VLAN 10 interface: the same rule, with the VLAN network as source.
- Under Firewall › NAT › Port Forward, on the WAN interface: protocol TCP, destination WAN address, destination port PORTE_WEB, Redirect target IP SERVER_WEB and, as redirect target port, the same alias PORTE_WEB. With the associated filter rule, the firewall lets the translated traffic through. In recent versions the name and position of this page may change.
- On the DMZ interface, to protect the internal networks, a Block rule with source DMZ net and destination RETI_INTERNE, placed before the rules that allow everything else.
The six original entries have become three, plus the DMZ protection rule, and the server address appears in one place only. How port forwarding and the DMZ work is covered in the articles on NAT and port forwarding and on the DMZ.
When the address changes
The web server is replaced and the new one has the address 192.168.20.12. Without aliases you would have to open six entries, correct them one by one and hope not to miss any. With aliases you open SERVER_WEB, type the new address, save and press Apply. The three entries do not change: they still say “to SERVER_WEB”, and it is the contents of the alias that now read 192.168.20.12.
Changing an alias, however, applies to every rule that uses it, including the ones we no longer remember. Before changing an alias used in many places, it is worth checking where it appears, by searching for its name in the rules, and taking a backup of the configuration.
Clear names and descriptions
An alias is only useful if its name can be understood without opening it. A few conventions help:
- names that say what they contain: SERVER_WEB, OFFICE_PRINTERS, MAIL_PORTS, not ALIAS1 or TEST;
- a consistent style, for example all capitals with underscores, perhaps with a prefix for the type, such as H_ for hosts, N_ for networks and P_ for ports;
- a description that is always filled in, with the purpose and, where useful, a reference to a request or an internal document;
- small, focused aliases: an alias that contains “a bit of everything” makes rules as ambiguous as addresses typed by hand.
A well-written rule reads like a sentence: “from the office network to SERVER_WEB on PORTE_WEB: allow”. Whoever reviews it understands the intention without having to work out from memory what each number means.
Under the bonnet: pf tables
OPNsense uses the pf packet filter, born in OpenBSD and ported to FreeBSD. In pf a table is a named structure holding addresses and networks, designed for fast lookups even when there are thousands of entries. Address aliases (hosts, networks, URL Tables, GeoIP) become tables with the same name; the generated rules do not contain the addresses, but a reference to the table. Port aliases, on the other hand, are not tables: the firewall writes the list of ports into the rules it generates. This is why, when the contents of an address alias change, the firewall updates the table and the rules stay exactly the same.
A simplified excerpt of the generated rules, with an example interface name, shows the idea; the exact form depends on the version:
table <SERVER_WEB> persist
table <RETI_INTERNE> persist
pass in quick on igb1 inet proto tcp from 192.168.1.0/24 to <SERVER_WEB> port { 80 443 }From the firewall shell, the contents of a table can be read with pfctl:
# pfctl -t SERVER_WEB -T show
192.168.20.12
# pfctl -t RETI_INTERNE -T show
192.168.1.0/24
192.168.10.0/24Without using the shell, the same check can be made in the web interface, under Firewall › Diagnostics › Aliases, where you pick the alias and see its actual contents. It is the most useful check for aliases that fill themselves: an FQDN, a URL Table or GeoIP.
Benefits for maintenance and audits
- Fewer rules. A port or host alias replaces several nearly identical entries: the list gets shorter and easier to read.
- A single point of change. A new address, a new network or an extra port is handled in the alias; the risk of forgetting a rule disappears.
- Simpler audits. Whoever reviews the configuration reads meaningful names, checks the contents of the aliases in one place and compares intentions with rules.
- A readable history. In the configuration change history, a change to an alias is a single difference, easy to recognise. Saving and comparing versions is covered in the article on OPNsense backups and updates.
- External lists always current. With URL Tables and GeoIP the contents update themselves, without touching the rules.
Common mistakes
- Ambiguous names. ALIAS1, SERVER or NEW say nothing; two similar aliases, such as SERVER_WEB and SERVERS_WEB, are easily confused. The name should describe the contents, and the description the purpose.
- Empty or unresolved alias. An alias with no entries, an FQDN that DNS cannot resolve or a URL Table that fails to download produce an empty table. A Pass rule with that alias lets nothing through; a Block rule blocks nothing, and the second case is the more dangerous one, because nobody notices. The actual contents are checked under Firewall › Diagnostics › Aliases.
- Changing an alias without knowing where it is used. Adding a network to an alias used in a permissive rule opens access to that network too, in every rule that refers to it.
- GeoIP as the only defence. Blocking some countries reduces the noise of automated attempts, but it does not stop anyone using a VPN, a cloud server or a compromised device in an allowed country. An exposed service must be protected anyway, with updates, strong authentication and minimal rules.
- URL Tables from untrusted sources. A list downloaded from any website may contain errors, legitimate networks or even your own networks, blocking services you need. Choose well-known sources, download over HTTPS, and check the contents after the first load and periodically.
- Forgetting Apply. An alias that has been saved but not applied is not active yet: the rule keeps using the previous contents.
Practical conclusions
- Look in the rules for addresses and ports that repeat: they are the first candidates to become aliases.
- Create small, focused aliases under Firewall › Aliases, with clear names, a consistent style and a description that is always filled in.
- Rewrite rules and port forwards with aliases, one entry at a time, and check that traffic works as before.
- Before changing an alias, check where it is used and back up the configuration; after the change, press Apply and check the contents under Firewall › Diagnostics › Aliases.
- Use GeoIP and URL Tables as an extra layer, with trusted sources, never as the only protection.
Readers who want to go deeper into firewall rules, NAT and network organisation with OPNsense will find these topics developed step by step in the Academy's books and video courses on OPNsense and LAN security.
The video
Further reading
- Book: OPNsense® in Practice — Volume 1: The Foundations of a Professional Journey (English edition)
- Book: LOCAL AREA NETWORKS (LAN) — Volume 5: LAN Security and Integration (English edition)
- Book: LOCAL AREA NETWORKS (LAN) — Volume 4: Routing, NAT, and Internet Access (English edition)
- Video course: Capire i Firewall con OPNsense (video course in Italian)
- Video course: VLAN con OPNsense®: progettazione e configurazione (video course in Italian)
- Related article: How a Firewall Rule Works
- Related article: NAT and Port Forwarding: How a Private Network Talks to the Internet
- Related article: OPNsense: Configuration Backups and Risk-Free Updates
- Specialization: Firewalls & Network Security
Technical sources
- Documentazione ufficiale di OPNsense — Aliases (Firewall › Aliases) — Alias types (Host(s), Network(s), Port(s), URL (IPs), URL Table (IPs), GeoIP and others), nested aliases, periodic name resolution, GeoIP settings, checking contents under Firewall › Diagnostics › Aliases; menus and options depend on the version.
- Documentazione ufficiale di OPNsense — Rules e NAT (Port Forward) — Using aliases in the source, destination and port fields of rules and in port forwarding, with the associated filter rule.
- OpenBSD/FreeBSD — pagine di manuale pf.conf(5) e pfctl(8) — pf tables: named structures for addresses and networks, fast lookups, the persist option; reading the contents with pfctl -t <name> -T show.
- MaxMind — GeoLite2 — Free IP geolocation database, with registration and a licence key; approximate accuracy.
- RFC 1918 e RFC 5737 — Addresses used in the examples: the private networks 192.168.1.0/24, 192.168.10.0/24 and 192.168.20.0/24; 203.0.113.0/24 and 198.51.100.0/24 reserved for documentation.
Standards and official documentation available from the respective organisations.