How to add NAT iptables rules with CSF

32 sec read

What is CSF

Config Server Firewall (or CSF) is a free and advanced firewall for most Linux distributions.

How to add NAT iptables rules with CSF Firewall

We can add custom NAT iptables rules in /etc/csf/csfpre.sh OR/AND /etc/csf/csfpost.sh
0. Create csfpre.sh or csfpost.sh

touch /etc/csf/csfpre.sh /etc/csf/csfpost.sh

1. Change permission to make the script executable

chmod +x /etc/csf/csfpre.sh /etc/csf/csfpost.sh
chmod +t /etc/csf/csfpre.sh /etc/csf/csfpost.sh

2. Add NAT iptables rules

vim /etc/csf/csfpre.sh

add iptables rules like this:

iptables -t nat -F
iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 1.2.3.4:80
iptables -t nat -A POSTROUTING -p tcp -d 1.2.3.4 --dport 80 -j SNAT --to-source 4.3.2.1

3. Restart csf and check the result

csf -r
iptables -t nat -L

Ref

+ Google
+ TecAdmin

How to install NextCloud in Ubuntu 22.04

What’s NextCloud Nextcloud is an open-source software suite that offers a secure, self-hosted alternative to popular cloud storage and productivity platforms. With a focus...
sysadmin.id
8 min read

Reset root password on CentOS 7

Edit boot menu on-the-go 0. reboot the CentOS 7 and press ESC when GRUB menu show up on the screen and press “e” 1....
sysadmin.id
18 sec read

Free ext4 reserved blocks with tune2fs

Check disk usage df -h Filesystem Size Used Avail Use% Mounted on /dev/vda1 95G 82G 7.9G 92% / Check size of Reserved block count...
sysadmin.id
19 sec read