Difference between revisions of "Internet sharing"

From ArchWiki
Jump to: navigation, search
m (Instructions: clarified wording on (2))
(Changes on interface names, commands (rc.d), and some clean up)
Line 1: Line 1:
 
[[Category:Networking (English)]]
 
[[Category:Networking (English)]]
 
[[fr:Partage de connexion]]
 
[[fr:Partage de connexion]]
 +
[[es:Conexion a Internet compartida]]
 
{{i18n|Internet_Share}}
 
{{i18n|Internet_Share}}
 
{{Out of date}}
 
{{Poor writing}}
 
  
 
==Preface==
 
==Preface==
Let's assume you have an internet connection and you want to share it. There are two main ways to do that.
+
Let's assume you have an Internet connection and you want to share it. There are two main ways to do that.
  
 
<pre>
 
<pre>
   internet                           pc1
+
   Internet                           pc1
 
1. ----> |router| ---> |switch| --->-<
 
1. ----> |router| ---> |switch| --->-<
 
                                       pc2 ..etc
 
                                       pc2 ..etc
 
   
 
   
   internet
+
   Internet
 
2. ------> |pc1 (router)| --> pc2..etc
 
2. ------> |pc1 (router)| --> pc2..etc
 
</pre>
 
</pre>
Line 25: Line 23:
 
