Tag/tag.png

Style Cleanup Required
This article does not follow the style standards in the Wiki Guide. More info...

Introduction

Internet Connection Sharing (ICS) provides the ability for one computer to share its Internet connection with another computer. To do this, a computer with an Internet connection must be configured to function as an Internet gateway. A second computer (or network of computers) connects to the Internet indirectly via the gateway computer.

Situations in which ICS may be necessary include:

GUI Method via Network Manager (Ubuntu 14.04, 16.04)

Assuming your Internet connection is on Wi-Fi, and you want to share via cable. On Ubuntu 14.04 open Network connections from the applet or via commandline nm-connection-editor, then add a connection, select type ethernet, create, then on tab IPv4 Settings select Method Shared to other computers. That should be all for connection sharing.

Assuming you have a cable connection and want to create a hotspot for other Wi-Fi devices. For Wi-Fi you need to set an SSID and Method Hotspot on tab Wi-Fi, as well as Security WPA and a password on Wi-Fi Security tab.

On 16.04 start the nm-connection-editor via commandline, configure as before, then the created connection appears and works from gnome3-shell network settings.

GUI Method via Network Manager (Ubuntu 12.04)

Open Settings->Network->Wireless and create a new Ad Hoc network. To use a common denominator for all devices choose WEP for security and create a 5 letters password from 0..9A..F. Note that this a least secure encryption standard.

GUI Method via Network Manager (Ubuntu 11.10)

Follow the GUI Method via Network Manager (Ubuntu 9.10 and up) below but there is a bug which turn off and on the share connection. The workaround for now is to set IPv6 options to Ignore and then sudo killall dnsmasq. Reconnect and it should work.

http://askubuntu.com/questions/64494/wired-connection-shared-with-other-computers-connects-then-disconnects-in-11-10

GUI Method via Network Manager (Ubuntu 9.10 and up)

In order to share an Internet connection, the computer that will do the sharing must have two network cards or ports. This assumes that you are using at least one Ethernet port and that it is identified as "eth0". eth0 will be the port that other computers will connect to you on.

When you are logged in:

A new window will open. Navigate to the tab titled "IPv4 Settings", and change the Method to "Shared to other computers". After restarting the computer, you should now be able to plug in any computer into your other Ethernet port or share through your wireless card.

Note: To clarify the above example, here is an example configuration that will work:

  1. You are already connected to the Internet using your wireless on port wlan0.
  2. The Ethernet port eth0 is connected to the PC that needs to share your Internet connection (or you could wire eth0 to a router for multiple machines).

Note: In the case of connecting a router, especially one with wireless, where you want the users to share your connection:

  1. Check before you start (in Synaptic or with dpkg-query -l dnsmasq*) that dnsmasq-base is installed and that dnsmasq is not installed. Install or uninstall as appropriate (see next section).

  2. After connecting the router, to enable masquerading, type:

sudo iptables -t nat -A POSTROUTING -j MASQUERADE

Wireless Ad-Hoc connection sharing scenario

Step-by-step guide:

sudo apt-get install dnsmasq-base

sudo apt-get remove dnsmasq

sudo /etc/init.d/network-manager restart

NetworkManager now should connect to itself (which means it creates the ad-hoc wireless network and routes any Internet traffic to your wired network interface). Now, connect with the client(s), and you should have a working Internet connection.

Ubuntu Internet Gateway Method (iptables)

You will need two network cards in the gateway computer, or a PPP interface and a network card. One network card (or PPP interface) connects to the Internet. We will call this card eth0. The other card connects to your internal network. We will call this eth1. It is also possible to do ICS with a single network card. In this case, use eth0 for the Internet and eth0:0 for the internal network.

  1. Internet <<==>> eth0 <> Ubuntu gateway <> eth1 <<==>> Client PC

  2. Internet <<==>> ppp0 <> Ubuntu gateway <> eth1 <<==>> Client PC

  3. Internet <<==>> eth0 <> Ubuntu gateway <> eth0:0 <<==>> Client PC

