Since this is like the #1 question I get from students and newcomers to Ubuntu I will try to answer it with a couple of things everyone should know about Ubuntu in regards to sharing an internet connection. This is divided in 3 sections:
- Sharing your Internet Connection via a Wired Connection
- Sharing your Internet connection via a Wireless Connection
- Sharing your Wireless Connection with Mobile Devices
- Connecting to a Wired or Wireless Device
Know that, in order to share your Internet connection either Wired or Wireless, you NEED to have at least 2 network cards, they can be a mix between a Wired card and a Wireless card, 2 Wired cards or 2 Wireless cards. The point is, you need to have at least 2 network cards, one card that receives your internet connection and another that sends it to other computers or devices on your network.
Sharing your Internet Connection via a Wired Connection
Configuring the PC that will share the connection
Assuming you are receiving Internet through a second Wired connection or a Wireless connection, Open your Network Manager via the Network Icon on the Unity Panel:
Go all the way down to the Edit Connections option and click on it.
You will appear on the Wired Tab. Each Wired connection is by default related to each Ethernet Wired NIC card you have. For example if you have 2 Wired NICs you will see 2 options here. In the image below you see one since this is a Laptop and they normally have one. Select the Wired connection you wish to edit which should be the one connected to your LAN and not the one that is receiving the Internet connection. Double click on it or select the EDIT button.
You will appear on the Wired Tab (Again). This time, go to the IPv4 Settings if you are using IPv4 or IPv6 if you are using that one. On the Method option select Share to Other Computers. Now SAVE. You are done.
This will do all the stuff that was normally done 6 or 7 years ago with all the terminal commands and iptable stuff.
Now just connect the 2 computers and you are done.
NOTE: If both computers have a Wired Gigabit Ethernet NIC then you can actually connect them directly since most 1000BASE-T have automatic MDI/MDI-X. If not just connect them to your switch, router, hub, whatever and that is all.
PC That will receive Shared Internet Connection (Depends on Network Setup)
Open your Network Manager via the Network Icon on the Unity Panel:
Go all the way down to the Edit Connections option and click on it.
You will appear on the Wired Tab. Each Wired connection is by default related to each Ethernet Wired NIC card you have. For example if you have 2 Wired NICs you will see 2 options here. In the image below you see one since this is a Laptop and they normally have one. Select the Wired connection you wish to edit and double click on it or select the EDIT button.
You will appear on the Wired Tab (Again). This time, go to the IPv4 Settings if you are using IPv4 or IPv6 if you are using that one. On the Method option select Manual. The options in Addresses will be editable now. Add the IP you want, the Mask you need and the IP of the PC that is sharing the connection. In this case I said that the IP of the Client PC will be 192.168.0.2, the Mask is 255.255.255.0 to imply that is a Class C (LAN) network and 192.168.0.1 to mention that the computer sharing the connection has the IP 192.168.0.1 with it. Additionally add the same IP of the computer sharing the connection to the DNS Servers (As shown in the image).
You can also (Depending on the network setup) select instead of Manual, select Automatic and just put the IP of the computer sharing the Internet in the DNS Servers section.
Note that this depends on the network setup. For example if you have both PCs connected to a switch or router, the switch/router will take care of the Client, so you only need to setup the PC Sharing Internet and just connect both to the switch/router. If by chance you are connecting the PCs directly together then it is better to do the additional setup of the Client.
In most cases, you will not need to setup the client, just connect to the network and done. Ubuntu will take care of everything. I just added the Client part for specific cases where problems might arise.
This should cover all the possible scenarios in a home LAN when one wants to share the internet connection over Wired cable.
Sharing your Internet connection via a Wireless Connection
NOTE: Read below for Android user information.
Since a couple of Ubuntu versions ago, there is no need for using masquerading techniques through terminal commands to share the internet connection. There isn't even the need to use
iwconfig and echo commands to forward stuff. For the many computers and laptops I have tested (Almost 1000 with Ubuntu) I can frankly say for sure that with the following steps you can share your internet connection with no trouble at all.
This is the same for open sourced and proprietary drivers. I can confirm for example Broadcom, Ralink and Atheros chipsets wireless cards will work out of the box, they will work since NetworkManager will take care of the low level details. The latest versions of Network Manager, specially since 0.9.x are excellent in this aspect.
Step 1 - Go to Network Manager and select Create New Wireless Network..
Note that there are other ways, terminal and GUI like, that can also do the job, but this is the easiest and fastest way possible (Even faster and easier than doing it in Windows XP or Windows 7). Also know that I am assuming you are receiving Internet through another network card (Wired or Wireless) and you want to share this Internet connection wirelessly.
Step 2 - Type name of SSID (The name that other devices will see when looking for the hotspot)
Step 3 - As you can see here I put as the SSID name "Ubuntu Ad Hoc Power" and it has an encryption of WEP. The encryption type depends on the network card. Some cards do no support Ad-hoc with WEP, others with WPA, and others with any security whatsoever. After setting the SSID name and security just click create.
Step 4 - For cases where you will share internet with somebody close to you on the same room, I normally just share the device with no security. Easier for them to connect. Of course do not do this in a party or in an office full of wireless vampires.
Step 5 - When a shared connection is created, the next time you try to share it, it will appear in a connection list, saving you the trouble of naming the connection and setting security.
After doing the above steps, you should have a computer sharing the internet connection with no problems at all. Now for the part that so many of us forget (specially me)
Sharing your Wireless Connection with Mobile Devices
Many users always ask why doesn't my phone (Android, IPhone, Blackberry) see the internet connection shared from my laptop?
In many of this cases, Android, IPhone or Blackberry will have disabled the option to "find" Ad-Hoc hotspots. This is for security measures. So they will only see actual Wireless routers and not Ad-hoc enabled devices like a Wireless PCI card, Wireless USB card or even another Ad-Hoc enabled phone. To be able to do this, at least in Android you need to root the device. For IPhone you need to jailbreak them. For Blackberries is more complicated. I have only done this on Androids and Iphones. After doing this you should see the Ad-Hoc device with no problem at all.
Of course this does not affect all models. Only some of them. Some you will only just buy and they will see Ad-hoc spots from day one. Others you will have to root/jail to fix this annoying problem.
I don't know if I can recommend some sites or ways to accomplish this here but google is your friend. Anyway a quick way to see this is to try to connect another computer to the one sharing the internet. It should work. Another way is to look for another cellphone that can see ad-hoc.
For Android users our sister site Android has a nice article on How to ROOT an android device
I also recommend this article which talks about several terms we should know when working with stuff like this: Phone Hacking Terminology
Lastly, sites like cyanogenmod and tools like clockworkmod are very good. But always remember that with great root cow power comes great responsibility.
For example I have a Huawei cellphone that had Android 2.2 originally. After I rooted the device and upgraded to 2.3 using Cyanogenmod I could see ad-hoc devices which saved me from several problems and added a bonus to my cellphone for stuff like using it as a remote control for XBMC.
I should also mention that I have read several of the answers in the Android SE site about Ad-Hoc support. Most are wrong in the sense that they confirm Ad-hoc not working on several older versions of Android (Version 4 and below). This is false since I have done probably more than 50 Android phones ranging from Samsung to Huawei and they all have worked. There might be some that do not get Ad-Hoc even after enabling Root and installing the needed apps but I have not yet come across one that had this issue.
So do not be discourage about looking to get your Ad-Hoc working on your phone. Trust me, it is easy. I hope I have shed some light with this to help you share quicker and easier and to understand when in some cases they do not find ad-hoc devices.
Connecting to a Wired or Wireless Device
Take a look at the image below:
If you click on the Network icon (The green circle at the top) it will drop a menu where you can see all the devices (Wired Connections and Wireless Connections) that you can use to connect. By clicking on them, they will automatically connect to the desire network. In the case of having a Wireless card and have not yet installed the drivers (In the case you need drivers for them), you can visit one of the following links that provide the steps needed to correctly install any Wireless card:
How do I get a Ralink RT3290 wireless card working?
Installing Broadcom Wireless Drivers
Drivers for Realtek wireless module for Toshiba C850-A965?
Wireless card Realtek RTL8723AE-BT is not recognized
As a bonus I recommend reading the How to connect and disconnect to a network manually in terminal?
(In cases where the need arises).