LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Laptop and Netbook
User Name
Password
Linux - Laptop and Netbook Having a problem installing or configuring Linux on your laptop? Need help running Linux on your netbook? This forum is for you. This forum is for any topics relating to Linux and either traditional laptops or netbooks (such as the Asus EEE PC, Everex CloudBook or MSI Wind).

Notices


Reply
  Search this Thread
Old 07-22-2016, 06:52 AM   #1
asteway
LQ Newbie
 
Registered: Jun 2012
Posts: 16

Rep: Reputation: Disabled
IPTABLES port forwarding not working


I have the following interfaces configured on my gateway running iptables:

eth0 - 6.7.8.9 (public ip)
eth1 - 10.0.10.1 (Internal LAN)

I want to host a web server on 10.0.10.6 from my internal LAN to be accessible from outside my LAN (the internet) via port 80.

I am assuming I need to forward port 80 on my gateway to port 80 in my internal web server. (please correct me if I am wrong or if I have any other option).

I run the following commands on my gateway to forward and NAT both incoming and outgoing traffic to my web server but I still can't reach my web server from outside.

Code:
iptables -A FORWARD -i eth0 -o eth1 -m state -p tcp -d 10.0.10.6 --dport 80 --state NEW,ESTABLISHED,RELATED -j ACCEPT

iptables -t nat -p tcp -A PREROUTING -i eth0 --dport 80 -j DNAT --to-destination 10.0.10.6
iptables -t nat -p tcp -o eth0 -A POSTROUTING -s 10.0.10.6 --dport 80 -j SNAT --to-source 6.7.8.9
FYI: I already have ip_forward enabled.
I have no other iptables rules on my gateway.

Thanks for your help

Last edited by asteway; 07-22-2016 at 03:06 PM.
 
Old 07-22-2016, 07:09 AM   #2
aragorn2101
Member
 
Registered: Dec 2012
Location: Mauritius
Distribution: Slackware
Posts: 567

Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
Hi,

What distro is this, please?
 
Old 07-22-2016, 08:28 AM   #3
asteway
LQ Newbie
 
Registered: Jun 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Thanks for the reply.
It is CentS 6
 
Old 07-22-2016, 01:04 PM   #4
lazydog
Senior Member
 
Registered: Dec 2003
Location: The Key Stone State
Distribution: CentOS Sabayon and now Gentoo
Posts: 1,249
Blog Entries: 3

Rep: Reputation: 194Reputation: 194
You should have more rules or at least setup the default profile to drop for safety reasons.
If your external IP is a DHCP address you should use MASQUERADE on the outbound rule.
You should stick to one format for your rules for example;

Code:
iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -p tcp -m tcp -i eth0 --dport 80 -m conntrack --ctstate NEW -j ACCEPT

iptables -t nat -A PREROUTING -p tcp -m tcp -i eth0 --dport 80 -j DNAT --to-destination 10.0.10.6

iptables -t nat -A POSTROUTING -p tcp -m tcp -o eth0 -j SNAT --to-source <External IP Address>
My reason for more rules or default to drop is you are open and if this box has any ports open they are also open to the internet.

A rule set I would suggest the following:
Code:
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

iptables -t nat -A PREROUTING -p tcp -m tcp -i eth0 --dport 80 -j DNAT --to-destination 10.0.10.6

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -j REJECT --reject-with icmp-host-prohibited

iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i eth1 -m conntrack --ctstate NEW -j ACCEPT
iptables -A FORWARD -p tcp -m tcp -i eth0 --dport 80 -m conntrack --ctstate NEW -j ACCEPT
iptables -A FORWARD -j REJECT --reject-with icmp-host-prohibited

iptables -A OUTPUT -m conntrack --ctstate NEW,RELATED,ESTABLISHED -j ACCEPT
Mind you the last OUTPUT rules is not really required but it comes in handy when you are check your connections as it will list all your outbound connections in the db. The above allows everything out no matter where it is coming from on the inside and only allows new connections from the internet to your web server. This is very useful for when you do updates so that the server can request the updates from the repos while blocking all new connection from the internet.

Last edited by lazydog; 07-22-2016 at 01:08 PM.
 
1 members found this post helpful.
Old 07-25-2016, 08:32 AM   #5
asteway
LQ Newbie
 
Registered: Jun 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Thanks for the comments,

I have applied your rules. But I still can't see my web site from the internet.

I guess the

Code:
iptables -t nat -A PREROUTING -p tcp -m tcp -i eth0 --dport 80 -j DNAT --to-destination 10.0.10.6
rule isn't working for some reason.


My rules look like this at the moment.

Code:
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            ctstate RELATED,ESTABLISHED 
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited 

