LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 02-27-2011, 03:50 AM   #1
dr_doom
LQ Newbie
 
Registered: Apr 2004
Posts: 13

Rep: Reputation: 0
Iptables redirect from one local port to another


Hello,

I need some help with creating my iptables rules.
I would like to redirect all requests coming to port 80 to port 8080.
I've tried with DNAT and REDIRECT and they both work fine, but also I would like to block port 8080 to be not accessible from outside the box.

Here is an example of what I have done:

iptables -P INPUT DROP
iptables -P OUTPUT DROP

iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

iptables -t nat -A PREROUTING -j REDIRECT -p tcp --dport 80 --to-ports 8080

If I set the default policy for INPUT and OUTPUT to DROP, the redirection does not work.
I've tried also to add the following two:

iptables -A INPUT -i eth0 -p tcp -s 192.168.1.5 -d 192.168.1.5 -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp -s 192.168.1.5 -d 192.168.1.5 -j ACCEPT

The IP 192.168.1.5 is where is bound the service listening to port 8080.

If I leave the default policies to ACCEPT everyone is able to access the service via both ports 80 and 8080, but I would like to prevent the access to port 8080.


Thanks in advance.
 
Old 02-28-2011, 06:42 AM   #2
nimnull22
Senior Member
 
Registered: Jul 2009
Distribution: OpenSuse 11.1, Fedora 14, Ubuntu 12.04/12.10, FreeBSD 9.0
Posts: 1,571

Rep: Reputation: 92
iptables -t nat -A PREROUTING -j REDIRECT -p tcp --dport 80 --to-ports 8080

This rule just changes destination port, from 80 to 8080 and doesn't the destination IP.
So the packet goes into INPUT or FORWARD chain, but they have their own rules DROP or ACCEPT.
I suggest to change default rules to ACCEPT and filter out unwanted packets in the INPUT or FORWARD chain.

Thanks
 
Old 02-28-2011, 10:19 PM   #3
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
You could try the approach I outlined over here. Example:
Code:
iptables -t mangle -A PREROUTING -i eth0 -p TCP --dport 8080 -j MARK --set-mark 666
iptables -t nat -A PREROUTING -i eth0 -p TCP --dport 80 -j REDIRECT --to-port 8080
iptables -I INPUT -m mark --mark 666 -j DROP
iptables -A INPUT -i eth0 -p TCP --dport 8080 -j ACCEPT
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
with iptables, how-to redirect outgoing mail from port 25 to port 587 thumbelina Linux - Networking 12 03-19-2012 08:26 AM
Port redirect with help of IPTABLES EgoX Linux - Networking 1 09-06-2010 02:45 AM
[SOLVED] iptables redirect : local to remote public host paziulek Linux - Networking 2 05-17-2010 02:34 PM
Local LPT Port Redirect to Network Printer miker9 Linux - Hardware 1 05-15-2008 06:39 PM
debian iptables squid - redirect port 80 to port 8080 on another machine nickleus Linux - Networking 1 08-17-2006 12:59 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 04:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration