Managing the firewall on your VM
Introduction
Some VM's provided by the FSE-CSLAB infrastructure comes with a preconfigured firewall using nftables. The firewall is enabled by default and uses a restrictive configuration: incoming network connections are blocked unless explicitly allowed. You can manage the firewall on your VM yourself. This allows you to control which IP addresses or networks can access services running on your VM. For example, you can:
- Allow SSH access from specific IP addresses or networks.
- Allow HTTPS connections from the university network.
- Restrict access to a web application to a particular group of IP addresses.
- Define reusable groups of IP addresses to simplify firewall rules.
This document explains how to configure and manage the firewall.
Firewall configuration
Config files
The firewall configuration consists of three files:
- /etc/nftables.conf - Main configuration containing the default firewall policies.
- /etc/nftables.d/set.nft - Defines groups (sets) of IP addresses and networks.
- /etc/nftables.d/input-rules.nft - Contains the rules controlling incoming network connections.
Understanding the firewall chains
The main configuration defines three firewall chains:
- input: Controls incoming network traffic destined for your VM. This is where you normally add firewall rules to allow access to your services.
- output: Controls outgoing network traffic originating from your VM.
- forward: Controls network traffic passing through your VM to another destination. This is mainly relevant when your VM acts as a router.
The default policies are:
| Chain | Default policy | Explanation |
|---|---|---|
| input | drop | Incoming connections are blocked unless explicitly allowed. |
| output | accept | Outgoing connections are allowed. |
| forward | drop | Forwarded traffic is blocked. |
The input chain also contains some predefined rules:
- Invalid connection states are dropped.
- Established and related connections are accepted.
- Loopback traffic is allowed.
Traffic attempting to reach the loopback addresses from another network interface is blocked.
Creating IP address groups
You can define groups of IP addresses and networks in: /etc/nftables.d/set.nft Groups allow you to reuse the same collection of IP addresses in multiple firewall rules. For example, suppose you want to allow access from the Maastricht University network. You can define the following group:
set umnet {
type ipv4_addr;
flags interval;
elements = {
137.120.0.0/16,
145.127.0.0/17
};
}
Explanation:
- set umnet: Creates a set named umnet.
- type ipv4_addr: The set contains IPv4 addresses.
- flags interval: Allows you to specify IP ranges and CIDR networks.
- elements: Contains the IP addresses and networks belonging to the group.
You can then reference these groups in your firewall rules. Note: The IP addresses above are examples. Replace them with the actual addresses or networks you want to allow.
Creating firewall rules
Incoming firewall rules must be added to: /etc/nftables.d/input-rules.nft Each rule specifies which traffic is allowed or blocked.
For example, to allow HTTPS connections (TCP port 443) from the Maastricht University network, add:
# Allow HTTPS from Maastricht University
ip saddr @umnet tcp dport 443 accept
Explanation:
| Part | Meaning |
|---|---|
| ip saddr @umnet | The source IPv4 address must belong to the umnet set. |
| tcp | Only TCP traffic is matched. |
| dport 443 | The destination port must be 443. |
| accept | Allow the connection |
Rules are evaluated in order. Place specific blocking rules before broader rules that might accept the same traffic.
Important: The default policy of the input chain is drop. This means that any incoming traffic that does not match an accepting rule will automatically be blocked. You do not need to add explicit drop rules for everything else.
Applying firewall changes
Changes to the configuration files are not applied automatically. After modifying either set.nft or input-rules.nft, you must reload the firewall configuration. Before applying your changes, it is recommended to check whether the configuration contains syntax errors. Run:
sudo nft -c -f /etc/nftables.conf
The -c option checks the configuration without applying the changes. If there are no errors, you can proceed with reloading the firewall. Run:
sudo systemctl reload nftables
Alternatively, you can restart the service:
sudo systemctl restart nftables
A reload is normally sufficient after changing the configuration.
Warning: Incorrect firewall rules can prevent you from accessing your VM, especially if you accidentally block SSH. Keep your current SSH session open while testing changes. Make sure you have access to the VM console through Xen Orchestra in case you lose network connectivity.
View firewall logs
To inspect firewall-related messages:
journalctl | grep nftables
ov 05 15:08:41 dacsvm-st-webapp01 kernel: nftables: input drop: IN=enX0 OUT= MACSRC=58:30:6e:cd:f3:e0 MACDST=7a:e4:37:96:d8:f5 MACPROTO=0800 SRC=209.188.108.84 DST=145.98.64.69 LEN=48 TOS=0x18 PREC=0x00 TTL=48 ID=51396 PROTO=ICMP TYPE=8 CODE=0 ID=3254 SEQ=6738
Nov 05 15:27:55 dacsvm-st-webapp01 kernel: nftables: input drop: IN=enX0 OUT= MACSRC=58:30:6e:cd:f3:e0 MACDST=7a:e4:37:96:d8:f5 MACPROTO=0800 SRC=81.16.152.2 DST=145.98.64.69 LEN=40 TOS=0x18 PREC=0x00 TTL=53 ID=2013 PROTO=ICMP TYPE=8 CODE=0 ID=51747 SEQ=44895
Nov 05 15:29:05 dacsvm-st-webapp01 kernel: nftables: input drop: IN=enX0 OUT= MACSRC=58:30:6e:cd:f3:e0 MACDST=7a:e4:37:96:d8:f5 MACPROTO=0800 SRC=209.38.30.136 DST=145.98.64.69 LEN=40 TOS=0x18 PREC=0x00 TTL=43 ID=38050 PROTO=ICMP TYPE=8 CODE=0 ID=23073 SEQ=54457
Nov 05 15:42:16 dacsvm-st-webapp01 kernel: nftables: input drop: IN=enX0 OUT= MACSRC=58:30:6e:cd:f3:e0 MACDST=7a:e4:37:96:d8:f5 MACPROTO=0800 SRC=218.187.80.98 DST=145.98.64.69 LEN=40 TOS=0x18 PREC=0x00 TTL=49 ID=37095 DF PROTO=ICMP TYPE=8 CODE=0 ID=26135 SEQ=15056
Nov 05 16:06:50 dacsvm-st-webapp01 kernel: nftables: input drop: IN=enX0 OUT= MACSRC=58:30:6e:cd:f3:e0 MACDST=7a:e4:37:96:d8:f5 MACPROTO=0800 SRC=85.217.140.33 DST=145.98.64.69 LEN=48 TOS=0x18 PREC=0x00 TTL=50 ID=34658 PROTO=ICMP TYPE=8 CODE=0 ID=7567 SEQ=22049
To follow the logs in real time:
journalctl -f | grep nftables
With CTRL + c you can interrupt the command.
Inspecting the active firewall rules
You can inspect the currently active firewall configuration using the nft command. To display the complete ruleset:
nft list ruleset
To display only the input chain:
nft list chain inet filter input
To display the configured address groups:
nft list sets
These commands are useful to verify whether your changes have been applied correctly.
Important considerations
- Please keep the following points in mind when configuring your firewall:
- Only allow the ports you need. Avoid opening unnecessary services to the network.
- Restrict access whenever possible. Prefer allowing connections from specific IP addresses or networks instead of allowing access from anywhere.
- Do not modify /etc/nftables.conf. Only modify the designated configuration files under /etc/nftables.d/.
- Validate your configuration before reloading. Use nft -c -f /etc/nftables.conf to detect syntax errors.
- Be careful with SSH rules. Make sure you do not accidentally block your own access to the VM.
- Remember IPv4 and IPv6. The examples in this document use IPv4. If your VM provides services over IPv6, you must also consider the corresponding IPv6 firewall rules.
- Keep the firewall enabled. Do not disable the nftables service.