https://wiki.archlinux.org/api.php?action=feedcontributions&user=Ctingwai&feedformat=atomArchWiki - User contributions [en]2024-03-29T08:26:08ZUser contributionsMediaWiki 1.41.0https://wiki.archlinux.org/index.php?title=Internet_sharing&diff=293135Internet sharing2014-01-16T11:17:54Z<p>Ctingwai: /* Enable NAT */ Added forwarding rules</p>
<hr />
<div>[[Category:Networking]]<br />
[[cs:Internet Share]]<br />
[[es:Conexion a Internet compartida]]<br />
[[fr:Partage de connexion]]<br />
[[it:Internet Share]]<br />
[[ru:Internet Share]]<br />
This article explains how to share the internet connection from one machine to other(s).<br />
<br />
== Requirements ==<br />
<br />
* The machine acting as server should have an additional network device.<br />
* That network device should be connected to the machines that are going to receive internet access. They can be one or more machines. To be able to share internet to several machines a [[Wikipedia:Network switch|switch]] is required. If you are sharing to only one machine, a [[Wikipedia:Ethernet crossover cable|crossover cable]] is sufficient.<br />
<br />
{{Note|If one of the two computers has a gigabit ethernet card, a crossover cable is not necessary and a regular ethernet cable should be enough}}<br />
<br />
== Configuration ==<br />
<br />
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'''''.<br />
<br />
{{Tip|You can rename your devices to this scheme using [[Udev#Setting static device names]].}}<br />
<br />
=== Static IP address ===<br />
<br />
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.<br />
# ip link set up dev net0<br />
# ip addr add 139.96.30.100/24 dev net0 # arbitrary address<br />
<br />
To have your static ip assigned at boot, you can use [[netctl]].<br />
<br />
=== Enable packet forwarding ===<br />
<br />
Check the current packet forwarding settings;<br />
# sysctl -a | grep forward<br />
<br />
Enter this command to temporarily enable packet forwarding:<br />
# sysctl net.ipv4.ip_forward=1<br />
<br />
Edit {{ic|/etc/sysctl.d/30-ipforward.conf}} to make the previous change persistent after a reboot.<br />
{{hc|/etc/sysctl.d/30-ipforward.conf|<nowiki><br />
net.ipv4.ip_forward=1<br />
net.ipv6.conf.default.forwarding=1<br />
net.ipv6.conf.all.forwarding=1<br />
</nowiki>}}<br />
<br />
=== Enable NAT ===<br />
<br />
[[pacman|Install]] the package {{Pkg|iptables}} from the [[Official Repositories|official repositories]]. Use iptables to enable NAT:<br />
<br />
# iptables -t nat -A POSTROUTING -o internet0 -j MASQUERADE<br />
# iptables -A FORWARD -i internet0 -o internal0 -j ACCEPT<br />
# iptables -A FORWARD -i internal0 -o internet0 -j ACCEPT<br />
<br />
Replace <code>internet0</code> with the device name of your network device that receives internet access and <code>internal0</code> with the device name that you want to share internet connection, e.g. <code>eth0</code>, <code>tun0</code> etc.<br />
<br />
{{Note|Of course, this also works with a mobile broadband connection (usually called ppp0 on PC1).}}<br />
<br />
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]].<br />
<br />
=== Assigning ip addresses to the client pc(s) ===<br />
<br />
If you are planning to regularly have several machines using the internet shared by this machine, then is a good idea to install a [[Wikipedia:dhcp|dhcp server]].<br />
<br />
You can read the [[dhcpd]] wiki article, to add a dhcp server. Then, install the [[dhcpcd]] client on every client pc.<br />
<br />
If you are not planing to use this setup regularly, you can manually add an ip to each client instead.<br />
<br />
==== Manually adding an ip ====<br />
<br />
Instead of using dhcp, on each client pc, add an ip address and the default route:<br />
# ip addr add 139.96.30.120/24 dev eth0<br />
# ip link set up dev eth0<br />
# ip route add default via 139.96.30.100 dev eth0<br />
<br />
{{Merge|resolv.conf|The following should be merged there to keep things in one place.}}<br />
<br />
Add a nameserver:<br />
# echo "nameserver <nameserver ip>" >> /etc/resolv.conf<br />
<br />
You can figure out the address of the nameserver by looking into the {{ic|/etc/resolv.conf}} of the server, if its Internet connection is already established.<br />
<br />
If you don't have a nameserver, you can use any of the free public DNS servers, which are relatively fast:<br />
* [https://code.google.com/speed/public-dns/ Google Public DNS]<br />
** 8.8.8.8<br />
** 8.8.4.4<br />
** [2001:4860:4860::8888]<br />
** [2001:4860:4860::8844]<br />
* [https://opendns.com OpenDNS]:<br />
** 208.67.222.222<br />
** 208.67.220.220<br />
** [2620:0:ccc::2]<br />
** [2620:0:ccd::2]<br />
<br />
{{Note|Bracket notation must be used for IPv6 addresses in resolv.conf.}}<br />
<br />
That's it. The client PC should now have Internet.<br />
<br />
== Troubleshooting ==<br />
<br />
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 [https://bbs.archlinux.org/viewtopic.php?pid=1093208 interfering].<br />
<br />
== See also ==<br />
<br />
* [[Ad-hoc networking]]<br />
* [[Sharing PPP Connection]]<br />
* [[Simple stateful firewall]]<br />
* [[Router]]<br />
* [[USB 3G Modem]]</div>Ctingwai