Chain FORWARD (policy DROP)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            ctstate RELATED,ESTABLISHED 
ACCEPT     all  --  anywhere             anywhere            ctstate NEW 
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:http ctstate NEW 
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            ctstate NEW,RELATED,ESTABLISHED
 
Old 07-25-2016, 08:44 AM   #6
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Each rule has a counter which you can print with iptables -vL. This is helpful to see where your packets are going.
 
Old 07-25-2016, 10:35 AM   #7
asteway
LQ Newbie
 
Registered: Jun 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Noted!

iptables -vL

Code:
Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     all  --  lo     any     anywhere             anywhere            
   43  9205 ACCEPT     all  --  any    any     anywhere             anywhere            ctstate RELATED,ESTABLISHED 
    1   328 ACCEPT     udp  --  eth1   any     anywhere             anywhere            udp spts:bootps:bootpc dpts:bootps:bootpc 
   17  1188 ACCEPT     udp  --  any    any     anywhere             anywhere            udp dpt:domain 
    1    52 ACCEPT     tcp  --  any    any     anywhere             anywhere            tcp dpt:domain 
  180 27964 REJECT     all  --  any    any     anywhere             anywhere            reject-with icmp-host-prohibited 
    0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere            state NEW tcp dpt:http 

Chain FORWARD (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
 6479 3665K ACCEPT     all  --  any    any     anywhere             anywhere            ctstate RELATED,ESTABLISHED 
 1311  152K ACCEPT     all  --  eth1   any     anywhere             anywhere            ctstate NEW 
    0     0 ACCEPT     tcp  --  eth0   any     anywhere             anywhere            tcp dpt:http ctstate NEW 
   20   860 REJECT     all  --  any    any     anywhere             anywhere            reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT 58 packets, 4366 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  180 33653 ACCEPT     all  --  any    any     anywhere             anywhere            ctstate NEW,RELATED,ESTABLISHED

Last edited by asteway; 07-25-2016 at 10:41 AM.
 
Old 07-25-2016, 09:08 PM   #8
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Nothing makes it to the last rule in your INPUT chain because it is after the reject-all rule.
 
Old 07-26-2016, 10:13 AM   #9
lazydog
Senior Member
 
Registered: Dec 2003
Location: The Key Stone State
Distribution: CentOS Sabayon and now Gentoo
Posts: 1,249
Blog Entries: 3

Rep: Reputation: 194Reputation: 194
How did your rules change so much from post #5 to post #7? Or are they from 2 different systems?
 
Old 07-26-2016, 11:10 AM   #10
asteway
LQ Newbie
 
Registered: Jun 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Wink

I just added few more rules to open ports for DNS and DHCP internally. Should have no effect on the NAT and FORWARD rules whatsoever. Different tables😜
 
Old 07-26-2016, 11:26 AM   #11
asteway
LQ Newbie
 
Registered: Jun 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by smallpond View Post
Nothing makes it to the last rule in your INPUT chain because it is after the reject-all rule.
Noted! It's now corrected.
 
Old 07-26-2016, 12:48 PM   #12
lazydog
Senior Member
 
Registered: Dec 2003
Location: The Key Stone State
Distribution: CentOS Sabayon and now Gentoo
Posts: 1,249
Blog Entries: 3

Rep: Reputation: 194Reputation: 194
Quote:
Originally Posted by asteway View Post
Noted! It's now corrected.
Is it now working?
 
Old 07-26-2016, 01:18 PM   #13
asteway
LQ Newbie
 
Registered: Jun 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Not yet. I have now noticed telnet works for some ports from outside and dosen't work for others including port 80, telling me there's a firewall on the EPON that connects to the ISP that is blocking some incoming traffic and is turned on by default. I am looking to get support from my ISP in the next few days.

Last edited by asteway; 07-26-2016 at 02:05 PM.
 
Old 08-19-2016, 08:36 AM   #14
asteway
LQ Newbie
 
Registered: Jun 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
This is now working. The problem was as I suspected.
Thanks to all who have contributed.
 
  


Reply

Tags
firewall, iptables, linux



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
iptables port forwarding not working... robertjinx Linux - Server 8 03-27-2010 06:01 AM
[SOLVED] IPTables port forwarding using prerouting not working blackman890 Linux - Networking 3 02-19-2010 02:33 PM
IPtables port-forwarding not working. Ratclaws Linux - Networking 3 04-12-2005 08:14 AM
iptables port forwarding not working! friendklay Linux - Networking 1 03-23-2005 06:37 AM
Port forwarding with iptables is not working?!! philipina Linux - Networking 1 04-03-2004 03:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Laptop and Netbook

All times are GMT -5. The time now is 04:24 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