You can quickly tell whether iptables or nftables is in use by looking at the output of iptables -V. Not 100% optimal as it still requires a little output parsing, but quite straight forward.
Redhat has a blog post from 2020-08 with guidance on this that also applies to Ubuntu.
The two variants of the iptables command are:
legacy: Often referred to as iptables-legacy. nf_tables: Often referred to as iptables-nft.
The newer iptables-nft command provides a bridge to the nftables kernel API and infrastructure.
You can find out which variant is in use by looking up the iptables version.
Ubuntu 22.04 (nftables)
For iptables-nft, the variant will be shown in parentheses after the version number, denoted as nf_tables:
# iptables -V iptables v1.8.7 (nf_tables)
Ubuntu 20.04 (iptables)
For iptables-legacy, the variant will either be absent, or it will show legacy in parentheses:
# iptables -V iptables v1.8.4 (legacy)
Testing
So a simple test with grep to determine if we are nftables or not would be:
iptables -V | grep -E ' \(nf_tables\) *$'
...it is possible in the future that iptables is no longer distributed by default and so a more future-proof test would look something like:
if command -v iptables; then if iptables -V | grep -E ' \(nf_tables\) *$'; then echo "nft" else echo "iptables" fi elif command -v nft; then echo "nft"; fi