This post describes one way to quickly set up a two-computer network such that both machines on the network can access the outside internet. The use case in mind here is you’re at a cafe with a friend, the wifi there sucks, you have your mobile broadband connection and you want to share it with your friend.
Note that there are many ways to get to the desired end result here. This solution has your local laptop doing NAT and forwarding packets at the IP layer.
Supplies:
- your debian/ubuntu laptop
- a “mobile broadband” connection with some provider, like Verizon or T-Mobile
- an ethernet cord
Step 0: Get your mobile broadband connection working, possibly with T-Mobile.
Step 1: Set up your machine to run a dhcp server for your local wired network
- sudo apt-get install isc-dhcp-server
- Edit /etc/dhcp/dhcpd.conf. Here are the relevant parts of mine. (Yes, the ‘authoritative’ directive is commented out, I’m not sure how essential this is or not – but I’m trying avoid the dhcp server from taking over my local machine’s default route.)
default-lease-time 600; max-lease-time 7200; #authoritative; log-facility syslog; subnet 172.16.16.0 netmask 255.255.255.0 { range 172.16.16.10 172.16.16.250; option domain-name-servers 208.67.222.222; # opendns option routers 172.16.16.1; } - Edit /etc/defaults/isc-dhcp-server:
mike@110psi:$ cat /etc/default/isc-dhcp-server | tail -n 1 INTERFACES="eth4"
- sudo ifconfig eth4 172.16.16.1 netmask 255.255.255.0
- sudo /etc/init.d/isc-dhcpd-server restart
At this point you should physically connect your friend’s laptop to yours using an ethernet cable. To watch the connection happen: tail -f /var/log/syslog. You should be able to go between the two computers and ping each other. If you can’t, then you want to debug this until you can… the rest of this recipe won’t have any effect if your local wired net is broken.
Step 2: Fix up your routing table
Between pppd, your local dhcp server, and you issuing manual ifconfig commands, it’s easy for your local routing table to get in a bad state. Here’s what you want it to look like:
mike@110psi:~$ netstat -nr Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 10.0.0.1 0.0.0.0 255.255.255.255 UH 0 0 0 ppp0 172.16.16.0 0.0.0.0 255.255.255.0 U 0 0 0 eth4 0.0.0.0 0.0.0.0 0.0.0.0 U 0 0 0 ppp0
If you need to remove/add/edit routes, the tool you want to use is ‘route’. For example:
sudo route del default sudo route add default gw 10.0.0.1
Step 3: Set up your computer to do some NAT
I scripted this out. Here’s the script:
#!/bin/sh
# nat and firewall
# for now, just nat.
ipt=/sbin/iptables
EXTIF=ppp0
INTIF=eth4
case "$1" in
start)
echo "Starting firewall:"
echo -ne "\tClearing existing rules..."
$ipt -F INPUT
$ipt -F OUTPUT
$ipt -F FORWARD
$ipt -t nat -F
echo " done."
echo -ne "\tInput / Output rules..."
$ipt -P INPUT ACCEPT
$ipt -P OUTPUT ACCEPT
echo " done."
echo -ne "\tForwarding rules, and /proc/sys/net/ipv4/ip_forward..."
echo "1" > /proc/sys/net/ipv4/ip_forward
#$ipt -A FORWARD -i $EXTIF -o $INTIF -m state --state ESTABLISHED,RELATED -j ACCEPT
$ipt -A FORWARD -i $EXTIF -o $INTIF -j ACCEPT
$ipt -A FORWARD -i $INTIF -o $EXTIF -j ACCEPT
echo " done."
echo -ne "\tEnabling MASQUERADE on $EXTIF..."
$ipt -t nat -A POSTROUTING -o $EXTIF -j MASQUERADE
echo " done."
echo "Firewall.sh is up."
;;
stop)
echo -n "Stopping firewall...";
$ipt -F INPUT
$ipt -F OUTPUT
$ipt -F FORWARD
$ipt -P INPUT ACCEPT
$ipt -P FORWARD ACCEPT
$ipt -P OUTPUT ACCEPT
$ipt -t nat -F
echo "0" > /proc/sys/net/ipv4/ip_forward
echo " done.";
echo "Firewall.sh is down."
;;
*)
N=/etc/init.d/firewall.sh
echo "Usage: $N {start|stop}" >&2
exit 1
;;
esac
Start your NAT up with “sudo ./nat-script-name.sh start”.
Step 4: debug it because it doesn’t work.
Break the problem into pieces:
- debug the connection between the two computers (/var/log/syslog, ifconfig, isc-dhcp-server and ping are your friends here)
- debug your local routing table (using netstat and route commands)
- debug your connection to your mobile broadband provider (pppd, wvdial, minicom, ping, etc)
If you find some variant of this recipe works better for your local machine, please post it in the comments so we can all share. Good luck.