LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 08-06-2016, 04:02 AM   #1
maduino
LQ Newbie
 
Registered: Jun 2016
Posts: 5

Rep: Reputation: Disabled
Simple network package forwarding using IPtables not working


Hi,

I'm trying to setup a simple network to experiment with NAT a little bit. But currently it isn't working. I can't ping from COMPUTER_B to COMPUTER_A (see bellow). Could you give me a hint how to fix it?

* When I perform "ping 192.168.0.2" on COMPUTER_B I get an error
* When I do a "tcpdump -i eth1" on COMPUTER_C and then perform "ping 192.168.0.2" on COMPUTER_B, tcpdump prints out something like "Request who-has 192.168.0.2 tell 192.168.100.2" but nothing more

Why does it fail?

The network setup is:
Code:
[COMPUTER_A] <192.168.0.2---| ETH0> [COMPUTER_C] <ETH1 |---192.168.100.2> [COMPUTER_B]
I didn't setup any kind of DHCP or DNS, all IP addresses are static:

Code:
=== COMPUTER_A (/etc/network/interfaces) ===
auto eth0
allow-hotplug eth0
iface eth0 inet static
        address 192.168.0.2
        netmask 255.255.255.0

=== COMPUTER_B (/etc/network/interfaces) ===
auto eth0
allow-hotplug eth0
iface eth0 inet static
        address 192.168.100.2
        netmask 255.255.255.0

=== COMPUTER_C (/etc/network/interfaces) ===
auto eth0
allow-hotplug eth0
iface eth0 inet static
        address 192.168.0.1
        netmask 255.255.255.0

auto eth1
allow-hotplug eth1
iface eth1 inet static
        address 192.168.100.1
        netmask 255.255.255.0
I used this simple script to setup IPtables:
Code:
#!/bin/sh
# Reset everything
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -F INPUT
iptables -F OUTPUT
iptables -F FORWARD
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT

# Enable forwarding
sysctl net.ipv4.ip_forward=1
echo 1 > /proc/sys/net/ipv4/ip_forward

# Set IPtable rules
iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT
iptables -A FORWARD -i eth0 -o eth1 -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

# Print result
iptables -L
 
Old 08-07-2016, 05:51 AM   #2
tshikose
Member
 
Registered: Apr 2010
Location: Kinshasa, Democratic Republic of Congo
Distribution: RHEL, Fedora, CentOS
Posts: 525

Rep: Reputation: 95
Hi,

I think you need to first read a good iptables reference documentation.
Try googling a little or start by reading this and this.

From your samples, it seems to me that you are using a kind of Ubuntu or similar distro.
I am not familiar with them, but I will try to provide advices.

1. Why are you wiping all the existing rules and customised chains instead of starting from the default ones?
2. Having set the default policy for filter FORWARD chain to ACCEPT you do not need anymore the first two rules in your "# Set IPtable rules" section...
3. ... unless you expected to have final rule rejecting everything on those filter chains. But again this works well when modifying default rules.
4. For masquerading try this rule iptables -t nat -A POSTROUTING -i eth1 -o eth0 -j MASQUERADE
5. Unfortunately all your settings (including the kernel behaviour for ip forwarding) will not survive a reboot. You better do it in persistent way through configuration files.

After teaching yourself a little more and going through my advices, you are welcome to post for more help.

PS: Is the new and actual firewalld not available on your system? Then you will not need to use iptables.
 
Old 08-08-2016, 01:59 PM   #3
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
First thing first is to setup a working network. Once that is finished you can move onto bigger and better things.

Make sure that COMPUTER_A and COMPUTER_B know that COMPUTER_C is their default gateway.

by running the following command;

Code:
route -n
On COMPUTER_A you should get something like this;
Code:
~ $ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    100    0        0 eth0
192.168.0.0     0.0.0.0         255.255.255.0   U     100    0        0 eth0
and for COMPUTER_B
Code:
~ $ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.100.1   0.0.0.0         UG    100    0        0 eth0
192.168.100.0   0.0.0.0         255.255.224.0   U     100    0        0 eth0
On COMPUTER_C you will want to ensure that the firewall is stopped or allowing everything and that FOWARDING is turned on in the kernel.

This would work for the initial setup;
Code:
iptables -F
iptables -X
iptables -Z
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT

Since you want FORWARDing turned on I would suggest you enable it in sysctl.conf and add/change the following
Code:
net.ipv4.ip_forward = 1
Now ping from A to B and B to A should work.

Once you have a working network you can then go in and make changed and test things you want to test.
 
Old 08-08-2016, 02:01 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
Quote:
Originally Posted by tshikose View Post
PS: Is the new and actual firewalld not available on your system? Then you will not need to use iptables.
Firewalld is a gimmick and uses iptables anyway so why switch?
There is no good reason to switch to firewalld as iptables will do the same.
 
  


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
simple port forwarding with iptables ratcateme Linux - Networking 1 01-12-2009 04:05 PM
Simple Network forwarding with iptables javiersp Linux - Networking 1 02-26-2008 08:13 PM
iptables on router: simple port forwarding not working hamish Linux - Networking 1 10-27-2005 06:06 AM
Simple iptables forwarding 8080 -> 80 gstarrett Linux - Networking 3 03-17-2004 12:52 PM
Simple enough...iptables..port forwarding pembo13 Linux - Networking 4 07-19-2003 02:08 AM

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

All times are GMT -5. The time now is 03:34 AM.

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