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

−

{{Out of date}}

+

==Requirements==

−

{{Poor writing}}

+

* The machine acting as server should have an additional network device

+

* That network device should be connected to the machines are going to receive internet access. They can be one or more machines,. To be able to share internet to several machies a [[Wikipedia:Network switch|switch]] is required. If you are sharing to only one machine, a [[Wikipedia:Ethernet crossover cable|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==

+

Using [[Udev#Setting static device names]], name the network device connected the other computer(s) as '''''net0''''' and the network device connected to the internet as '''''internet0'''''.

+

===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.

+

{{bc|

+

# 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 [[netcfg]].

−

==Preface==

+

===Enable packet forwarding===

−

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

+

Enter this command to temporaly enable packet forwarding:

−

+

{{bc|<nowiki>sysctl net.ipv4.ip_forward=1</nowiki>}}

−

<pre>

+

Edit {{ic|/etc/sysctl.conf}} and add this line, which will make the previous change persistent after a reboot.

−

internet pc1

+

{{bc|<nowiki>net.ipv4.ip_forward=1</nowiki>}}

−

1. ----> |router| ---> |switch| --->-<

+

If you are using ipv6, use these lines:

−

pc2 ..etc

+

{{bc|<nowiki>

−

+

net.ipv6.conf.default.forwarding=1

−

internet

+

net.ipv6.conf.all.forwarding=1

−

2. ------> |pc1 (router)| --> pc2..etc

+

</nowiki>}}

−

</pre>

+

===Enable NAT===

−

+

[[pacman|Install]] the package {{Pkg|iptables}} from the [[Official Repositories|official repositories]].

−

==Instructions==

+

Use iptables to enable NAT:{{bc|<nowiki>

−

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

+

# iptables -t nat -A POSTROUTING -o internet0 -j MASQUERADE

−

<ol>

+

# iptables-save > /etc/iptables/iptables.rules

−

<li>Install a second network card to the first PC.</li>

+

# systemctl start iptables

−

+

</nowiki>}}{{Note| Of course, this also works with a mobile broadband connection (usually called ppp0 on PC1)}}

−

<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 might work.</li>

+

You can set {{ic|iptables.service}} to [[Daemon|auto start a boot]].

−

−

<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>Configure the second network card with:

−

:'''IP:''' 192.168.0.1

−

:'''Netmask:''' 255.255.255.0

−

or enter in a console (as root)

−

<pre>ifconfig eth1 192.168.0.1 netmask 255.255.255.0

−

ifconfig eth1 up</pre></li>

−

<li>Enter that same information in your '''/etc/rc.conf''' so that this card is set up correctly earch time your computer starts.

+

Read the [[iptables]] article for more information.

−

'''Note''': If you use '''Wicd''', you do not do this.

−

<pre>interface=eth0

−

address=192.168.0.1

−

netmask=255.255.255.0

−

broadcast=

−

gateway=

−

INTERFACES=(lo eth0 eth1)</pre></li>

−

<li>Enable packet forwarding. To do so, write a "'''1'''" to '''/proc/sys/net/ipv4/ip_forward''' with:

+

===Assigning ip addresses to the client pc(s)===

−

<pre>echo 1 > /proc/sys/net/ipv4/ip_forward</pre></li>

+

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]].

−

−

<li>Then edit '''/etc/sysctl.conf''' and set 1 to the net.ipv4.ip_forward (It should be '''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.

−

<pre>pacman -S iptables

−

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

−

/etc/rc.d/iptables save</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

−

<pre>net.ipv4.ip_forward=1

−

net.ipv6.conf.default.forwarding=1

−

net.ipv6.conf.all.forwarding=1</pre></li>

−

<li>Start iptables:<pre>/etc/rc.d/iptables start</pre></li>

+

You can read the [[dhcpd]] wiki article, to add a dhcp server. Then, install the [[dhcpcd]] client on every client pc.

−

<li>Add iptables in your DAEMONS array in your /etc/rc.conf so that it is started each time.</li>

+

If you are not planing to use this setup regularly, you can manually add an ip to each client instead.

+

====Manually adding an ip====

−

<li>Go to the second PC and set:

+

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

−

:'''IP:''' 192.168.0.2

+

{{bc|<nowiki>

−

:'''Netmask:''' 255.255.255.0

+

ip addr add 139.96.30.120/24 dev eth0

−

:'''Gateway:''' 192.168.0.1

+

ip link set up dev eth0

−

:'''DNS:''' The same DNS as the first PC

+

ip route add default via 139.96.30.100 dev eth0

+

</nowiki>}}

+

Add a nameserver:

+

{{bc|<nowiki>

+

echo "nameserver <nameserver ip>" >> /etc/resolv.conf

+

</nowiki>}}

−

Proceed like this (eth0 is assumed to be your network interface on PC2 with which you are connected to PC1):

+

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 [https://code.google.com/speed/public-dns/ Google Public DNS] which is relatively fast. Its addresses are '''8.8.8.8''' and '''8.8.4.4'''.

−

<pre>ifconfig eth0 192.168.0.2 netmask 255.255.255.0

−

ifconfig eth0 up

−

route add default gw 192.168.0.1 eth0

−

echo "nameserver <adr of nameserver>" >> /etc/resolv.conf

−

</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>

+

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

−

{{Note| Of course, this also works with a mobile broadband connection (usually called ppp0 on PC1)}}

+

== Troubleshooting ==

−

That's it. The second 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 [https://bbs.archlinux.org/viewtopic.php?pid=1093208 interfering].

==See also==

==See also==

Revision as of 02:09, 13 January 2013

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

Contents

Requirements

The machine acting as server should have an additional network device

That network device should be connected to the machines are going to receive internet access. They can be one or more machines,. To be able to share internet to several machies 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

Using Udev#Setting static device names, name the network device connected the other computer(s) as net0 and the network device connected to the internet as internet0.

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.

You can figure out the address of the nameserver by looking into the /etc/resolv.conf of the server, 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.4.4.

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.