Gateway set up

The following example will focus on the most common gateway setup: an Ubuntu computer with two wired network adapters (eth0 and eth1) hosting ICS to a static internal network configured for the 192.168.0.x subnet.

For this example, eth0 is used to represent the network card connected to the Internet, and eth1 represents the network card connected to a client PC. You can replace eth0 and eth1 as needed for your situation. Also, any private IP subnet can be used for the internal network IP addresses.

In summary:

Your setup may be different. If so, make sure to change them accordingly in the following commands.

Configure internal network card

Configure your internal network card (eth1) for static IP like so:

sudo ip addr add 192.168.0.1/24 dev eth1

The external and internal network cards cannot be on the same subnet.

Configure NAT

Configure iptables for NAT translation so that packets can be correctly routed through the Ubuntu gateway.

sudo iptables -A FORWARD -o eth0 -i eth1 -s 192.168.0.0/24 -m conntrack --ctstate NEW -j ACCEPT
sudo iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -t nat -F POSTROUTING
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

The first rule allows forwarded packets (initial ones). The second rule allows forwarding of established connection packets (and those related to ones that started). The third rule does the NAT.

IPtables settings need to be set-up at each boot (they are not saved automatically), with the following commands:

sudo iptables-save | sudo tee /etc/iptables.sav

iptables-restore < /etc/iptables.sav

Enable routing

sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

net.ipv4.conf.default.forwarding=1
net.ipv4.conf.all.forwarding=1

The /etc/sysctl.conf edit is required because of the following bug in Hardy and later releases: Launchpad Bug Report

#net.ipv4.ip_forward=1

... so that it reads:

net.ipv4.ip_forward=1

Client set up

Any OS can connect to the Internet as an ICS client as long as networking has been configured correctly. The following example will focus on how to set up an Ubuntu ICS client. For this example, it is assumed that the client is connected to an Ubuntu gateway, which has been configured to share ICS on the 192.168.0.x subnet according to the gateway set up outlined above.

For this example, eth0 is the network card on the client which is connected (by crossover cable) to eth1 on the Ubuntu gateway. You can replace eth0 as needed for your situation. Also, any private IP subnet can be used for the internal network IP address, as long as it matches the subnet on the gateway.

Disable networking

sudo /etc/init.d/networking stop

Give the client a static IP address

sudo ip addr add 192.168.0.100/24 dev eth0

This IP address can be anything within the gateway's private IP range.

Configure routing

sudo ip route add default via 192.168.0.1

This address should match the IP address on the gateway's internal network card (eth1 in the above example).

Configure DNS servers

Unless your ICS gateway can also perform DNS, you must manually configure the client with your ISP DNS servers. If you do not know your ISP's DNS servers, you can use OpenDNS servers instead.

sudo cp /etc/resolv.conf /etc/resolv.conf.backup

sudo nano /etc/dhcp3/dhclient.conf

prepend domain-name-servers 208.67.222.222,208.67.220.220;

208.67.222.222 and 208.67.220.220 are OpenDNS DNS servers. If you wish to use your ISP's DNS servers, use them here instead of the OpenDNS servers.

Restart networking

sudo /etc/init.d/networking restart

Once this is finished, your client will now have access to the Internet via ICS. Please direct any questions/comments to the Internet Connection Sharing Documentation thread.


A beginner's working example of a Ubuntu Desktop with 2 NIC cards, sharing Internet connection: http://ubuntuforums.org/showthread.php?p=3713684

Advanced Gateway Configuration

The above example outlines how to do basic ICS on a static IP network. Once you have configured your Ubuntu computers for ICS and confirmed that everything works across your static network, there are a few advanced routing configurations which can make it much easier to set up the ICS client.

Advanced configurations include DHCP server and DNS server. A DHCP server allows the client to get an IP address automatically without having to manually configure a static IP. A DNS server allows the client to resolve Internet host names without manually configuring DNS addresses.

