100

I have seen on many blogs, using this command to enable IP forwarding while using many network security/sniffing tools on linux

echo 1 > /proc/sys/net/ipv4/ip_forward

Can anyone explain me in layman terms, what essentially does this command do? Does it turn your system into router?

1

2 Answers 2

117

"IP forwarding" is a synonym for "routing." It is called "kernel IP forwarding" because it is a feature of the Linux kernel.

A router has multiple network interfaces. If traffic comes in on one interface that matches a subnet of another network interface, a router then forwards that traffic to the other network interface.

So, let's say you have two NICs, one (NIC 1) is at address 192.168.2.1/24, and the other (NIC 2) is 192.168.3.1/24. If forwarding is enabled, and a packet comes in on NIC 1 with a "destination address" of 192.168.3.8, the router will resend that packet out of the NIC 2.

It's common for routers functioning as gateways to the Internet to have a default route whereby any traffic that doesn't match any NICs will go through the default route's NIC. So in the above example, if you have an internet connection on NIC 2, you'd set NIC 2 as your default route and then any traffic coming in from NIC 1 that isn't destined for something on 192.168.2.0/24 will go through NIC 2. Hopefully there's other routers past NIC 2 that can further route it (in the case of the Internet, the next hop would be your ISP's router, and then their providers upstream router, etc.)

Enabling ip_forward tells your Linux system to do this. For it to be meaningful, you need two network interfaces (any 2 or more of wired NIC cards, Wifi cards or chipsets, PPP links over a 56k modem or serial, etc.).

When doing routing, security is important and that's where Linux's packet filter, iptables, gets involved. So you will need an iptables configuration consistent with your needs.

Note that enabling forwarding with iptables disabled and/or without taking firewalling and security into account could leave you open to vulnerabilites if one of the NICs is facing the Internet or a subnet you don't have control over.

8
  • 2
    Right, you'll want to do NAT if NIC 2 is a private IP. Linux can do NAT as well and iptables is the way to set it up.
    – LawrenceC
    Jul 13, 2014 at 23:22
  • Does it need a NAT for data going from NIC 2 to NIC 1?
    – GutenYe
    Jul 14, 2014 at 0:12
  • 3
    Typically NAT will work from the LAN to the WAN, so if NIC 2 has a private IP, and NIC 1 has a public 1, you do need NAT; specifically, the internet-facing interface (NIC 1 per above) needs a MASQUERADE rule in iptables's POSTROUTING on a chain to do that. See revsys.com/writings/quicktips/nat.html and i.stack.imgur.com/rzz83.png.
    – LawrenceC
    Jul 16, 2014 at 11:14
  • In simple words, will it be right if I say that enabling IP forwarding means enabling the machine to pass a packet from one of its NIC to another of its NIC? Also, would it be true if I say enabling IP forwarding is not really required on a machine with a single IP/NIC ?
    – Sreeraj
    Nov 21, 2014 at 6:37
  • 1
    @ultrasawblade In the case of IP forwarding enabled and having internet connection on NIC 2 and also as the default route, if it receives a packet destined for 192.168.2.2 from NIC 1, what will the router do?
    – bobo
    May 14, 2015 at 5:30
13

When enabled, "IP forwarding" allows a Linux machine to receive incoming packets and forward them. A Linux machine acting as an ordinary host would not need to have IP forwarding enabled, because it just generates and receives IP traffic for its own purposes (i.e., the purposes of its user).

However, there are cases when IP forwarding is useful: 1. We want our machine to act as a router, receiving packets from other hosts and routing them toward their destination. 2. We are bad guys and we want to impersonate another machine in a so called "man-in-the-middle-attack". In this case, we want to intercept and see all the traffic directed to the victim, but we want also to forward this traffic to her, so that she does not "sense" our presence.

2
  • Why "IP forwarding" is not enabled by default in Linux distros?
    – t7e
    Aug 31, 2020 at 20:15
  • @t7e I think it is enabled by default in most Linux distributions nowadays. It's enabled by default in my Ubuntu 20 Aug 1, 2021 at 12:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .