Whiskey and rants

Biased ideas about things

UFW - Redirect a port from one IP to another

12 April, 2018 | Linux

Redirect from source (93.200.200.200) to destination (39.2.2.2) on port 443 (HTTPS)

/etc/sysctl.conf

# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1

/etc/ufw/sysctl.conf

# Uncomment this to allow this host to route packets between interfaces
net/ipv4/ip_forward=1
#net/ipv6/conf/default/forwarding=1
#net/ipv6/conf/all/forwarding=1

/etc/ufw/before.rules

# add this before FILTER
*nat :PREROUTING ACCEPT [0:0] :POSTROUTING ACCEPT [0:0] -A PREROUTING -i eth0 -d 93.200.200.200 -p tcp --dport 443 -j DNAT --to-destination 39.2.2.2:443 -A POSTROUTING -p tcp -d 39.2.2.2 --dport 443 -j SNAT --to-source 93.200.200.200 COMMIT # end of NAT section
*filter :ufw-before-input - [0:0] ...

Flush the NAT table

iptables -t nat -L -v

Check the redirection

iptables -t nat -L