25% off on first invoice for all services*

SPRING SALE

Use coupon

*Offer valid for new customers only

25% off on first invoice for all services*

SPRING SALE

Use coupon

*Offer valid for new customers only

Need help?

Our experts have had an average response time of 11.43 minutes in March 2024 to fix urgent issues.

We will keep your servers stable, secure, and fast at all times for one fixed price.

How to set up UFW port forwarding

by | Nov 12, 2019

Port forwarding in UFW can be tricky if you are not familiar with the working of the firewall.

UFW is a user-friendly firewall and can set up port forwarding with a few steps. Port forwarding helps to keep unwanted traffic off from servers.

At Bobcares, we often receive requests regarding the port forwarding as a part of our Server Management Services.

Today, let us discuss how Support Engineers set up port forwarding for our customers.

 

DNAT and PREROUTING

DNAT stands for Destination Network Address Translation that helps to change the destination port in the TCP/UDP headers.

It is used to redirect incoming packets with a destination of a public address/port to a private IP address/port inside a network.

PREROUTING is mostly used in DNAT. It will give access to internal machines. Even before a routing decision is made we need to alter its Destination address.

In short, port forwarding or port mapping is an application of network address translation (NAT).

 

How to set up port forwarding in UFW

Recently one of our customers contacted us to forward the incoming traffic for his port 80 to another interface to port 8080. Let us discuss how our Support Engineers setup port forwarding.

 

UFW Port forwarding to different IPs and ports

To set up port fowrading we need to enable IP forwarding at sysctl.conf. Our Support Engineers edit the file.

vi /etc/sysctl.conf

We un-comment the line by removing the # for “net.ipv4.ip_forward” to enable it.

If it is not present we will add the below line in the file and save the file.

net.ipv4.ip_forward=1

Reload using the command.

sysctl -p

Now we need to open the port for server for port forwarding. Else UFW will block the requests that need to be redirected.

ufw allow proto tcp from any to 2xx.2xx.1xx.0 port 8080

The port forwarding rule needs to enter in the file before.rules. We edit the file and add the below entry.

vi /etc/ufw/before.rules
*nat
:PREROUTING ACCEPT [0:0]
-A PREROUTING -i eth0 -d 2xx.2xx.1xx.0 -p tcp --dport 80 -j DNAT --to-destination 1xx.1xx.2xx.0:8080
# setup routing
-A POSTROUTING -s 1xx.1xx.2xx.0/24 ! -d 1xx.1xx.2xx.0/24 -j MASQUERADE
COMMIT

After entering the details save the file.

Finally, restart the ufw service.

systemctl restart ufw

 

UFW Port forwarding to a different port

Another customer contacted us and requested us to forward his traffic for port 80 to port 440. Let us discuss how our Support Engineers set it up for our customers.

First, we allow the port 440 in the firewall.

ufw allow 440

Add the below rules in before.rules file.

vi /etc/ufw/before.rules
*nat
:PREROUTING ACCEPT [0:0]
-A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 440
COMMIT

Save the file. Finally, we restart the service to make the changes effective.

service ufw restart

[Need help to set up UFW port forwarding? We’ll help you.]

 

Conclusion

In short, we discussed DNAT and PREROUTING and how we port forward from one port to another. Also, we have discussed how our Support Engineers set up port forwarding for port 80 to an internal interface.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

4 Comments

  1. Nigel Horne

    Doesn’t work:

    Jun 22 11:13:46 gs580s-u18njh ufw-init[974]: Bad argument `*nat’

    Reply
    • Sijin George

      Hi Nigel,
      Looks like a wrong edit in the config file.
      If you still have errors and need help, we’ll be happy to talk to you on chat (click on the icon at right-bottom).

      Reply
  2. William Lee

    Hello! I tried to do TCP port forwarding successfully. However, it seems the same steps if I use UDP it just don’t work.

    Any hints?

    Reply
    • Maheen Aboobakkar

      Hello William,

      We’ll be happy to talk to you on chat regarding the issue (click on the icon at right-bottom).

      Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Categories

Tags

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF