Masquerading how-to for GNU/Linux
Prerequisites
- dnsmasq
- iptables
Those softwares should be available in your distribution's repositories. For Debian (or Debian-based distribution) install for example with:
aptitude install dnsmasq iptables
Dnmasq configuration
We'll use dnsmasq as DNS proxy and DHCP server. Will configure it so that it handles DHCP and DNS request on any interface with an IP adress in the 192.168.40.0/24 subnet.
In /etc/dnsmasq/dnsmasq.conf add the following line:
dhcp-range=192.168.40.50,192.168.40.200,12h
Enable masquerading
Let's suppose you're connected to the internet via wan and you want to
share it on lan.
Enable packet forwarding
echo 1 > /proc/sys/net/ipv4/ip_forwardEnable masquerading of packet forwarded to
waniptables -t nat -A POSTROUTING -o wan -j MASQUERADESetup the
laninterfaceip link set dev lan up ip addr add 192.168.40.1/24 dev lanStart dnsmasq
If you are using sysvinit execute
invoke-rc.d dnsmasq start.If you are using systemd execute
systemctl start dnsmasq.
Disable masquerading
It is sufficient to remove the ip address on the interface sharing the connection:
ip addr del 192.168.40.1/24 dev lan
To clean things up you may also:
Disable packet forwarding
echo 0 > /proc/sys/net/ipv4/ip_forwardDisable masquerading of packet forwarded to
waniptables -t nat -D POSTROUTING -o wan -j MASQUERADEBring
landownip link set dev eth0 downStop dnsmasq
If you are using sysvinit execute
invoke-rc.d dnsmasq stop.If you are using systemd execute
systemctl stop dnsmasq.