Skip to content

Instantly share code, notes, and snippets.

@sdomi
Last active March 2, 2024 18:15
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdomi/82ca3af1dd5e2f463de6739067530e77 to your computer and use it in GitHub Desktop.
Save sdomi/82ca3af1dd5e2f463de6739067530e77 to your computer and use it in GitHub Desktop.
farewell

Magical Linux internet sharing: The Tutorial

The following tutorial assumes that you want to provide the interwebs to a PC connected via eth0 to your host, and that your host is connected to the world via wlan0.

  1. Set up the local IP address
# ifconfig eth0 192.168.250.1

or, if you're using ip:

# ip a a 192.168.250.1/24 dev eth0
  1. Enable IPv4 forwarding
# sysctl -w net.ipv4.ip_forward=1
  1. Forward packets via iptables
# iptables -A FORWARD --in-interface eth0 -j ACCEPT
# iptables --table nat -A POSTROUTING --out-interface wlan0 -j MASQUERADE
  1. Launch dnsmasq
# dnsmasq -F 192.168.250.2,192.168.250.10,12h -i eth0 -d

4.1 If dnsmasq is not available, set up IP on the second machine by hand

# ifconfig eth0 192.168.250.2
# route add default gw 192.168.250.1

...or, without ifconfig/route...

# ip a a 192.168.250.2/24 dev eth0
# ip r a default via 192.168.250.1
  1. ???
  2. Profit!
#!/bin/bash
# Working example script that shares internet from your Android phone via USB tethering through your PC to Ethernet.
# Tested on Void linux and quirky network card (note that we need to bring the iface down before bringing it up!)
ifconfig eth0 down
ifconfig eth0 0.0.0.0
sysctl -w net.ipv4.ip_forward=1
iptables -A FORWARD --in-interface eth0 -j ACCEPT
iptables --table nat -A POSTROUTING --out-interface usb0 -j MASQUERADE
ifconfig eth0 192.168.250.1
ifconfig eth0 up
dnsmasq -F 192.168.250.2,192.168.250.10,12h -i eth0 -d
@n0toose
Copy link

n0toose commented Jul 17, 2020

Hey, casual onlooker that's maintaining their own network as a hobby over here.

What would be the best way to do that automatically, in your opinion? (as in; not using a systemd service to run this bash script)

@sdomi
Copy link
Author

sdomi commented Jul 17, 2020

Hey, casual onlooker that's maintaining their own network as a hobby over here.

What would be the best way to do that automatically, in your opinion? (as in; not using a systemd service to run this bash script)

  1. Invoke iptables-save (and you may need to redirect the output to /etc/iptables/rules.v4 depending on your OS).

  2. Replace your /etc/dnsmasq.conf with the following:

port=53

except-interface=wlan0
dhcp-range=interface:eth0,192.168.250.10,192.168.250.254,24h
  1. Add net.ipv4.ip_forward=1 to your /etc/sysctl.conf.

  2. And for the last step, you need to somehow bring up eth0 on boot and set the static IP. I'd go with either /etc/rc.local and adding a few ifconfig commands in there, or using /etc/network/interfaces and configuring it over there - but you may go as far as to create a static IP NetworkManager config if you so desire.

@n0toose
Copy link

n0toose commented Jul 18, 2020

Thanks a lot, really needed a run-down like this because I kept doing all of these steps alongside other "contradictory" steps that just broke the entire thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment