sodcalculator

10 Tech How-To

Arch Linux Firewall: SSH, UFW Rules & Fail2ban

Guide 10-53744 · Updated · Step by step

Yes, you can use UFW (Uncomplicated Firewall) and Fail2ban on Arch Linux to protect SSH, but Arch does not enable a firewall by default. You must install, enable, and configure both tools yourself, and UFW is not the standard Arch firewall (that is iptables/nftables), but it works fine as a front-end. This guide covers the full setup, common pitfalls, and how to make SSH both reachable and secure.

Why Arch Linux Has No Default Firewall

Arch Linux follows a minimal, user-centric philosophy: the base install includes only what you explicitly choose. Unlike Ubuntu or Fedora, which ship with a preconfigured firewall (UFW or firewalld), Arch leaves networking completely open. This means after a fresh install, all ports are accessible from the network unless you lock them down yourself. For SSH, this is especially critical because the SSH daemon (sshd) listens on port 22 by default, and without a firewall, any attacker who can reach your IP can attempt to connect.

The Arch Wiki recommends using iptables or nftables directly, but many users prefer UFW for its simplicity. UFW is available in the official repositories (extra/ufw) and works as a wrapper around iptables. It is not a separate firewall engine; it generates iptables rules for you. This is perfectly acceptable for most setups, including home servers and VPS instances.

Installing and Enabling UFW on Arch

To install UFW, use pacman. Then enable and start the service. The key is to set default policies before enabling, otherwise you might lock yourself out if SSH is already running.

  1. Install UFW: sudo pacman -S ufw
  2. Set default policies to deny incoming and allow outgoing: sudo ufw default deny incoming and sudo ufw default allow outgoing
  3. Allow SSH before enabling: sudo ufw allow ssh (or sudo ufw allow 22/tcp if you use a custom port)
  4. Enable UFW: sudo systemctl enable --now ufw and then sudo ufw enable
  5. Check status: sudo ufw status verbose – you should see the SSH rule and the default policies.

If you skip step 2 or 3, enabling UFW will block your current SSH session. If that happens, you can recover by using a console (physical or via a VPS web terminal) and running sudo ufw allow ssh then sudo ufw reload.

Configuring UFW Rules for SSH

UFW rules are straightforward. For SSH, you typically want to allow connections from specific IPs (your office, home) rather than from anywhere. This reduces brute-force exposure significantly. Here are common patterns:

  • Allow SSH from a single IP: sudo ufw allow from 203.0.113.5 to any port 22 proto tcp
  • Allow SSH from a subnet: sudo ufw allow from 192.168.1.0/24 to any port 22 proto tcp
  • Allow SSH on a non-standard port: change Port 2222 in /etc/ssh/sshd_config, then use sudo ufw allow 2222/tcp
  • If you use IPv6, ensure UFW is configured for it (it is by default, but check /etc/default/ufw for IPV6=yes)

Remember to reload UFW after changes: sudo ufw reload. Also note that UFW rules are applied in order, and the first match wins. The default deny incoming rule is last, so your allow rules take precedence.

Setting Up Fail2ban for SSH Protection

Fail2ban is a log-scanning daemon that bans IPs after repeated failed login attempts. It works with any firewall that can block IPs, including UFW. Install it with sudo pacman -S fail2ban. The Arch package includes a default configuration, but you should copy the jail.conf to jail.local to avoid overwrites during updates.

  1. Copy the default config: sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
  2. Edit /etc/fail2ban/jail.local. Find the [sshd] section and enable it by setting enabled = true. You can also set maxretry = 5, bantime = 1h, and findtime = 10m.
  3. Set the ban action to use UFW: in the [DEFAULT] section, change banaction = ufw (it is often already set to ufw on Arch, but verify).
  4. Start and enable: sudo systemctl enable --now fail2ban
  5. Check status: sudo fail2ban-client status sshd – you should see the jail active.

Fail2ban reads logs from /var/log/auth.log (on Arch, this is symlinked to journald via systemd, but the package handles it). If you use a non-standard SSH port, you need to add port = 2222 in the [sshd] section of jail.local, otherwise it will still monitor port 22.

How UFW and Fail2ban Work Together

UFW is the firewall that filters traffic at the network level. Fail2ban is an intrusion prevention tool that watches logs and dynamically adds UFW rules to block offending IPs. When Fail2ban detects too many failed SSH logins (based on maxretry within findtime), it executes the ufw ban action, which inserts a DENY rule for that IP. The ban lasts for bantime seconds, after which the rule is removed automatically.