DHCP/DNS server

This is deceptively easy, and will be acceptable for most situations. However, it will not allow the ICS client to see computers on different subnets.

sudo aptitude install dnsmasq

sudo /etc/init.d/dnsmasq stop

sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf-backup

interface=eth1
dhcp-range=192.168.0.100,192.168.0.250,72h

Note: The "interface" should match the interface that your clients are connected to, and the "dhcp-range" should be within the gateway's private IP subnet that you configured according with the "Gateway set up" directions above.

sudo /etc/init.d/dnsmasq start

Now, your clients should be able to pull an automatic ip address and resolve host names.

Other approaches

The following section includes a rough outline of some alternative methods for configuring an ICS gateway. They are incomplete and untested. They are included simply for the sake of information.

Alternate server software (CLI)

There are other ways to host ICS, but they are outside the scope of this article.

Alternate NAT

The ipmasq daemon does NAT routing so you don't have to configure iptables. The following directions are incomplete and should not be considered a full description of what needs to be done to configure ipmasq.

sudo aptitude install ipmasq

Configure ipmasq to allow dhcp requests. Otherwise, you need to stop ipmasq to make a connection. You need to copy a .rul from the documentation directory into the /etc config and edit the interface name. Then, reconfigure ipmasq to start after networking has been started.

sudo dpkg-reconfigure ipmasq

Dedicated DHCP server

dhcp3 is an easy to configure and scalable true DHCP server that can be configured for many different aplications. dhcp3 configuration is more complex, but it can be useful in many situations:

https://help.ubuntu.com/community/dhcp3-server

Dedicated DNS server

BIND9 is a popular and well-supported local DNS server. It is very versatile and very powerful, but difficult to configure correctly:

https://help.ubuntu.com/community/BIND9ServerHowto

Alternate gateway software (GUI)

Another approach is to set up Firestarter, to run connection sharing, to set up dhcp3-server, and to set its configuration to listen to the correct eth*. To change this later, run sudo dpkg-reconfigure dhcp3-server.

Basically, you need to have Firestarter active/turned on/protecting, to have the connection shared.

When you install dhcp3-server, it will place a sample config file in your /etc/dhcp3 folder, called dhcpd.conf. I suggest that you install dhcp3-server first and then Firestarter because if you are lucky, Firestarter will set up a new dhcp3 config file for you.

At any time that changes are made to your dhcpd.conf file, restart the server by typing sudo /etc/init.d/dhcp3-server restart. Alternatively, every time you run the sudo dpkg-reconfigure dhcp3-server command, at the end, your server will restart.

There are several issues that I had. First of all, the Firestarter firewall won't even start if you don't have it configured to listen to the right interface. You can change which one it listens to in Preferences --> Network Settings. The Local-network-connected device must be the same as you have dhcp3-server listening to. Of course, both checkboxes under that need to be checked. The Internet-connected network device will be the one that is configured for Internet. Now, I have two NICs, but I have PPPoE configured on eth0, and I have Internet connection sharing configured on the same one, because eth0 is also configured for a static 192.168 internal IP for my internal network.

Simple iptables example

In a simple example, wlan0 has the Internet connection, and eth0 is being used to share the connection. It could be connected directly with a single computer via a crossover cable or switch, or you could have a router with a cable from eth0 to the WAN port and a whole LAN setup behind this. Interestingly, the Internet connection could be ppp0, a 3G, or mobile Internet modem.

You could use the above as a bash script changing things to suit your needs.


Further reading: https://help.ubuntu.com/community/IptablesHowTo

Internet connection sharing documentation thread: http://ubuntuforums.org/showthread.php?t=503287 (Please note that this thread is in an old read-only section of the forum and cannot be posted to. Requests for help may be posted in one of the main sections of the forum.)

See also


CategoryHardware CategoryInternet CategoryNetworking

Internet/ConnectionSharing (last edited 2018-11-22 07:07:31 by knome)