Use iptables as router
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
up vote
3
down vote
favorite
I am having the following setup:
client:
192.168.103.55
"router"
192.168.103.30 (eth3), 192.168.102.30 (eth2)
server-1 server-2
192.168.102.21 (eth2) 192.168.102.22 (eth2)
The router is actually a small linux machine, running iptables.
The goal is to configure iptables in such a way that I can ping from the servers (192.168.102.21 and .22) to the client (192.168.103.55), via the router (192.168.102.30 at server-side and 192.168.103.30 at client-side)
The servers have a route configured as:
192.168.103.55 via 192.168.102.30 dev eth2
On the router I have configured the following rules in IP-tables:
*filter
:INPUT ACCEPT [1610193:248234329]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [1945999:238163662]
-A FORWARD -i eth3 -j ACCEPT
-A FORWARD -o eth3 -j ACCEPT
-A FORWARD -i eth2 -j ACCEPT
-A FORWARD -o eth2 -j ACCEPT
COMMIT
# Completed on Wed May 2 08:26:55 2018
# Generated by iptables-save v1.4.21 on Wed May 2 08:26:55 2018
*nat
:PREROUTING ACCEPT [5610:715368]
:INPUT ACCEPT [2029:121740]
:OUTPUT ACCEPT [326029:19788110]
:POSTROUTING ACCEPT [326029:19788110]
-A POSTROUTING -d 192.168.103.55/32 -o eth3 -j SNAT --to-source 192.168.103.30
COMMIT
The ping from 192.168.102.21 is reaching the client (192.168.103.55), and the client sends the reply towards 192.168.103.30. But the reply is not forwarded towards to the client (.55). It gets stuck in the router
What am I missing here?
Thanks in advance for your time!
=================== UPDATE ===================
Guntbert suggestion on The forwarding was already done.
The SNAT-rule is required in order for the router to know to which server to route back the ICMP Reply.
In my first explanation I left out the second server. I updated the question now to give you the full overview.
The SNAT seems to work somehow, since I can see the ICMP-reply packet going to the correct server:
[router ~]# tcpdump -ni eth2 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth2, link-type EN10MB (Ethernet), capture size 65535 bytes
08:38:26.110245 IP 192.168.102.21 > 192.168.103.55: ICMP echo request, id 4471, seq 1, length 64
08:38:26.112722 IP 192.168.103.55 > 192.168.102.21: ICMP echo reply, id 4471, seq 1, length 64
08:39:53.238281 IP 192.168.102.22 > 192.168.103.55: ICMP echo request, id 8285, seq 1, length 64
08:39:53.239110 IP 192.168.103.55 > 192.168.102.22: ICMP echo reply, id 8285, seq 1, length 64
What actually should happen next, I think, is that the source IP (192.168.103.55) in the reply needs to be replaced by the router's IP (192.168.102.30).
Or am I wrong?
iptables router
add a comment |Â
up vote
3
down vote
favorite
I am having the following setup:
client:
192.168.103.55
"router"
192.168.103.30 (eth3), 192.168.102.30 (eth2)
server-1 server-2
192.168.102.21 (eth2) 192.168.102.22 (eth2)
The router is actually a small linux machine, running iptables.
The goal is to configure iptables in such a way that I can ping from the servers (192.168.102.21 and .22) to the client (192.168.103.55), via the router (192.168.102.30 at server-side and 192.168.103.30 at client-side)
The servers have a route configured as:
192.168.103.55 via 192.168.102.30 dev eth2
On the router I have configured the following rules in IP-tables:
*filter
:INPUT ACCEPT [1610193:248234329]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [1945999:238163662]
-A FORWARD -i eth3 -j ACCEPT
-A FORWARD -o eth3 -j ACCEPT
-A FORWARD -i eth2 -j ACCEPT
-A FORWARD -o eth2 -j ACCEPT
COMMIT
# Completed on Wed May 2 08:26:55 2018
# Generated by iptables-save v1.4.21 on Wed May 2 08:26:55 2018
*nat
:PREROUTING ACCEPT [5610:715368]
:INPUT ACCEPT [2029:121740]
:OUTPUT ACCEPT [326029:19788110]
:POSTROUTING ACCEPT [326029:19788110]
-A POSTROUTING -d 192.168.103.55/32 -o eth3 -j SNAT --to-source 192.168.103.30
COMMIT
The ping from 192.168.102.21 is reaching the client (192.168.103.55), and the client sends the reply towards 192.168.103.30. But the reply is not forwarded towards to the client (.55). It gets stuck in the router
What am I missing here?
Thanks in advance for your time!
=================== UPDATE ===================
Guntbert suggestion on The forwarding was already done.
The SNAT-rule is required in order for the router to know to which server to route back the ICMP Reply.
In my first explanation I left out the second server. I updated the question now to give you the full overview.
The SNAT seems to work somehow, since I can see the ICMP-reply packet going to the correct server:
[router ~]# tcpdump -ni eth2 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth2, link-type EN10MB (Ethernet), capture size 65535 bytes
08:38:26.110245 IP 192.168.102.21 > 192.168.103.55: ICMP echo request, id 4471, seq 1, length 64
08:38:26.112722 IP 192.168.103.55 > 192.168.102.21: ICMP echo reply, id 4471, seq 1, length 64
08:39:53.238281 IP 192.168.102.22 > 192.168.103.55: ICMP echo request, id 8285, seq 1, length 64
08:39:53.239110 IP 192.168.103.55 > 192.168.102.22: ICMP echo reply, id 8285, seq 1, length 64
What actually should happen next, I think, is that the source IP (192.168.103.55) in the reply needs to be replaced by the router's IP (192.168.102.30).
Or am I wrong?
iptables router
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I am having the following setup:
client:
192.168.103.55
"router"
192.168.103.30 (eth3), 192.168.102.30 (eth2)
server-1 server-2
192.168.102.21 (eth2) 192.168.102.22 (eth2)
The router is actually a small linux machine, running iptables.
The goal is to configure iptables in such a way that I can ping from the servers (192.168.102.21 and .22) to the client (192.168.103.55), via the router (192.168.102.30 at server-side and 192.168.103.30 at client-side)
The servers have a route configured as:
192.168.103.55 via 192.168.102.30 dev eth2
On the router I have configured the following rules in IP-tables:
*filter
:INPUT ACCEPT [1610193:248234329]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [1945999:238163662]
-A FORWARD -i eth3 -j ACCEPT
-A FORWARD -o eth3 -j ACCEPT
-A FORWARD -i eth2 -j ACCEPT
-A FORWARD -o eth2 -j ACCEPT
COMMIT
# Completed on Wed May 2 08:26:55 2018
# Generated by iptables-save v1.4.21 on Wed May 2 08:26:55 2018
*nat
:PREROUTING ACCEPT [5610:715368]
:INPUT ACCEPT [2029:121740]
:OUTPUT ACCEPT [326029:19788110]
:POSTROUTING ACCEPT [326029:19788110]
-A POSTROUTING -d 192.168.103.55/32 -o eth3 -j SNAT --to-source 192.168.103.30
COMMIT
The ping from 192.168.102.21 is reaching the client (192.168.103.55), and the client sends the reply towards 192.168.103.30. But the reply is not forwarded towards to the client (.55). It gets stuck in the router
What am I missing here?
Thanks in advance for your time!
=================== UPDATE ===================
Guntbert suggestion on The forwarding was already done.
The SNAT-rule is required in order for the router to know to which server to route back the ICMP Reply.
In my first explanation I left out the second server. I updated the question now to give you the full overview.
The SNAT seems to work somehow, since I can see the ICMP-reply packet going to the correct server:
[router ~]# tcpdump -ni eth2 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth2, link-type EN10MB (Ethernet), capture size 65535 bytes
08:38:26.110245 IP 192.168.102.21 > 192.168.103.55: ICMP echo request, id 4471, seq 1, length 64
08:38:26.112722 IP 192.168.103.55 > 192.168.102.21: ICMP echo reply, id 4471, seq 1, length 64
08:39:53.238281 IP 192.168.102.22 > 192.168.103.55: ICMP echo request, id 8285, seq 1, length 64
08:39:53.239110 IP 192.168.103.55 > 192.168.102.22: ICMP echo reply, id 8285, seq 1, length 64
What actually should happen next, I think, is that the source IP (192.168.103.55) in the reply needs to be replaced by the router's IP (192.168.102.30).
Or am I wrong?
iptables router
I am having the following setup:
client:
192.168.103.55
"router"
192.168.103.30 (eth3), 192.168.102.30 (eth2)
server-1 server-2
192.168.102.21 (eth2) 192.168.102.22 (eth2)
The router is actually a small linux machine, running iptables.
The goal is to configure iptables in such a way that I can ping from the servers (192.168.102.21 and .22) to the client (192.168.103.55), via the router (192.168.102.30 at server-side and 192.168.103.30 at client-side)
The servers have a route configured as:
192.168.103.55 via 192.168.102.30 dev eth2
On the router I have configured the following rules in IP-tables:
*filter
:INPUT ACCEPT [1610193:248234329]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [1945999:238163662]
-A FORWARD -i eth3 -j ACCEPT
-A FORWARD -o eth3 -j ACCEPT
-A FORWARD -i eth2 -j ACCEPT
-A FORWARD -o eth2 -j ACCEPT
COMMIT
# Completed on Wed May 2 08:26:55 2018
# Generated by iptables-save v1.4.21 on Wed May 2 08:26:55 2018
*nat
:PREROUTING ACCEPT [5610:715368]
:INPUT ACCEPT [2029:121740]
:OUTPUT ACCEPT [326029:19788110]
:POSTROUTING ACCEPT [326029:19788110]
-A POSTROUTING -d 192.168.103.55/32 -o eth3 -j SNAT --to-source 192.168.103.30
COMMIT
The ping from 192.168.102.21 is reaching the client (192.168.103.55), and the client sends the reply towards 192.168.103.30. But the reply is not forwarded towards to the client (.55). It gets stuck in the router
What am I missing here?
Thanks in advance for your time!
=================== UPDATE ===================
Guntbert suggestion on The forwarding was already done.
The SNAT-rule is required in order for the router to know to which server to route back the ICMP Reply.
In my first explanation I left out the second server. I updated the question now to give you the full overview.
The SNAT seems to work somehow, since I can see the ICMP-reply packet going to the correct server:
[router ~]# tcpdump -ni eth2 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth2, link-type EN10MB (Ethernet), capture size 65535 bytes
08:38:26.110245 IP 192.168.102.21 > 192.168.103.55: ICMP echo request, id 4471, seq 1, length 64
08:38:26.112722 IP 192.168.103.55 > 192.168.102.21: ICMP echo reply, id 4471, seq 1, length 64
08:39:53.238281 IP 192.168.102.22 > 192.168.103.55: ICMP echo request, id 8285, seq 1, length 64
08:39:53.239110 IP 192.168.103.55 > 192.168.102.22: ICMP echo reply, id 8285, seq 1, length 64
What actually should happen next, I think, is that the source IP (192.168.103.55) in the reply needs to be replaced by the router's IP (192.168.102.30).
Or am I wrong?
iptables router
iptables router
edited May 8 at 18:25
guntbert
8,750123067
8,750123067
asked Apr 13 at 11:42
Radje
162
162
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
you need to enable packet forwarding in the kernel
edit
/etc/sysctl.conf
and activate the linenet.ipv4.ip_forward=1
by removing the
#
at the beginning.reboot or enable the setting immediately with
echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
Don't use SNAT (in fact don't use any kind of NAT at all). You have two networks, the router is attached to both (it has a leg in both). So remove the 3rd line of your
iptables
-rules.- remove the NAT rule from the router
- configure all systems to reach the "other" network via the router
- on the servers replace your route with
192.168.103.0/24 via 192.168.102.30 dev eth2
(this tells them how to reach the whole network, not just one client) - on the client add the following route
192.168.102.0/24 via 192.168.103.30 dev eth0
- on the servers replace your route with
Now you will see that every packet from both servers will reach the client with the original source address. The client knows how to reach that source address and can send a reply to exactly the server that pinged it.
Hello Guntbert, The forwarding was already enabled. I need the SNAT-rule in order the router to know to which server to route back the ICMP packet. This seems to work well, as I can see the ICMP-reply packet going to the correct server:
â Radje
May 2 at 6:56
see above the update questions and additional findings
â Radje
May 2 at 7:06
@Radje thank you for the clarifying picture - I think I can see where you made the mistake that led you to using NAT and have amended my answer accordingly.
â guntbert
May 8 at 18:48
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
you need to enable packet forwarding in the kernel
edit
/etc/sysctl.conf
and activate the linenet.ipv4.ip_forward=1
by removing the
#
at the beginning.reboot or enable the setting immediately with
echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
Don't use SNAT (in fact don't use any kind of NAT at all). You have two networks, the router is attached to both (it has a leg in both). So remove the 3rd line of your
iptables
-rules.- remove the NAT rule from the router
- configure all systems to reach the "other" network via the router
- on the servers replace your route with
192.168.103.0/24 via 192.168.102.30 dev eth2
(this tells them how to reach the whole network, not just one client) - on the client add the following route
192.168.102.0/24 via 192.168.103.30 dev eth0
- on the servers replace your route with
Now you will see that every packet from both servers will reach the client with the original source address. The client knows how to reach that source address and can send a reply to exactly the server that pinged it.
Hello Guntbert, The forwarding was already enabled. I need the SNAT-rule in order the router to know to which server to route back the ICMP packet. This seems to work well, as I can see the ICMP-reply packet going to the correct server:
â Radje
May 2 at 6:56
see above the update questions and additional findings
â Radje
May 2 at 7:06
@Radje thank you for the clarifying picture - I think I can see where you made the mistake that led you to using NAT and have amended my answer accordingly.
â guntbert
May 8 at 18:48
add a comment |Â
up vote
2
down vote
you need to enable packet forwarding in the kernel
edit
/etc/sysctl.conf
and activate the linenet.ipv4.ip_forward=1
by removing the
#
at the beginning.reboot or enable the setting immediately with
echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
Don't use SNAT (in fact don't use any kind of NAT at all). You have two networks, the router is attached to both (it has a leg in both). So remove the 3rd line of your
iptables
-rules.- remove the NAT rule from the router
- configure all systems to reach the "other" network via the router
- on the servers replace your route with
192.168.103.0/24 via 192.168.102.30 dev eth2
(this tells them how to reach the whole network, not just one client) - on the client add the following route
192.168.102.0/24 via 192.168.103.30 dev eth0
- on the servers replace your route with
Now you will see that every packet from both servers will reach the client with the original source address. The client knows how to reach that source address and can send a reply to exactly the server that pinged it.
Hello Guntbert, The forwarding was already enabled. I need the SNAT-rule in order the router to know to which server to route back the ICMP packet. This seems to work well, as I can see the ICMP-reply packet going to the correct server:
â Radje
May 2 at 6:56
see above the update questions and additional findings
â Radje
May 2 at 7:06
@Radje thank you for the clarifying picture - I think I can see where you made the mistake that led you to using NAT and have amended my answer accordingly.
â guntbert
May 8 at 18:48
add a comment |Â
up vote
2
down vote
up vote
2
down vote
you need to enable packet forwarding in the kernel
edit
/etc/sysctl.conf
and activate the linenet.ipv4.ip_forward=1
by removing the
#
at the beginning.reboot or enable the setting immediately with
echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
Don't use SNAT (in fact don't use any kind of NAT at all). You have two networks, the router is attached to both (it has a leg in both). So remove the 3rd line of your
iptables
-rules.- remove the NAT rule from the router
- configure all systems to reach the "other" network via the router
- on the servers replace your route with
192.168.103.0/24 via 192.168.102.30 dev eth2
(this tells them how to reach the whole network, not just one client) - on the client add the following route
192.168.102.0/24 via 192.168.103.30 dev eth0
- on the servers replace your route with
Now you will see that every packet from both servers will reach the client with the original source address. The client knows how to reach that source address and can send a reply to exactly the server that pinged it.
you need to enable packet forwarding in the kernel
edit
/etc/sysctl.conf
and activate the linenet.ipv4.ip_forward=1
by removing the
#
at the beginning.reboot or enable the setting immediately with
echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
Don't use SNAT (in fact don't use any kind of NAT at all). You have two networks, the router is attached to both (it has a leg in both). So remove the 3rd line of your
iptables
-rules.- remove the NAT rule from the router
- configure all systems to reach the "other" network via the router
- on the servers replace your route with
192.168.103.0/24 via 192.168.102.30 dev eth2
(this tells them how to reach the whole network, not just one client) - on the client add the following route
192.168.102.0/24 via 192.168.103.30 dev eth0
- on the servers replace your route with
Now you will see that every packet from both servers will reach the client with the original source address. The client knows how to reach that source address and can send a reply to exactly the server that pinged it.
edited Aug 26 at 18:15
![](https://i.stack.imgur.com/9L8vd.png?s=32&g=1)
![](https://i.stack.imgur.com/9L8vd.png?s=32&g=1)
dessert
19.8k55594
19.8k55594
answered Apr 13 at 21:44
guntbert
8,750123067
8,750123067
Hello Guntbert, The forwarding was already enabled. I need the SNAT-rule in order the router to know to which server to route back the ICMP packet. This seems to work well, as I can see the ICMP-reply packet going to the correct server:
â Radje
May 2 at 6:56
see above the update questions and additional findings
â Radje
May 2 at 7:06
@Radje thank you for the clarifying picture - I think I can see where you made the mistake that led you to using NAT and have amended my answer accordingly.
â guntbert
May 8 at 18:48
add a comment |Â
Hello Guntbert, The forwarding was already enabled. I need the SNAT-rule in order the router to know to which server to route back the ICMP packet. This seems to work well, as I can see the ICMP-reply packet going to the correct server:
â Radje
May 2 at 6:56
see above the update questions and additional findings
â Radje
May 2 at 7:06
@Radje thank you for the clarifying picture - I think I can see where you made the mistake that led you to using NAT and have amended my answer accordingly.
â guntbert
May 8 at 18:48
Hello Guntbert, The forwarding was already enabled. I need the SNAT-rule in order the router to know to which server to route back the ICMP packet. This seems to work well, as I can see the ICMP-reply packet going to the correct server:
â Radje
May 2 at 6:56
Hello Guntbert, The forwarding was already enabled. I need the SNAT-rule in order the router to know to which server to route back the ICMP packet. This seems to work well, as I can see the ICMP-reply packet going to the correct server:
â Radje
May 2 at 6:56
see above the update questions and additional findings
â Radje
May 2 at 7:06
see above the update questions and additional findings
â Radje
May 2 at 7:06
@Radje thank you for the clarifying picture - I think I can see where you made the mistake that led you to using NAT and have amended my answer accordingly.
â guntbert
May 8 at 18:48
@Radje thank you for the clarifying picture - I think I can see where you made the mistake that led you to using NAT and have amended my answer accordingly.
â guntbert
May 8 at 18:48
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1024638%2fuse-iptables-as-router%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password