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.

Iptables udp port forwarding- Easy way to do it!!

by | Nov 23, 2019

Have you ever faced any trouble while doing iptables UDP port forwarding? We can help you with it.

A few days ago, we received a request to do port forwarding using iptables to keep unwanted traffic off networks.

At Bobcares, we often get queries regarding port forwarding as part of our Server Management Services.

Today, let’s see how our Support Engineers do port forwarding efficiently using iptables.

 

Why we do ‘iptables port forwarding’?

Have any idea about port forwarding? Port forwarding forwards requests for a specific port to another host, network, or port.

We do port forwarding as it protects servers and clients from unwanted access. And, it limits access to and from a network.

By the way, port forwarding is simple to do with iptables.

 

PREROUTING Chain

Iptables have three different tables in the kernel: filter, nat(network address translation) and mangle table.

Among these, we use the nat table for address translation. It can change the source and/or target IP address in packets.

Usually, It has two chains: PREROUTING and POSTROUTING.

We use the PREROUTING chain when the destination address of the packet needs to be changed.

The nat table’s PREROUTING chain inspects the packet entering the firewall to see whether it requires destination modification (DNAT).

Here is a screenshot of how the packets process in the chain.

 

How does iptables UDP port forwarding occur?

So far we have discussed port forwarding. Now, let’s check how to do port forwarding using iptables.

Recently, one of our customers approached us with a request to do port forwarding.

He has two stream sources to his server on the same UDP port from two different IPs.

19x.16x.1.2:1003 via UDP to 19x.16x.1.4 (server)
19x.16x.1.3:1003 via UDP to 19x.16x.1.4 (server)

Our Support Engineers used the prerouting chain to forward the requested port. We used the below command.

iptables -t nat -A PREROUTING -p UDP -i eth0 -d 19x.16x.1.2 --dport 1003 -j DNAT --to-destination 19x.16x.1.2:1004

This rule indicates that all incoming UDP connections to the port 1003 should be sent to port 1004 of 192.168.1.2.

Here, -dport sets the destination port and DNAT indicates the packet’s destination address.

 

Allow traffic to port

But, we normally deny all the incoming connections to a gateway machine by default.

This is to reduce the security risk of opening all the ports and services. And, we only open the ports for the services that we use.

Therefore, in this case, to allow traffic on the port 1004, we use the below command.

iptables -A FORWARD -p UDP -d 19x.16x.1.2 --dport 1004 -j ACCEPT

So, by using these commands, we forwarded the ports as required by the customer.

 

[Need more help in port forwarding using iptables?- We’ll help you.]

 

Conclusion

In short, iptables UDP port forwarding keeps unwanted traffic off networks. In today’s writeup, we discussed port forwarding using iptables and saw how our Support Engineers do it efficiently.

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";

3 Comments

  1. Michael

    I have been searching for an answer to a very specific problem I was having with RHEL, Nested Virtualization, and iptables. I’ve been searching for DAYS, going over every stack overflow question and answer multiple times.

    THIS PAGE is the only thing that made my problem make sense, finally allowing me to solve it. From the bottom of my now pit-black soul – Thank you!

    Reply
  2. Rakesh

    I am seeing iptables erroe while forwarding snmp-traps from same server port to different port. can you plz help me.

    iptables -t nat -A PREROUTING -p UDP -i bond0 -d 10.10.10.10 –dport 162 -j DNAT –to-destination 10.194.236.246:9980
    iptables -A FORWARD -p UDP -d 10.10.10.10 –dport 9980 -j ACCEPT

    Reply
    • Hiba Razak

      Hi Rakesh,
      Please contact our support through live chat(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