How-To Alpine Wall: Difference between revisions

From Alpine Linux
(add shorewall sample dnat config)
(added wikilink and simplified introduction)
 
(36 intermediate revisions by 8 users not shown)
Line 1: Line 1:
{{Draft}}
This page shows how to configure Alpine Wall(awall) as compared to {{pkg|Shorewall}} firewall. This comparison is meant to help readers who are familiar with Shorewall learn AWall. Awall users should follow the AWall configuration examples. Use the Shorewall examples as contextual references, or ignore them, if unfamiliar with Shorewall.


Purpose of this doc is to illustrate Alpine Wall (AWall) by examples. Please see [[Alpine_Wall_User's_Guide]] for details about the syntax. We will explain AWall from the viewpoint of a Shorewall user. AWall is available since Alpine v2.4.
[[Zero-To-Awall]] howto page is meant for users with no firewall experience and [https://git.alpinelinux.org/awall/about/ Alpine Wall User Guide] is the official source for details about the syntax.  


Your firewall configuration goes to '''/usr/share/awall/optional'''. Each file is called ''Policy''. ''Policy'' files are not equivalent to /etc/shorewall/policy file. An AWall ''Policy'' contains definitions of variables (like /etc/shorewall/params), zones (like /etc/shorewall/zones), interfaces (like /etc/shorewall/interfaces), policies (like /etc/shorewall/policy), filters and NAT rules (like /etc/shorewall/rules). You may have multiple ''Policy'' files. This is useful, for example, for specific firewall roles, such as FTP, HTTP, etc. You can create separated ''policies''  that can be enabled or disabled on the fly with the "awall [enable|disable]" command.
== Installation ==
Install the {{pkg|awall}} package by running the following command:{{cmd|# apk add iptables awall}}


==Prerequisites==
Note that awall requires {{pkg|iptables}} but works with both backends [[nftables]] and legacy [[iptables]]. It does not interact with nftables directly.


After installing awall package, if this is the first time that you configure iptables on your machine, you need to load the following iptables modules:
== Configuration ==


  # modprobe ip_tables
The easier method for performing initial steps for running awall: {{cmd|# awall activate}}
  # modprobe iptable_nat    #if NAT is used
The above command has special handling for the first run, when firewall is not yet enabled in the kernel. It performs the  below manual steps and also updates the default runlevel and files in {{Path|/etc/conf.d}}


Make the firewall to autostart at boot and autoload the needed modules:
Use the below commands for performing initial setup manually.
* To update /etc/iptables: {{cmd|# awall translate}}
* To start the [[OpenRC]] services for iptables and load modules and rules for IPv4 and IPv6 {{cmd|<nowiki># rc-service iptables start 
# rc-service ip6tables start </nowiki>}}


  # rc-update add iptables
=== Configuration files ===


==A Basic Home Firewall==
Your [[Alpine Wall]] configuration files go in {{Path|/etc/awall/optional}}. From version 0.2.12 and later, Awall will look for ''Policy'' files in both the former and {{Path|/usr/share/awall/optional}}
In this case you just have a "local" zone and an "internet" zone, and the Alpine router firewall you from internet.
Each such file is called a ''Policy''.<br>


Let's suppose you have the following Shorewall configuration:
You may have multiple ''Policy'' files. It is useful to have separate files for eg. HTTP, FTP, etc.
The ''Policy(s)'' can be enabled or disabled by using the command: {{cmd|<nowiki># awall [enable|disable]</nowiki>}}


/etc/shorewall/zones:
An AWall ''Policy'' can contain definitions of:
* variables ''(like {{Path|/etc/shorewall/params}})''
* zones ''(like {{Path|/etc/shorewall/zones}})''
* interfaces ''(like {{Path|/etc/shorewall/interfaces}})''
* policies ''(like {{Path|/etc/shorewall/policy}})''
* filters and NAT rules ''(like {{Path|/etc/shorewall/rules}})''
* services ''(like {{Path|/usr/share/shorewall/macro.HTTP}})''


  inet  ipv4
== Basic home firewall configuration ==
  loc  ipv4


/etc/shorewall/interfaces:
The examples below show how to configure a basic home firewall using AWall. Note that AWall Policy files differ substantially from Shorewall's policy files in syntax and layout.


  inet  eth0
=== Shorewall configuration ===
  loc  eth1


/etc/shorewall/policy:
Let's suppose you have the following Shorewall configuration files in {{Path|/etc/shorewall/}}, which you want to convert to a [[#AWall configuration|configuration for AWall]].  {{cat|/etc/shorewall/zones|inet  ipv4
loc  ipv4}}


  fw  all ACCEPT
{{cat|/etc/shorewall/interfaces|inet eth0
  loc inet ACCEPT
loc  eth1}}
   all  all  DROP


/etc/shorewall/masq:
{{cat|/etc/shorewall/policy|fw  all  ACCEPT
loc  inet ACCEPT
all  all  DROP}}


  eth0  0.0.0.0/0
{{cat|/etc/shorewall/masq|eth0  0.0.0.0/0}}


You can convert this configuration to AWall doing the following:
=== AWall configuration ===


Open a blank file from /usr/share/awall/optional and start with a description of your ''Policy'' (useful when you have multiple policies) and the define the zones:
The equivalent AWall configuration that does the same thing as the above [[#Shorewall configuration|Shorewall example]] is given below.


  {
Create a new file called {{Path|/etc/awall/optional/home-policy.json}} and add the following content {{Cat|/etc/awall/optional/home-policy.json|<nowiki>
    "description": "Home firewall"
{
  "description": "Home firewall",


    "zone": {
  "zone": {
      "inet": { "iface": "eth0" },
    "inet": { "iface": "eth0" },
      "loc": { "iface": "eth1" }
    "loc": { "iface": "eth1" }
     },
  },
 
  "policy": [
    { "in": "_fw", "action": "accept" },
     { "in": "loc", "out": "inet", "action": "accept" }
  ],
 
  "snat": [
    { "out": "inet" }
  ]
}</nowiki>}}
 
The above configuration will:
* Create a description of your ''Policy''
* Define ''zones''
* Define ''policy''
* Define ''snat'' ''(to masqurade the outgoing traffic)''
 
'''snat''' means "source NAT". It does <u>not</u> mean "static NAT".
 
{{Tip| AWall has a built-in zone named "_fw" which is the "firewall itself". This corresponds to the Shorewall "fw" zone.}}


AWall has a default zone built-in '''_fw''', that corresponds to the Shorewall "fw" zone, the firewall itself. Setup your default polices:
=== Activating/Applying a Policy ===


    "policy": [
After saving the ''Policy'' you can run the following commands to activate your firewall settings.
      { "in": "_fw", "action": "accept" },
To listing available 'Policy(s)' {{cmd|# awall list}}
      { "in": "loc", "out": "inet", "action": "accept" }
To enable the 'Policy' created in the previous section:{{cmd|# awall enable home-policy}}  
    ],
To generate firewall configuration from the 'Policy' file and enable it i.e start the firewall: {{Cmd|# awall activate}}            


Then you need to masquerade the outgoing traffic:
If you have multiple policies, after enabling or disabling them, you need to always run {{Codeline|'''awall activate'''}} in order to update the iptables rules.


    "snat": [
== Advanced configuration ==
      { "out": "inet", "action": "masquerade" }
    ]
  }


'''snat''' here has to be intended as "source NAT" and not "static NAT".
Assuming you have your {{Path|/etc/awall/optional/home-policy.json}} with your "Basic home firewall" settings, you could choose to modify that file to test the below examples. You could also create new files in {{Path|/etc/awall/optional/}} for testing some of the below examples.


After saving the ''Policy'', you can list it, enable/disable it and activate it (that is start the firewall):
AWall already has a "service" definition list for several services like HTTP, FTP, SNMP, etc. '' in the file {{Path|/usr/share/awall/mandatory/services.json}})''


  # awall list
{{Note|If you are adding the sample content given in this section to an already existing policy file, then make sure you add "," signs where they are needed!}}
  myfirewall  enabled  Home firewall
  # awall activate
  Warning: inet6 rules not tested
  New firewall configuration activated
  Press RETURN to commit changes permanently:


If I want to log all dropped packets from "inet", I can add the following policy:
=== Logging ===


  { "in": "inet", "out": "loc", "action": "logdrop" }
AWall will ''(since v0.2.7)'' automatically log dropped packets.


==Port-Forwarding==
You could add the following row to the "policy" section in your ''Policy'' file in order to see the dropped packets.
Let's suppose you have a local web server (192.168.1.10) that you want to make accessible from the "inet". With Shorewall you would have a rule like this:
<pre>{ "in": "inet", "out": "loc", "action": "drop" }</pre>


  #ACTION  SOURCE  DEST              PROTO  DEST    SOURCE    ORIGINAL
=== Port forwarding ===
  #                                          PORT(S) PORT(S)  DEST
  DNAT    inet    loc:192.168.1.10  tcp    80


AWall already has a "service" definition list for several services (in /usr/share/awall/mandatory/services.json), like HTTP, FTP, SNMP, etc. So, in order to port-forward the HTTP port to your "loc" zone, you could add a "variables" block with your IP Addresses, and then a "filter" definition:
Let's suppose you have a local web server (192.168.1.10) that you want to make accessible from the "inet".<br>
With Shorewall you would have a rule like this in your {{Cat|/etc/shorewall/rules|<nowiki>#ACTION  SOURCE  DEST              PROTO  DEST PORT(S)    SOURCEPORT(S)    ORIGINALDEST
DNAT    inet    loc:192.168.1.10  tcp    80</nowiki>}}


Lets configure our AWall ''Policy'' file likewise by adding the following content.
<pre>
   "variable": {
   "variable": {
     "APACHE": "192.168.1.10",
     "APACHE": "192.168.1.10",
     "STATIC_IP": "1.2.3.4"
     "STATIC_IP": "1.2.3.4"
  },
    },


   "filter": [
   "filter": [
Line 103: Line 130:
       "dnat": "$APACHE"  
       "dnat": "$APACHE"  
       }
       }
   ]
    ]
</pre>
As you can see in the above example, we create a
* "variable" section where we specify some IP-addresses
* "filter" section where we do the actual port-forwarding (using the variables we just created and using some preexisting "services" definitions)
 
If you need to forward to a different port (e.g. 8080) you can do:
 
<pre>
"dnat": [
   {"in": "inet", "dest": "$STATIC_IP", "to-addr": "$APACHE", "service": "http", "to-port": 8080 }
]
</pre>
 
=== Create your own service definitions ===


==More Stuff==
{{Note| You can not override a "service" definition that comes from {{Path|/usr/share/awall/mandatory/services.json}}}}


You can add your own service definitions into your ''Policy'' files:
You can add your own service definitions into your ''Policy'' files:
<pre>
"service": { 
  "openvpn": { "proto": "udp", "port": 1194 }
  }
</pre>


  "service":
=== Inherit services or variables ===
    "openvpn": { "proto": "udp", "port": "1194" }
 
  }
You can import a ''Policy'' into other ''Policy'' files for inheriting services or variables definitions:
<pre>
"import": "myfirewall"
</pre>
 
=== Customize policy loading order ===


Or you can import a ''Policy'' into other ''Policy'' files, for inheriting services or variables definitions:
By default policies are loaded on alphabetical order. The load order can be changed with the keywords "before" and "after":
<pre>
"before": "myfirewall"
"after": "someotherpolicy"
</pre>


  "import": "myfirewall"
== Troubleshooting ==


By default policies are loaded on alphabetical order. You can change the load order with the keywords "before" and "after":
If you end up in some kind of trouble, you might find some commands useful when debugging:
{{cmd|awall                # (With no parameters) Shows some basic help about awall application
awall dump            # Dump definitions like zones and variables
iptables -L -n        # Show what's in <code>iptables</code>}}


  "before": "myfirewall"
== See also ==
  "after": "someotherpolicy"


* [https://git.alpinelinux.org/awall/about/ Alpine Wall User Guide]
* [[Zero-To-Awall]]
* [https://www.cyberciti.biz/faq/how-to-set-up-a-firewall-with-awall-on-alpine-linux/ How To Set Up a Firewall with Awall on Alpine Linux]


[[Category:Networking]]
[[Category:Firewall]]
[[Category:Security]]

Latest revision as of 16:56, 24 January 2026

This page shows how to configure Alpine Wall(awall) as compared to Shorewall firewall. This comparison is meant to help readers who are familiar with Shorewall learn AWall. Awall users should follow the AWall configuration examples. Use the Shorewall examples as contextual references, or ignore them, if unfamiliar with Shorewall.

Zero-To-Awall howto page is meant for users with no firewall experience and Alpine Wall User Guide is the official source for details about the syntax.

Installation

Install the awall package by running the following command:

# apk add iptables awall

Note that awall requires iptables but works with both backends nftables and legacy iptables. It does not interact with nftables directly.

Configuration

The easier method for performing initial steps for running awall:

# awall activate

The above command has special handling for the first run, when firewall is not yet enabled in the kernel. It performs the below manual steps and also updates the default runlevel and files in /etc/conf.d

Use the below commands for performing initial setup manually.

  • To update /etc/iptables:

    # awall translate

  • To start the OpenRC services for iptables and load modules and rules for IPv4 and IPv6

    # rc-service iptables start # rc-service ip6tables start

Configuration files

Your Alpine Wall configuration files go in /etc/awall/optional. From version 0.2.12 and later, Awall will look for Policy files in both the former and /usr/share/awall/optional Each such file is called a Policy.

You may have multiple Policy files. It is useful to have separate files for eg. HTTP, FTP, etc.

The Policy(s) can be enabled or disabled by using the command:

# awall [enable|disable]

An AWall Policy can contain definitions of:

  • variables (like /etc/shorewall/params)
  • zones (like /etc/shorewall/zones)
  • interfaces (like /etc/shorewall/interfaces)
  • policies (like /etc/shorewall/policy)
  • filters and NAT rules (like /etc/shorewall/rules)
  • services (like /usr/share/shorewall/macro.HTTP)

Basic home firewall configuration

The examples below show how to configure a basic home firewall using AWall. Note that AWall Policy files differ substantially from Shorewall's policy files in syntax and layout.

Shorewall configuration

Let's suppose you have the following Shorewall configuration files in /etc/shorewall/, which you want to convert to a configuration for AWall.

Contents of /etc/shorewall/zones

inet ipv4 loc ipv4

Contents of /etc/shorewall/interfaces

inet eth0 loc eth1

Contents of /etc/shorewall/policy

fw all ACCEPT loc inet ACCEPT all all DROP

Contents of /etc/shorewall/masq

eth0 0.0.0.0/0

AWall configuration

The equivalent AWall configuration that does the same thing as the above Shorewall example is given below.

Create a new file called /etc/awall/optional/home-policy.json and add the following content

Contents of /etc/awall/optional/home-policy.json

{ "description": "Home firewall", "zone": { "inet": { "iface": "eth0" }, "loc": { "iface": "eth1" } }, "policy": [ { "in": "_fw", "action": "accept" }, { "in": "loc", "out": "inet", "action": "accept" } ], "snat": [ { "out": "inet" } ] }

The above configuration will:

  • Create a description of your Policy
  • Define zones
  • Define policy
  • Define snat (to masqurade the outgoing traffic)

snat means "source NAT". It does not mean "static NAT".

Tip: AWall has a built-in zone named "_fw" which is the "firewall itself". This corresponds to the Shorewall "fw" zone.

Activating/Applying a Policy

After saving the Policy you can run the following commands to activate your firewall settings.

To listing available 'Policy(s)'

# awall list

To enable the 'Policy' created in the previous section:

# awall enable home-policy

To generate firewall configuration from the 'Policy' file and enable it i.e start the firewall:

# awall activate

If you have multiple policies, after enabling or disabling them, you need to always run awall activate in order to update the iptables rules.

Advanced configuration

Assuming you have your /etc/awall/optional/home-policy.json with your "Basic home firewall" settings, you could choose to modify that file to test the below examples. You could also create new files in /etc/awall/optional/ for testing some of the below examples.

AWall already has a "service" definition list for several services like HTTP, FTP, SNMP, etc. in the file /usr/share/awall/mandatory/services.json)

Note: If you are adding the sample content given in this section to an already existing policy file, then make sure you add "," signs where they are needed!

Logging

AWall will (since v0.2.7) automatically log dropped packets.

You could add the following row to the "policy" section in your Policy file in order to see the dropped packets.

{ "in": "inet", "out": "loc", "action": "drop" }

Port forwarding

Let's suppose you have a local web server (192.168.1.10) that you want to make accessible from the "inet".

With Shorewall you would have a rule like this in your

Contents of /etc/shorewall/rules

#ACTION SOURCE DEST PROTO DEST PORT(S) SOURCEPORT(S) ORIGINALDEST DNAT inet loc:192.168.1.10 tcp 80

Lets configure our AWall Policy file likewise by adding the following content.

  "variable": {
    "APACHE": "192.168.1.10",
    "STATIC_IP": "1.2.3.4"
    },

  "filter": [
    { "in": "inet", 
      "dest": "$STATIC_IP", 
      "service": "http", 
      "action": "accept", 
      "dnat": "$APACHE" 
      }
    ]

As you can see in the above example, we create a

  • "variable" section where we specify some IP-addresses
  • "filter" section where we do the actual port-forwarding (using the variables we just created and using some preexisting "services" definitions)

If you need to forward to a different port (e.g. 8080) you can do:

"dnat": [
  {"in": "inet", "dest": "$STATIC_IP", "to-addr": "$APACHE", "service": "http", "to-port": 8080 }
]

Create your own service definitions

Note: You can not override a "service" definition that comes from /usr/share/awall/mandatory/services.json

You can add your own service definitions into your Policy files:

"service": {  
  "openvpn": { "proto": "udp", "port": 1194 }
  }

Inherit services or variables

You can import a Policy into other Policy files for inheriting services or variables definitions:

"import": "myfirewall"

Customize policy loading order

By default policies are loaded on alphabetical order. The load order can be changed with the keywords "before" and "after":

"before": "myfirewall"
"after": "someotherpolicy"

Troubleshooting

If you end up in some kind of trouble, you might find some commands useful when debugging:

awall # (With no parameters) Shows some basic help about awall application awall dump # Dump definitions like zones and variables iptables -L -n # Show what's in iptables

See also