インターネット共有
関連記事
この記事ではあるマシンと他のマシンでインターネット接続を共有する方法を解説します。
目次
要件
- サーバーとして機能するマシンにはネットワークデバイスを別に設定する必要があります。
- ネットワークデバイスはインターネットにアクセスするマシンと接続します。一つ、または複数のマシンを使うことができます。複数のマシンとインターネットを共有するにはスイッチが必要です。一つのマシンで共有する場合は、クロスオーバーケーブルで十分です。
設定
This section assumes, that the network device connected to the client computer(s) is named net0 and the network device connected to the internet as internet0.
固定 IP アドレス
Assign an static IPv4 address to the interface connected to the other machines. The first 3 bytes of this address cannot be exactly the same as those of another interface.
# ip link set up dev net0 # ip addr add 192.168.123.100/24 dev net0 # arbitrary address
To have your static ip assigned at boot, you can use netctl.
パケット転送の有効化
Check the current packet forwarding settings:
# sysctl -a | grep forward
Enter this command to temporarily enable packet forwarding:
# sysctl net.ipv4.ip_forward=1
Edit /etc/sysctl.d/30-ipforward.conf
to make the previous change persistent after a reboot.
/etc/sysctl.d/30-ipforward.conf
net.ipv4.ip_forward=1 net.ipv6.conf.default.forwarding=1 net.ipv6.conf.all.forwarding=1
NAT の有効化
Install the package iptables from the official repositories. Use iptables to enable NAT:
# iptables -t nat -A POSTROUTING -o internet0 -j MASQUERADE # iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT # iptables -A FORWARD -i net0 -o internet0 -j ACCEPT
Read the iptables article for more information (especially saving the rule and applying it automatically on boot). There is also an excellent guide on iptables Simple stateful firewall.
クライアント PC に IP アドレスを割り当てる
If you are planning to regularly have several machines using the internet shared by this machine, then is a good idea to install a dhcp server.
You can read the dhcpd wiki article, to add a dhcp server. Then, install the dhcpcd client on every client pc.
If you are not planing to use this setup regularly, you can manually add an ip to each client instead.
手動で IP を追加
Instead of using dhcp, on each client pc, add an ip address and the default route:
# ip addr add 192.168.123.201/24 dev eth0 # arbitrary address, first three blocks must match the address from above # ip link set up dev eth0 # ip route add default via 192.168.123.100 dev eth0 # same address as in the beginning
Configure a DNS server for each client, see resolv.conf for details.
That's it. The client PC should now have Internet.
トラブルシューティング
If you are able to connect the two PCs but cannot send data (for example, if the client PC makes a DHCP request to the server PC, the server PC receives the request and offers an IP to the client, but the client does not accept it, timing out instead), check that you don't have other Iptables rules interfering.