Given: I have a machine (HostA) with only one NIC which has Internet connectivity. I have another machine (HostB) with one NIC on the same switch. HostB is not configured for Internet access yet. HostA has its default gateway and DNS servers appropriately configured. IPv4 is being used. OSes on the hosts are Ubuntu 13 and Fedora17.What I want: Now, I would like HostB to have Internet connectivity, too. Is this possible using 'some' combination of iptables, virtual tun/tap devices, or a VPN setup between HostA and HostB, etc? What I already know and can do: Currently, I can use an ssh-based SOCKS proxy on HostB (ssh -D 9050 UserA@HostA) and route traffic of select 'socksifiable' applications on HostB via this proxy to HostA and beyond. However, sadly, not all applications are socksifiable. Now, I know very well that if HostA had 2 NICs, I could have used some iptables rules to convert HostA into a gateway that would then route traffic between its NIC-1 and NIC-2 (where NIC-1 would be connected to HostB and NIC-2 to Internet). But installing another NIC in HostA is not feasible for me.PS: I had posted this earlier on superuser.com but got no useful information.edit 1:network informationHost A::> ip addr[...]2: p4p1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether d4:be:d9:d5:46:05 brd ff:ff:ff:ff:ff:ffinet 192.168.22.9/24 brd 192.168.22.255 scope global p4p1 :> ip routedefault via 192.168.22.254 dev p4p1 proto static192.168.22.0/24 dev p4p1 proto kernel scope link src 192.168.22.9Host B::> ip addr[...]2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 30:f9:ed:d9:2e:20 brd ff:ff:ff:ff:ff:ffinet 192.168.22.234/24 brd 192.168.22.255 scope global eth0 :> ip route169.254.0.0/16 dev eth0 scope link metric 1000192.168.22.0/24 dev eth0 proto kernel scope link src 192.168.22.234 metric 1
Can iptables be used to convert a single-homed host into a NAT server?
linux;networking;iptables;nat
Routing is about where (and if) to send to. That's not limited to selecting a NIC. In your case routing is very simple though.You need masquerading in its most simple form (all commands on host A):iptables -t nat -I POSTROUTING -s 192.168.22.234 -j MASQUERADEAnd maybe (if not yet) you need allow forwarding:iptables -I FORWARD 1 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPTiptables -I FORWARD 2 -s 192.168.22.234 -j ACCEPTAssuming host A is configured as the default gateway for Host B.Edit 1:After a chat discussion the situation has become clearer. In theory configuring the default gateway on B should have been enough. But it seems that the gateway (which is not under the control of the questioner) blocks host B. Thus the masquerading solution was necessary.