fwdctl has many commands to manage your forwards. Let's dive into them!
The apply command is used to apply rules from a rules file. Here's a rules file example.
cat << EOF > rules.yml
rules:
- dport: 2022
saddr: 192.168.122.43
sport: 22
iface: eth1
proto: tcp
- dport: 3022
saddr: 192.168.122.44
sport: 22
iface: eth1
proto: tcp
EOFOnce created, you can easily run:
sudo fwdctl apply --file rules.ymland apply all the rules listed in the file.
Each rule can define optional hooks: commands executed after the corresponding forward is created or deleted. This is useful to run side effects such as notifying a service, updating DNS, or logging.
rules:
- dport: 2022
saddr: 192.168.122.43
sport: 22
iface: eth1
proto: tcp
hooks:
on-create: "/path/to/on-create-script.sh"
on-delete: "/path/to/on-delete-script.sh"Details:
- A hook command is run through
sh -c, so it can include arguments, pipes and shell expansion. - The
on-createhook runs after the forward is applied; theon-deletehook runs after the forward is removed. A hook never prevents the forward change itself. - By default, if a hook exits non-zero the program exits non-zero as well
(for
apply/delete, on the first failing hook). Pass--continue-on-hook-failto log the failure and keep going instead. - Hooks are only available where the rules file provides context:
fwdctl apply,fwdctl delete --file, and thedaemon. They are not run byfwdctl create,fwdctl delete --id, orfwdctl delete --all, since those paths have no access to a rule's hooks. - In
daemonmode theon-createhook fires when a rule is applied at startup or added via a config change, and theon-deletehook fires when a rule is removed via a config change or during shutdown. Without--continue-on-hook-fail, a failing hook makes the daemon tear down its forwards and exit non-zero.
Hooks receive the forward's context through the following environment variables, so a single script can be reused across rules:
| Variable | Description |
|---|---|
FWDCTL_IFACE |
Interface the forward listens on |
FWDCTL_PROTO |
Protocol (tcp/udp) |
FWDCTL_DPORT |
Destination (listening) port |
FWDCTL_SADDR |
Source (target) address |
FWDCTL_SPORT |
Source (target) port |
Example on-create script:
#!/usr/bin/env bash
# This script is executed when a rule is created.
touch /tmp/created.txt
echo "fwdctl rule $FWDCTL_DPORT <- $FWDCTL_SADDR:$FWDCTL_SPORT created at $(date)" >> /tmp/created.txtThe create command is used to create single rules manually. Let's see an example.
To implement the rule for this scenario, just type the following command:
sudo fwdctl create --destination-port 3000 --source-address 192.168.199.105 --source-port 80The daemon command is used to run fwdctl as service.
When modifications are applied to the rules file, fwdctl is triggered and start to apply changes that have been requested.
To start the daemon, run the following command:
sudo fwdctl daemon start -f rules.yamlWhen you want to stop the daemon execution, then type:
sudo fwdctl daemon stopThis will remove all the forwards that have been applied during its execution.
Here's a demo on how to use this command:
The delete command is used to delete rules using their ID or a file where the rules are listed.
To delete a specific rule (identified with a number), type the following command:
sudo fwdctl delete --id 4This will remove the rule n. 4, listed from iptables.
To delete a set of rules listed in a rule file, type the following command:
sudo fwdctl delete --file rules.yamlThis will remove the listed rules within the file.
The list command is used to list the applied rules.
To list the rules, type the following command:
sudo fwdctl listThe output will look like this:
+--------+-----------+----------+---------------+----------------+---------------+
| NUMBER | INTERFACE | PROTOCOL | EXTERNAL PORT | INTERNAL IP | INTERNAL PORT |
+--------+-----------+----------+---------------+----------------+---------------+
| 1 | lo | tcp | 2022 | 192.168.122.43 | 22 |
| 2 | lo | tcp | 3022 | 192.168.122.44 | 443 |
+--------+-----------+----------+---------------+----------------+---------------+If you want to use a different view of applied rules, you can choose between different format:
sudo fwdctl list --output jsonThe generate command is used to generate the following files:
- systemd service for
fwdctl - rules empty file
To generate a systemd service, type the following command:
fwdctl generate systemd -o fwdctl.serviceTo generate a fwdctl rules file, instead, type the following command:
fwdctl generate rules -o rules.ymlThe version command is used show the version of the program.