This two-layer approach is effective because UFW blocks all traffic from the banned IP, not just SSH. So even if the attacker tries other services (like a web server), they are blocked. However, Fail2ban only works if it can read the logs; ensure rsyslog or systemd-journald is running and logging SSH attempts. On Arch, journald is default, and Fail2ban uses a backend that reads from it.

Common Problems and Troubleshooting

Several issues arise when setting up UFW and Fail2ban on Arch. Here are the most frequent and how to solve them:

  • UFW fails to start with “Problem with iptables”: This usually means the iptables kernel modules are not loaded. Install iptables-nft (the default) and ensure the iptables service is not conflicting. Run sudo systemctl restart ufw after loading modules.
  • UFW status shows “inactive” after enable: Check sudo systemctl status ufw for errors. Often it is because the ufw service is not enabled, so run sudo systemctl enable ufw and then sudo ufw enable again.
  • Fail2ban does not ban: Check sudo fail2ban-client status sshd – if the jail is not listed, you forgot enabled = true. Also verify the log path: run sudo tail -f /var/log/auth.log and try an SSH login; if nothing appears, the log is elsewhere. On Arch, you may need to set backend = systemd in jail.local.
  • Locked out of SSH after enabling UFW: Use the console to run sudo ufw allow ssh immediately. To prevent this, always allow SSH before enabling, and test with a separate session.
  • UFW and Docker conflict: Docker manipulates iptables directly, which can bypass UFW. This is a known issue; you may need to configure Docker to use a separate bridge or adjust UFW’s DEFAULT_FORWARD_POLICY to ACCEPT.

UFW vs. Firewalld vs. nftables for SSH

Arch users often wonder whether to use UFW, firewalld, or raw nftables. UFW is the simplest for a single-service setup like SSH. Firewalld is more complex but supports zones and dynamic updates; it is overkill for most home servers. nftables is the modern successor to iptables and is the Arch default, but it requires writing rules manually, which is error-prone for beginners.

For SSH protection, UFW plus Fail2ban is a solid, widely documented combination. If you prefer a more Arch-native approach, you can use nftables directly with a simple table for SSH, but you will lose the convenience of UFW’s syntax. Firewalld is available in the repos but is not as integrated with Arch’s init system as UFW.

One misconception is that UFW is not “real” on Arch because it is not the default. This is false – UFW is just a front-end to iptables, and it works identically on any distribution. The Arch Wiki even mentions UFW as an alternative.

Security Best Practices for SSH Beyond Firewall

A firewall and Fail2ban are necessary but not sufficient for SSH security. You should also:

  • Disable password authentication and use SSH keys: set PasswordAuthentication no in /etc/ssh/sshd_config.
  • Disable root login: set PermitRootLogin no.
  • Change the SSH port (optional but reduces automated scans).
  • Install sshguard as an alternative to Fail2ban if you prefer a lighter daemon.
  • Keep your system updated: sudo pacman -Syu regularly.

Remember that Fail2ban is not a substitute for strong authentication; it only slows down brute-force attempts. Using key-based authentication is far more secure than relying on bans.

Frequently Asked Questions

Does Arch Linux enable a firewall by default? No. Arch does not enable any firewall by default. You must install and configure one manually, whether it is UFW, iptables, nftables, or firewalld.

Can I use UFW on Arch without installing iptables? No. UFW is a wrapper around iptables, so you need iptables (or iptables-nft) installed. Arch includes iptables-nft as part of the base, but verify with pacman -Q iptables.

Why is Fail2ban not banning IPs even though I see failed attempts? Check the jail status with sudo fail2ban-client status sshd. If the jail is active, look at the log path. On Arch, the auth log may be in /var/log/auth.log only if rsyslog is installed; otherwise, use journalctl -u sshd and set backend = systemd in jail.local.

Should I use UFW or nftables for SSH on Arch? For most users, UFW is easier and less error-prone. nftables gives finer control but requires learning its syntax. If you only need to allow SSH and block the rest, UFW is sufficient.

Can Fail2ban work with nftables instead of UFW? Yes. Fail2ban supports multiple ban actions, including nftables. In jail.local, set banaction = nftables and ensure the nftables module is loaded. The same logic applies.

How do I unban an IP manually? Use sudo fail2ban-client set sshd unbanip IP_ADDRESS. To check current bans, run sudo fail2ban-client status sshd.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →