blog post internet sharing

This commit is contained in:
Jörg Thalheim 2014-02-08 20:35:02 +01:00
parent dc1a7e5012
commit bc971c97c4
5 changed files with 91 additions and 0 deletions

View File

@ -0,0 +1,43 @@
---
layout: post
title: "Internet sharing (ipv4 and ipv6) on archlinux using dnsmasq"
date: 2014-02-08 20:02:22 +0100
comments: true
categories:
- arch
- dhcp
- ipv6 router advertisement
---
A guide to connect with a different machine using a ethernet cable for
internet sharing or just transferring files:
1. Install dnsmasq and iproute2
$ pacman -S dnsmasq iproute2
2. Copy over the configuration files at the end of the article and edit the
*/etc/conf.d/share-internet* to match your network setup.
3. Start the sharing service with systemd
$ sudo systemctl start internet-sharing.service
After that the other machine can connect via dhcp. It will get an ipv4
address from the **10.20.0.0/24** subnet and a ipv6 address from the **fd21:30c2:dd2f::**
subnet. Your host will be reachable via **10.20.0.1** or **fd21:30c2:dd2f::1**.
Thanks to ipv6 router advertising, an AAAA record for each host is automatically set based on the hostname.
This means if your hostname is *foo*, all members of the network can just connect
to it using the address *foo*. You should disable the share-internet.service, if
you don't need it. Otherwise you might mess up network setups, if you connect to a
network with the device on which the dhcp service is running.
Happy networking!
{% include_code /etc/conf.d/share-internet lang:bash share-internet/share-internet %}
{% include_code /etc/systemd/system/share-internet.service lang:ini share-internet/share-internet.service %}
{% include_code /etc/dnsmasq.conf lang:ini share-internet/dnsmasq.conf %}
{% include_code /etc/dnsmasq.conf.dhcp lang:bash share-internet/dnsmasq.conf.dhcp %}

View File

@ -0,0 +1,7 @@
# google as an upstream dns server
server=8.8.8.8
server=8.8.4.4
no-resolv
cache-size=2000
#conf-file=/etc/dnsmasq.conf.dhcp

View File

@ -0,0 +1,10 @@
# no need to modify
# ingoing and outgoing device will be set by the systemd service
interface=enp0s25
no-dhcp-interface=wlp3s0
dhcp-range=10.20.0.100,10.20.0.199,infinite
dhcp-option=3,10.20.0.1 # router
dhcp-option=5,10.20.0.1 # dns
dhcp-option=42,10.20.0.1 # ntp
dhcp-range=fd21:30c2:dd2f::,ra-stateless,ra-names,infinite
enable-ra

View File

@ -0,0 +1,4 @@
# Network device where other hosts are connect to, ex: eth0
INTERNAL_DEVICE="enp0s25"
# Device which has internet access, ex: wlan0 or usb0
EXTERNAL_DEVICE="wlp3s0"

View File

@ -0,0 +1,27 @@
[Unit]
Description='share internet'
[Service]
Type=oneshot
EnvironmentFile=/etc/conf.d/share-internet
RemainAfterExit=yes
ExecStart=/usr/bin/sed -ie 's!^#conf-file=/etc/dnsmasq\.conf\.dhcp!conf-file=/etc/dnsmasq.conf.dhcp!' /etc/dnsmasq.conf
ExecStart=/usr/bin/sed -ie 's/^interface=[[:alnum:]]+/interface=$INTERNAL_DEVICE/' /etc/dnsmasq.conf.dhcp
ExecStart=/usr/bin/sed -ie 's/^no-dhcp-interface=[[:alnum:]]+/no-dhcp-interface=$EXTERNAL_DEVICE/' /etc/dnsmasq.conf.dhcp
ExecStart=/usr/bin/iptables -t nat -A POSTROUTING -o $EXTERNAL_DEVICE -j MASQUERADE
ExecStart=/usr/bin/ip6tables -t nat -A POSTROUTING -o $EXTERNAL_DEVICE -j MASQUERADE
ExecStart=/usr/bin/sysctl -w net.ipv4.ip_forward=1 net.ipv6.conf.all.forwarding=1
ExecStart=/usr/bin/ip addr add 10.20.0.1/24 dev $INTERNAL_DEVICE
ExecStart=/usr/bin/ip addr add fd21:30c2:dd2f::1/64 dev $INTERNAL_DEVICE
ExecStart=/usr/bin/systemctl restart dnsmasq
ExecStop=/usr/bin/ip addr del 10.20.0.1/24 dev $INTERNAL_DEVICE
ExecStop=/usr/bin/ip addr del fd21:30c2:dd2f::1/64 dev $INTERNAL_DEVICE
ExecStop=/usr/bin/sysctl -w net.ipv4.ip_forward=0 net.ipv6.conf.all.forwarding=0
ExecStop=/usr/bin/iptables -t nat -D POSTROUTING -o $EXTERNAL_DEVICE -i $INTERNAL_DEVICE -j MASQUERADE
ExecStop=/usr/bin/ip6tables -t nat -D POSTROUTING -o $EXTERNAL_DEVICE -i $INTERNAL_DEVICE -j MASQUERADE
ExecStop=/usr/bin/sed -ie 's!^conf-file=/etc/dnsmasq\.conf\.dhcp!#conf-file=/etc/dnsmasq.conf.dhcp!' /etc/dnsmasq.conf
ExecStop=/usr/bin/systemctl restart dnsmasq
[Install]
WantedBy=multi-user.target