<li>Connect the two PCs using [http://en.wikipedia.org/wiki/Ethernet_crossover_cable crossover cable] or a [http://en.wikipedia.org/wiki/Network_switch switch].  If one of the two computers has a gigabit ethernet card, a regular ethernet cable should work.</li>
 
<li>Connect the two PCs using [http://en.wikipedia.org/wiki/Ethernet_crossover_cable crossover cable] or a [http://en.wikipedia.org/wiki/Network_switch switch].  If one of the two computers has a gigabit ethernet card, a regular ethernet cable should work.</li>
  
<li>Let's assume that the first card (with the internet) is called '''''eth0''''' and the other one (for the sharing) is called '''''eth1'''''. (If those two keep switching at every boot read [http://wiki.archlinux.org/index.php/Udev#Mixed_Up_Devices.2C_Sound.2FNetwork_Cards_Changing_Order_Each_Boot this] ).</li>
+
<li>Let's assume that the first card (with the internet) is called '''''internet0''''' and the other one (for the sharing) is called '''''local0'''''. (If those two keep switching at every boot read [http://wiki.archlinux.org/index.php/Udev#Mixed_Up_Devices.2C_Sound.2FNetwork_Cards_Changing_Order_Each_Boot this] ). The newtork interface of the client machine will be called '''''local1'''''.</li>
  
 
<li>Configure the second network card with:
 
<li>Configure the second network card with:
Line 31: Line 29:
 
:'''Netmask:''' 255.255.255.0
 
:'''Netmask:''' 255.255.255.0
 
or enter in a console (as root)
 
or enter in a console (as root)
<pre>ifconfig eth1 192.168.0.1 netmask 255.255.255.0
+
<pre>ifconfig local0 192.168.0.1 netmask 255.255.255.0
ifconfig eth1 up</pre></li>
+
ifconfig local0 up</pre></li>
  
 
<li>To make this permanent, install [[netcfg]] if you don't have it and set up a network profile in '''/etc/network.d''', drawing on the examples in '''/etc/network.d/examples'''.  Or, put the above lines in '''/etc/rc.local'''.
 
<li>To make this permanent, install [[netcfg]] if you don't have it and set up a network profile in '''/etc/network.d''', drawing on the examples in '''/etc/network.d/examples'''.  Or, put the above lines in '''/etc/rc.local'''.
Line 41: Line 39:
 
<li>Then edit '''/etc/sysctl.conf''' and set '''net.ipv4.ip_forward=1'''. This will make that change persistant after a reboot.</li>
 
<li>Then edit '''/etc/sysctl.conf''' and set '''net.ipv4.ip_forward=1'''. This will make that change persistant after a reboot.</li>
  
<li>(If you haven't already) Install iptables and enter this rule (for the forwarding of the internet to the second PC) and save iptables.
+
<li>Install iptables and enter this rule (because NAT is needed when sharing internet) and save iptables.
 
<pre>pacman -S iptables
 
<pre>pacman -S iptables
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
+
iptables -t nat -A POSTROUTING -o internet0 -j MASQUERADE
/etc/rc.d/iptables save</pre></li>
+
rc.d save iptables</pre></li>
  
<li>Edit '''/etc/conf.d/iptables''' and enable IP forwarding there (IPTABLES_FORWARD=1).  Or, the recommended alternative is to edit '''/etc/sysctl.conf''' and add the lines
+
<li>Edit '''/etc/sysctl.conf''' and add the lines
 
<pre>net.ipv4.ip_forward=1
 
<pre>net.ipv4.ip_forward=1
 
net.ipv6.conf.default.forwarding=1
 
net.ipv6.conf.default.forwarding=1
 
net.ipv6.conf.all.forwarding=1</pre></li>
 
net.ipv6.conf.all.forwarding=1</pre></li>
  
<li>Start iptables:<pre>/etc/rc.d/iptables start</pre></li>
+
<li>Start iptables:
 +
<pre>rc.d start iptables</pre></li>
  
 
<li>Add iptables in your DAEMONS array in your /etc/rc.conf so that it is started each time.</li>
 
<li>Add iptables in your DAEMONS array in your /etc/rc.conf so that it is started each time.</li>
  
<li>Go to the second PC and set:
+
<li>Go to the client PC and set:
 
:'''IP:''' 192.168.0.2
 
:'''IP:''' 192.168.0.2
 
:'''Netmask:''' 255.255.255.0
 
:'''Netmask:''' 255.255.255.0
Line 61: Line 60:
 
:'''DNS:''' The same DNS as the first PC
 
:'''DNS:''' The same DNS as the first PC
  
Proceed like this (eth0 is assumed to be your network interface on PC2 with which you are connected to PC1):
+
Proceed like this (internet0 is assumed to be your network interface on PC2 with which you are connected to PC1):
<pre>ifconfig eth0 192.168.0.2 netmask 255.255.255.0
+
<pre>ifconfig local1 192.168.0.2 netmask 255.255.255.0
ifconfig eth0 up
+
ifconfig local1 up
route add default gw 192.168.0.1 eth0
+
route add default gw 192.168.0.1 local1
 
echo "nameserver <adr of nameserver>" >> /etc/resolv.conf
 
echo "nameserver <adr of nameserver>" >> /etc/resolv.conf
 
</pre>
 
</pre>
  
You can figure out the address of the nameserver by looking into the /etc/resolv.conf of PC1, if its internet connection is already established.</li></ol>
+
You can figure out the address of the nameserver by looking into the /etc/resolv.conf of PC1, if its internet connection is already established. If you don't have a nameserver, you can use [https://code.google.com/speed/public-dns/ Google Public DNS] which is relatively fast. Its addresses are '''8.8.8.8''' and '''8.8.8.4'''.</li></ol>
  
 
{{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)}}
  
That's it. The second PC should now have internet.
+
That's it. The client PC should now have Internet.
  
 
==See also==
 
==See also==

Revision as of 04:43, 8 November 2011

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.


Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어


External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

Preface

Let's assume you have an Internet connection and you want to share it. There are two main ways to do that.

   Internet                           pc1
1. ----> |router| ---> |switch| --->-<
                                      pc2 ..etc
 
   Internet
2. ------> |pc1 (router)| --> pc2..etc

Instructions

I'll explain the second way (it is easier and requires one less machine).

  1. Install a second network card to the first PC.
  2. Connect the two PCs using crossover cable or a switch. If one of the two computers has a gigabit ethernet card, a regular ethernet cable should work.
  3. Let's assume that the first card (with the internet) is called internet0 and the other one (for the sharing) is called local0. (If those two keep switching at every boot read this ). The newtork interface of the client machine will be called local1.
  4. Configure the second network card with:
    IP: 192.168.0.1
    Netmask: 255.255.255.0
    or enter in a console (as root)
    ifconfig local0 192.168.0.1 netmask 255.255.255.0
    ifconfig local0 up
  5. To make this permanent, install netcfg if you don't have it and set up a network profile in /etc/network.d, drawing on the examples in /etc/network.d/examples. Or, put the above lines in /etc/rc.local.
  6. Enable packet forwarding. To do so, write a "1" to /proc/sys/net/ipv4/ip_forward with:
    echo 1 > /proc/sys/net/ipv4/ip_forward
  7. Then edit /etc/sysctl.conf and set net.ipv4.ip_forward=1. This will make that change persistant after a reboot.
  8. Install iptables and enter this rule (because NAT is needed when sharing internet) and save iptables.
    pacman -S iptables
    iptables -t nat -A POSTROUTING -o internet0 -j MASQUERADE
    rc.d save iptables
  9. Edit /etc/sysctl.conf and add the lines
    net.ipv4.ip_forward=1
    net.ipv6.conf.default.forwarding=1
    net.ipv6.conf.all.forwarding=1
  10. Start iptables:
    rc.d start iptables
  11. Add iptables in your DAEMONS array in your /etc/rc.conf so that it is started each time.
  12. Go to the client PC and set:
    IP: 192.168.0.2
    Netmask: 255.255.255.0
    Gateway: 192.168.0.1
    DNS: The same DNS as the first PC
    Proceed like this (internet0 is assumed to be your network interface on PC2 with which you are connected to PC1):
    ifconfig local1 192.168.0.2 netmask 255.255.255.0
    ifconfig local1 up
    route add default gw 192.168.0.1 local1
    echo "nameserver <adr of nameserver>" >> /etc/resolv.conf
    
    You can figure out the address of the nameserver by looking into the /etc/resolv.conf of PC1, if its internet connection is already established. If you don't have a nameserver, you can use Google Public DNS which is relatively fast. Its addresses are 8.8.8.8 and 8.8.8.4.
Note: Of course, this also works with a mobile broadband connection (usually called ppp0 on PC1)

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

See also