Difference between revisions of "Internet sharing"

From ArchWiki
Jump to: navigation, search
m (style, cleanup)
(update link(s) (avoid redirect))
(22 intermediate revisions by 8 users not shown)
Line 1: Line 1:
 
[[Category:Networking]]
 
[[Category:Networking]]
 
[[cs:Internet Share]]
 
[[cs:Internet Share]]
[[es:Conexion a Internet compartida]]
 
 
[[fr:Partage de connexion]]
 
[[fr:Partage de connexion]]
 
[[it:Internet Share]]
 
[[it:Internet Share]]
Line 16: Line 15:
 
== Configuration ==
 
== Configuration ==
  
This section assumes, that the network device connected to the other computer(s) is named '''''net0''''' and the network device connected to the internet as '''''internet0'''''.
+
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'''''.
  
 
{{Tip|You can rename your devices to this scheme using [[Udev#Setting static device names]].}}
 
{{Tip|You can rename your devices to this scheme using [[Udev#Setting static device names]].}}
Line 30: Line 29:
 
=== Enable packet forwarding ===
 
=== Enable packet forwarding ===
  
Enter this command to temporaly enable packet forwarding:
+
Check the current packet forwarding settings;
 +
# sysctl -a | grep forward
 +
 
 +
Enter this command to temporarily enable packet forwarding:
 
  # sysctl net.ipv4.ip_forward=1
 
  # sysctl net.ipv4.ip_forward=1
  
Edit {{ic|/etc/sysctl.conf}} to make the previous change persistent after a reboot.
+
Edit {{ic|/etc/sysctl.d/30-ipforward.conf}} to make the previous change persistent after a reboot.
{{hc|/etc/sysctl.conf|<nowiki>
+
{{hc|/etc/sysctl.d/30-ipforward.conf|<nowiki>
 
net.ipv4.ip_forward=1
 
net.ipv4.ip_forward=1
</nowiki>}}
 
 
If you are using ipv6, use this:
 
{{hc|/etc/sysctl.conf|<nowiki>
 
 
net.ipv6.conf.default.forwarding=1
 
net.ipv6.conf.default.forwarding=1
 
net.ipv6.conf.all.forwarding=1
 
net.ipv6.conf.all.forwarding=1
Line 46: Line 44:
 
=== Enable NAT ===
 
=== Enable NAT ===
  
[[pacman|Install]] the package {{Pkg|iptables}} from the [[Official Repositories|official repositories]]. Use iptables to enable NAT:
+
[[pacman|Install]] the package {{Pkg|iptables}} from the [[official repositories]]. Use iptables to enable NAT:
  
 
  # iptables -t nat -A POSTROUTING -o internet0 -j MASQUERADE
 
  # iptables -t nat -A POSTROUTING -o internet0 -j MASQUERADE
  # iptables-save > /etc/iptables/iptables.rules
+
  # iptables -A FORWARD -i net0 -o internet0 -j ACCEPT
  # systemctl start iptables
+
  # iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
  
 
{{Note|Of course, this also works with a mobile broadband connection (usually called ppp0 on PC1).}}
 
{{Note|Of course, this also works with a mobile broadband connection (usually called ppp0 on PC1).}}
  
{{Tip|You can set {{ic|iptables.service}} to [[Daemon|auto start at boot]].}}
+
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]].
 
+
Read the [[iptables]] article for more information. There's also an excellent guide on iptables [[Simple stateful firewall]].
+
  
 
=== Assigning ip addresses to the client pc(s) ===
 
=== Assigning ip addresses to the client pc(s) ===
Line 73: Line 69:
 
  # ip route add default via 139.96.30.100 dev eth0
 
  # ip route add default via 139.96.30.100 dev eth0
  
Add a nameserver:
+
Configure a DNS server for each client, see [[resolv.conf]] for details.
# echo "nameserver <nameserver ip>" >> /etc/resolv.conf
+
 
+
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.
+
 
+
If you don't have a nameserver, you can use any of the free public DNS servers, which are relatively fast:
+
* [https://code.google.com/speed/public-dns/ Google Public DNS]
+
** 8.8.8.8
+
** 8.8.4.4
+
** [2001:4860:4860::8888]
+
** [2001:4860:4860::8844]
+
* [https://opendns.com OpenDNS]:
+
** 208.67.222.222
+
** 208.67.220.220
+
** [2620:0:ccc::2]
+
** [2620:0:ccd::2]
+
 
+
{{Note|Bracket notation must be used for IPv6 addresses in resolv.conf.}}
+
  
 
That's it. The client PC should now have Internet.
 
That's it. The client PC should now have Internet.
Line 100: Line 79:
 
== See also ==
 
== See also ==
  
* [[Sharing ppp connection with wlan interface]]
+
* [[Ad-hoc networking]]
 +
* [[Sharing PPP Connection]]
 
* [[Simple stateful firewall]]
 
* [[Simple stateful firewall]]
 
* [[Router]]
 
* [[Router]]
 
* [[USB 3G Modem]]
 
* [[USB 3G Modem]]

Revision as of 13:04, 25 February 2014

This article explains how to share the internet connection from one machine to other(s).

Requirements

  • The machine acting as server should have an additional network device.
  • 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 switch is required. If you are sharing to only one machine, a crossover cable is sufficient.
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

Configuration

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.

Tip: You can rename your devices to this scheme using Udev#Setting static device names.

Static IP address

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 139.96.30.100/24 dev net0 # arbitrary address

To have your static ip assigned at boot, you can use netctl.

Enable packet forwarding

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

Enable 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 -i net0 -o internet0 -j ACCEPT
# iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
Note: Of course, this also works with a mobile broadband connection (usually called ppp0 on PC1).

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.

Assigning ip addresses to the client pc(s)

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.

Manually adding an ip

Instead of using dhcp, on each client pc, add an ip address and the default route:

# ip addr add 139.96.30.120/24 dev eth0
# ip link set up dev eth0
# ip route add default via 139.96.30.100 dev eth0

Configure a DNS server for each client, see resolv.conf for details.

That's it. The client PC should now have Internet.

Troubleshooting

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.

See also