Google
 

Thursday, February 25, 2010

Internet Connection Sharing in Linux over Ad-hoc Wireless

Laptops are common these days and so is wireless networking that comes with it. Almost all of us use Wireless Router for wireless networking and internet sharing. But most of us might not be familiar with ad-hoc mode that works in every wireless card and can eliminate the use of router altogether. Here's how:

To setup ad-hoc wireless, you basically set two or more wireless cards to same ESSID in ad-hoc mode and different IP addresses.

Setting the wireless card in ad-hoc mode

Generally iwconfig is used to set the wireless mode, but if you have madwifi drivers (see this too), the commands are slightly different. Go to terminal and type,

iwconfig
If you see interfaces named wlan0, wlan1, etc iwconfig will work. If you see interface names similar to ath1 and wifi0, then madwifi specific commands are to be used.

For general wifi cards:
sudo iwconfig wlan0 mode ad-hoc
Replace wlan0 with the interface name as listed by iwconfig

For cards using madwifi driver:
sudo wlanconfig ath1 destroy
sudo wlanconfig ath1 create wlandev wifi0 wlanmode adhoc
Now set essid. Pick some name like 'adhocwifi'
sudo iwconfig wlan0 essid adhocwifi
If required, to set encryption key
sudo iwconfig wlan0 key 1234567890
Now set IP address:
sudo ifconfig wlan0 192.168.0.1
Follow the above steps for another wireless card and set IP address in same subnet, say 192.168.0.2, and ping each other.

The whole ad-hoc mode setting can also be done using GUI network-manager, but since i'm not so much fond of graphical interface i'm not covering it. It should be simple.. :)

Internet Connection Sharing

Now to share the internet over wireless,
sudo iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
where ppp0 is the connection you want to share (PPPoE connection in this case)

You also need to enable IP forwarding:
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
Or, to enable permanently add the following line to /etc/sysctl.conf
net.ipv4.ip_forward=1
Some ISPs might limit the TTL so that you wont be able to share the internet. Fix:
sudo iptables -t mangle -A PREROUTING -j TTL --ttl-inc 1

Using the shared internet (in Linux)

Now to use the shared internet on another computer, set it to ad-hoc mode and assign IP address in the same subnet as described above and perform the following:

1. Set the IP of computer sharing internet as gateway
sudo route add default gw 192.168.0.1
2. Set DNS server. We're using Google's DNS.
sudo sh -c "echo 'nameserver 8.8.8.8' >> /etc/resolv.conf"
You can also use IP and DNS Masquerading to ease the task.


Using the shared internet (in M$ Windows)

If you want to use the shared internet on M$ Windows,

1. Connect to the Wireless Network (in this case 'adhocwifi')
2. Go to Network Connections
3. Right click the Wireless Connection
4. Select the Internet Protocol (TCP/IP) and click Properties
5. Set IP to 192.168.0.2 (or accordingly in the same subnet as set on Linux box), gateway to 192.168.0.1 (as set on Linux box) and DNS to 8.8.8.8

15 comments:

  1. Worked like a charm... :)

    ReplyDelete
  2. Perfect!
    Thanks for the tip!

    ReplyDelete
  3. What if I don't want to use an IP, instead share only the internet? I used NetworkManager and use share to other computers on IPv4 settings (no IP entered) and manage to share the connection with my other PC.

    But I wanna try the command line for that. Can you please help me?

    ReplyDelete
  4. there's no such thing as not using IP, maybe the network manager uses dhcp to assign IP. please follow tutorial at http://lartc.org/ for commandline details.

    ReplyDelete
  5. sorry for the confusion, what I meant is dchp. I am just new to linux :(

    ReplyDelete
  6. hi there,I used NetworkManager and use share to other computers on IPv4 settings (no IP entered) and manage to share the connection with my other PC.
    Michael
    security+ test
    USA

    ReplyDelete
  7. Good write up man.. help for all beginners of linux as well as networking and ad-hoc

    Keep writing. All the very best.

    R@J

    ReplyDelete
  8. Appreicate your thoughts, Im not always in agreement, but you do cause a peron to think keep blogging!carpet cleaning adelaide

    ReplyDelete
  9. How can i share my internet connection over WiFi to connect my Android device through WiFi ..??

    ReplyDelete
  10. Thanks, helped getting started with ad-hoc wireless on Linux. For increased convenience we can run the "dnsmasq" daemon on the internet gateway which provides dns and dhcp services. (Set wireless network interface and IP range in /etc/dnsmasq.conf.) On other machines, "dhclient" then does the IP auto-configuration.

    ReplyDelete
  11. great blog ... thanks for sharing....

    ReplyDelete
  12. Hello

    "sudo iwconfig wlan0 mode ad-hoc" gives error:

    satyam@ubuntu:~$ sudo iwconfig wlan0 mode ad-hoc
    [sudo] password for satyam:
    Error for wireless request "Set Mode" (8B06) :
    SET failed on device wlan0 ; Operation not supported.
    satyam@ubuntu:~$

    Please help

    ReplyDelete
  13. This blog is truly useful to convey overhauled instructive undertakings over web which is truly examination. I discovered one fruitful case of this truth through this blog. I will utilize such data now.HPE ProLiant ML350 Gen9

    ReplyDelete